Public: anyone can read this; the crew can change it.

A device address written as IPv4-mapped IPv6 is refused like the IPv4 address it is #15050

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15036-a-device-address-written-as-ipv4-mapped → main 1 commit fork

  • Merged. scout/15036-a-device-address-written-as-ipv4-mapped was merged into main as 04f3e16.

Review

Sign in to review this change.
2 files changed +51 −3
src/core/devices.ts +10 −3
@@ -9,6 +9,7 @@
99
1010 import type { Database } from "bun:sqlite";
1111 import { isPrivateHost } from "../actions/migrate.ts";
12 import { addressBytes } from "../kernel/cidr.ts";
1213 import { err, ok, type Result } from "../kernel/result.ts";
1314 import type { PrinterReport } from "../devices/ipp.ts";
1415
@@ -51,9 +52,15 @@ export function validateDeviceUrl(raw: string): Result<string, DeviceError> {
5152 const host = u.hostname; // IPv6 keeps its brackets
5253 const literal = /^\d{1,3}(\.\d{1,3}){3}$/.test(host) || (host.startsWith("[") && host.endsWith("]"));
5354 if (!literal) return err("public_address"); // a name is never looked up
54 if (isNeverLiteral(host)) return err("public_address");
55 if (isLoopbackLiteral(host)) return allowLoopback() ? ok(u.toString()) : err("public_address");
56 if (!isPrivateHost(host)) return err("public_address");
55 const bytes = addressBytes(host);
56 if (bytes === undefined) return err("public_address");
57 // Judged by its bytes, never its spelling (#15036): `[::ffff:169.254.169.254]`
58 // (which the URL parser writes `[::ffff:a9fe:a9fe]`) is the IPv4 address it
59 // carries, so it meets the same refusals as `169.254.169.254`.
60 const judged = bytes.length === 4 ? bytes.join(".") : host;
61 if (isNeverLiteral(judged)) return err("public_address");
62 if (isLoopbackLiteral(judged)) return allowLoopback() ? ok(u.toString()) : err("public_address");
63 if (!isPrivateHost(judged)) return err("public_address");
5764 return ok(u.toString());
5865 }
5966
test/devices.test.ts +41
@@ -209,6 +209,47 @@ test("a second view inside 30 seconds asks the printer nothing", async () => {
209209 expect(text).toContain("Fresh");
210210 });
211211
212 /** Runs `fn` with the loopback switch at `value`, putting it back after. */
213 function withLoopback(value: string | undefined, fn: () => void): void {
214 const before = process.env.FORGE_DEVICES_ALLOW_LOOPBACK;
215 if (value === undefined) delete process.env.FORGE_DEVICES_ALLOW_LOOPBACK;
216 else process.env.FORGE_DEVICES_ALLOW_LOOPBACK = value;
217 try {
218 fn();
219 } finally {
220 if (before === undefined) delete process.env.FORGE_DEVICES_ALLOW_LOOPBACK;
221 else process.env.FORGE_DEVICES_ALLOW_LOOPBACK = before;
222 }
223 }
224
225 test("an IPv4 address written as IPv4-mapped IPv6 is refused like the IPv4 address it is", () => {
226 withLoopback(undefined, () => {
227 for (const host of [
228 "[::ffff:169.254.169.254]",
229 "[::ffff:a9fe:a9fe]",
230 "[0:0:0:0:0:ffff:a9fe:a9fe]",
231 "[::ffff:0.0.0.0]",
232 "[::ffff:0:0]",
233 "[::ffff:127.0.0.1]",
234 "[::ffff:7f00:1]",
235 "[0:0:0:0:0:ffff:7f00:1]",
236 ])
237 expect(validateDeviceUrl(`ipp://${host}/ipp/print`)).toEqual({ ok: false, error: "public_address" });
238 });
239 });
240
241 test("an IPv4-mapped loopback printer is let in only where plain loopback is", () => {
242 withLoopback("1", () => {
243 expect(validateDeviceUrl("ipp://[::ffff:127.0.0.1]:631/ipp/print").ok).toBe(true);
244 expect(validateDeviceUrl("ipp://[::ffff:169.254.169.254]/ipp/print").ok).toBe(false);
245 expect(validateDeviceUrl("ipp://[::ffff:0:0]/ipp/print").ok).toBe(false);
246 });
247 });
248
249 test("an IPv4-mapped private-network printer is still accepted", () => {
250 expect(validateDeviceUrl("ipp://[::ffff:192.168.1.161]:631/ipp/print").ok).toBe(true);
251 });
252
212253 test("link-local (cloud metadata) and the unspecified address are never accepted", () => {
213254 for (const bad of ["ipp://169.254.169.254/ipp/print", "http://169.254.1.1/x", "ipp://0.0.0.0/ipp/print"])
214255 expect(validateDeviceUrl(bad)).toEqual({ ok: false, error: "public_address" });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs