Commit 996ffafc authored by Lee ByeongJun's avatar Lee ByeongJun
Browse files

fix: configurable max redirection

parent 0e336a3a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
dist/
deno.lock
node_modules/
package-lock.json
repomix-output.xml
t.ts
t2.ts
+39 −0
Original line number Diff line number Diff line
@@ -171,6 +171,45 @@ test({
      assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
    });

    fetchMock.removeRoutes();
    let redirectCount = 0;
    fetchMock.get(
      "begin:https://example.com/.well-known/webfinger",
      () => {
        redirectCount++;
        if (redirectCount < 3) {
          return {
            status: 302,
            headers: { Location: `/.well-known/webfinger?redirect=${redirectCount}` },
          };
        }
        return { body: expected };
      },
    );

    await t.step("custom maxRedirection", async () => {
      // Test with maxRedirection: 2 (should fail)
      redirectCount = 0;
      assertEquals(
        await lookupWebFinger("acct:johndoe@example.com", { maxRedirection: 2 }),
        null,
      );

      // Test with maxRedirection: 3 (should succeed)
      redirectCount = 0;
      assertEquals(
        await lookupWebFinger("acct:johndoe@example.com", { maxRedirection: 3 }),
        expected,
      );

      // Test with default maxRedirection: 5 (should succeed)
      redirectCount = 0;
      assertEquals(
        await lookupWebFinger("acct:johndoe@example.com"),
        expected,
      );
    });

    fetchMock.hardReset();
  },
});
+10 −2
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ import type { ResourceDescriptor } from "./jrd.ts";

const logger = getLogger(["fedify", "webfinger", "lookup"]);

const MAX_REDIRECTION = 5; // TODO: Make this configurable.
const DEFAULT_MAX_REDIRECTION = 5;

/**
 * Options for {@link lookupWebFinger}.
@@ -40,6 +40,13 @@ export interface LookupWebFingerOptions {
   */
  allowPrivateAddress?: boolean;

  /**
   * The maximum number of redirections to follow.
   *
   * Defaults to 5.
   */
  maxRedirection?: number;

  /**
   * The OpenTelemetry tracer provider.  If omitted, the global tracer provider
   * is used.
@@ -155,7 +162,8 @@ async function lookupWebFingerInternal(
      response.headers.has("Location")
    ) {
      redirected++;
      if (redirected >= MAX_REDIRECTION) {
      const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
      if (redirected >= maxRedirection) {
        logger.error(
          "Too many redirections ({redirections}) while fetching WebFinger " +
            "resource descriptor.",