Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

relay: accept IPv6 origins in RELAY_PHONE_ORIGINS; test the default refusal on a running relay

Bugfixer 222cd4cc15b9a
diff --git a/relay/src/config.ts b/relay/src/config.ts
index 06d901c682..757926f4d8 100644
--- a/relay/src/config.ts
+++ b/relay/src/config.ts
@@ -91,7 +91,8 @@ const MIN_METRICS_TOKEN = 32;
const MAX_PHONE_ORIGINS = 8;
const PHONE_ORIGINS_RULE = `RELAY_PHONE_ORIGINS must list exact https:// origins, comma-separated, at most ${MAX_PHONE_ORIGINS}`;
-const HOSTNAME = /^[a-z0-9-]+(\.[a-z0-9-]+)*$/;
+/** A DNS name or IPv4 address, or a bracketed IPv6 address, as the URL parser serializes them. */
+const HOSTNAME = /^([a-z0-9-]+(\.[a-z0-9-]+)*|\[[0-9a-f:]+\])$/;
/** Exactly what a browser sends as Origin: `https://host[:port]`, lower case, no default port, path or credentials. No wildcards. */
function isExactHttpsOrigin(value: string): boolean {
diff --git a/relay/test/origins.test.ts b/relay/test/origins.test.ts
index 76b36edad0..878e242061 100644
--- a/relay/test/origins.test.ts
+++ b/relay/test/origins.test.ts
@@ -18,6 +18,11 @@ describe("RELAY_PHONE_ORIGINS", () => {
assert.deepEqual([...phoneOrigins], [APP, "https://web.example.org:8443"]);
});
+ test("accepts IP literal hosts as browsers send them", () => {
+ const value = "https://[::1]:8443,https://[fd00::a:1],https://192.168.1.20:8443";
+ assert.deepEqual([...loadConfig({ ...base, RELAY_PHONE_ORIGINS: value }).phoneOrigins], value.split(","));
+ });
+
test("refuses anything that is not an exact https:// origin, and never repeats it", () => {
const bad = [
"http://app.example.com",
@@ -29,6 +34,9 @@ describe("RELAY_PHONE_ORIGINS", () => {
"https://App.example.com",
"https://app.example.com:443",
"https://*.example.com",
+ "https://*",
+ "https://[0:0::1]",
+ "https://[::1]:443",
"app.example.com",
"null",
`${APP},ftp://files.example.com`,
@@ -49,6 +57,19 @@ describe("RELAY_PHONE_ORIGINS", () => {
});
});
+describe("a relay without RELAY_PHONE_ORIGINS", () => {
+ let relay: TestRelay;
+ before(async () => {
+ relay = await startTestRelay();
+ });
+ after(() => relay.stop());
+
+ test("refuses every browser page on /v1/phone", async () => {
+ assert.equal(await Client.refusal(relay.url, "/v1/phone", { headers: { origin: APP } }), 403);
+ assert.equal(await Client.refusal(relay.url, "/v1/phone"), 101);
+ });
+});
+
describe("a relay with RELAY_PHONE_ORIGINS", () => {
let relay: TestRelay;
before(async () => {