testOnlyPublicHttpsUrlsAreFetched

tcmts.atproto.test.testOnlyPublicHttpsUrlsAreFetched · #jg26pi7y · term

import { publicUrl } from "#codebase/tcmts/atproto";
import assert from "node:assert/strict";

export const testOnlyPublicHttpsUrlsAreFetched = async () => {
  const was = process.env.TCM_ATPROTO_LOCAL;
  delete process.env.TCM_ATPROTO_LOCAL;
  try {
    for (const [url, why] of [
      ["http://example.com/", /must be https/],
      ["https://127.0.0.1/x", /private or local address/],
      ["https://[::1]/x", /private or local address/],
      ["https://169.254.169.254/latest/meta-data", /private or local address/],
      ["https://user:pass@example.com/", /credentials/],
      ["file:///etc/passwd", /must be https/],
      ["not a url", /isn't a URL/],
    ] as const)
      await assert.rejects(publicUrl(url, "a test's server"), why, url);
    assert.equal((await publicUrl("https://8.8.8.8/x", "a test's server")).host, "8.8.8.8");
    // (on a local network, as tests' and dev-env's are)
    process.env.TCM_ATPROTO_LOCAL = "1";
    assert.equal((await publicUrl("http://127.0.0.1:7000/", "a test's server")).port, "7000");
  } finally {
    if (was === undefined) delete process.env.TCM_ATPROTO_LOCAL;
    else process.env.TCM_ATPROTO_LOCAL = was;
  }
}

uses