Unverified Commit 78783d8f authored by Emelia Smith's avatar Emelia Smith Committed by Hong Minhee
Browse files

Fix incorrect handling of relative path URIs in link alternate headers

parent 72fedb62
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -8,6 +8,11 @@ Version 0.15.4

To be released.

 -  Fixed default document loaders' incorrect handling of relative URIs in
    `Link` headers with `rel=alternate`.  [[#155] by Emelia Smith]

[#155]: https://github.com/dahlia/fedify/pull/155


Version 0.15.3
--------------
@@ -1567,4 +1572,4 @@ Version 0.1.0

Initial release.  Released on March 8, 2024.

<!-- cSpell: ignore Dogeon Fabien Wressell -->
<!-- cSpell: ignore Dogeon Fabien Wressell Emelia -->
+39 −0
Original line number Diff line number Diff line
@@ -83,6 +83,19 @@ test("fetchDocumentLoader()", async (t) => {
      },
    ));

  mf.mock("GET@/link-obj-relative", (_req) =>
    new Response(
      "",
      {
        status: 200,
        headers: {
          "Content-Type": "text/html; charset=utf-8",
          Link: '</object>; rel="alternate"; ' +
            'type="application/activity+json"',
        },
      },
    ));

  await t.step("Link header", async () => {
    assertEquals(await fetchDocumentLoader("https://example.com/link-ctx"), {
      contextUrl: "https://www.w3.org/ns/activitystreams",
@@ -106,6 +119,32 @@ test("fetchDocumentLoader()", async (t) => {
    });
  });

  await t.step("Link header relative url", async () => {
    assertEquals(await fetchDocumentLoader("https://example.com/link-ctx"), {
      contextUrl: "https://www.w3.org/ns/activitystreams",
      documentUrl: "https://example.com/link-ctx",
      document: {
        id: "https://example.com/link-ctx",
        name: "Fetched object",
        type: "Object",
      },
    });

    assertEquals(
      await fetchDocumentLoader("https://example.com/link-obj-relative"),
      {
        contextUrl: null,
        documentUrl: "https://example.com/object",
        document: {
          "@context": "https://www.w3.org/ns/activitystreams",
          id: "https://example.com/object",
          name: "Fetched object",
          type: "Object",
        },
      },
    );
  });

  mf.mock("GET@/html-link", (_req) =>
    new Response(
      `<html>
+4 −3
Original line number Diff line number Diff line
@@ -133,18 +133,19 @@ async function getRemoteDocument(
    } else {
      const entries = link.getByRel("alternate");
      for (const [uri, params] of entries) {
        const altUri = new URL(uri, docUrl);
        if (
          "type" in params &&
          (params.type === "application/activity+json" ||
            params.type === "application/ld+json" ||
            params.type.startsWith("application/ld+json;")) &&
          new URL(uri).href !== docUrl.href
          altUri.href !== docUrl.href
        ) {
          logger.debug(
            "Found alternate document: {alternateUrl} from {url}",
            { alternateUrl: uri, url: documentUrl },
            { alternateUrl: altUri.href, url: documentUrl },
          );
          return await fetch(uri);
          return await fetch(altUri.href);
        }
      }
    }