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

The operator guard treats a trailing-dot hostname as the host it names #14976

Merged opened this

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

Merged

scout/forge:scout/14954-the-operator-guard-treats-a-trailing-dot → main 1 commit fork

  • Merged. scout/14954-the-operator-guard-treats-a-trailing-dot was merged into main as 30549c8.

Review

Sign in to review this change.
2 files changed +104 −3
src/origins/operator.ts +13 −3
@@ -19,11 +19,21 @@ import type { Provider, RemoteIdentity, RemoteRepo } from "./types.ts";
1919
2020 type Env = Record<string, string | undefined>;
2121
22 /** `git.example` from `https://git.example:8443/api/v1`: lower-case, no port. */
22 /**
23 * A hostname as the guard compares it: lower-case, one trailing dot removed.
24 * `git.example.` is the fully qualified spelling of `git.example` and
25 * reaches the same server, so it must meet the same guard (#14954).
26 */
27 export function normalHost(host: string): string {
28 const lower = host.trim().toLowerCase();
29 return lower.endsWith(".") ? lower.slice(0, -1) : lower;
30 }
31
32 /** `git.example` from `https://git.example.:8443/api/v1`: normalized, no port. */
2333 export function hostnameOf(url: string): string {
2434 // A bare `host:port` parses as a URL whose scheme is the host.
2535 const parsed = URL.parse(url.includes("://") ? url : `https://${url}`);
26 return parsed === null ? "" : parsed.hostname.toLowerCase();
36 return parsed === null ? "" : normalHost(parsed.hostname);
2737 }
2838
2939 /**
@@ -35,7 +45,7 @@ export function operatorHosts(env: Env = process.env): string[] {
3545 for (const entry of (env.FORGE_ORIGIN_CLIENT_ID ?? "").split(",")) {
3646 const at = entry.indexOf("=");
3747 if (at === -1) continue;
38 const host = entry.slice(0, at).trim().toLowerCase();
48 const host = normalHost(entry.slice(0, at));
3949 if (host !== "" && entry.slice(at + 1).trim() !== "" && !out.includes(host))
4050 out.push(host);
4151 }
test/origin-guard-trailing-dot.test.ts added +91
@@ -0,0 +1,91 @@
1 /**
2 * The operator guard (`origins/operator.ts`) names hosts the way DNS does:
3 * `git.example.` is `git.example`, and resolves to the same server. An
4 * origin connected under the trailing-dot spelling must be guarded exactly
5 * as the plain one — or an account that administers the instance, or can
6 * push to a protected repository, walks through (#14954, from #14771).
7 */
8
9 import { describe, expect, test } from "bun:test";
10 import {
11 checkCredential,
12 denied,
13 denyPushRules,
14 guarded,
15 hostnameOf,
16 operatorHosts,
17 } from "../src/origins/operator.ts";
18 import type { Provider, RemoteIdentity } from "../src/origins/types.ts";
19
20 const dotted = {
21 apiUrl: "https://GIT.example./api/v1",
22 webUrl: "https://git.example.",
23 };
24
25 /** A provider that answers every lookup with push rights. */
26 function pushEverywhere(origin: { apiUrl: string; webUrl: string }): Provider {
27 const repo = (fullName: string) => ({
28 fullName,
29 permissions: { push: true, admin: false, pull: true },
30 });
31 return {
32 kind: "forgejo",
33 ...origin,
34 getRepo: async (name: string) => ({ ok: true, value: repo(name) }),
35 walkRepos: async () => ({
36 ok: true,
37 value: { repos: [repo("system/core")], complete: true },
38 }),
39 } as unknown as Provider;
40 }
41
42 const person = { login: "ana", siteAdmin: false } as RemoteIdentity;
43 const admin = { login: "root", siteAdmin: true } as RemoteIdentity;
44
45 describe("a trailing-dot hostname", () => {
46 test("is the host it names, lower-cased, one dot removed", () => {
47 expect(hostnameOf("https://Git.Example./api/v1")).toBe("git.example");
48 expect(hostnameOf("git.example.:8443")).toBe("git.example");
49 expect(hostnameOf("https://git.example")).toBe("git.example");
50 });
51
52 test("in the operator's variables matches the plain one, and the other way", () => {
53 expect(operatorHosts({ FORGE_ORIGIN_CLIENT_ID: "git.example.=id" })).toEqual([
54 "git.example",
55 ]);
56 expect(
57 denyPushRules({ FORGE_ORIGIN_DENY_PUSH: "git.example.:system/*" })[0]!.host,
58 ).toBe("git.example");
59 const plain = { apiUrl: "https://git.example/api/v1", webUrl: "https://git.example" };
60 expect(guarded(plain, { FORGE_ORIGIN_CLIENT_ID: "git.example.=id" })).toBe(true);
61 });
62
63 test("is guarded where the plain host is", () => {
64 expect(guarded(dotted, { FORGE_ORIGIN_CLIENT_ID: "git.example=id" })).toBe(true);
65 expect(guarded(dotted, { FORGE_ORIGIN_DENY_PUSH: "git.example:system/*" })).toBe(
66 true,
67 );
68 });
69
70 test("does not unprotect a repository a host rule protects", () => {
71 const env = { FORGE_ORIGIN_DENY_PUSH: "git.example:system/*" };
72 expect(denied(dotted, "system/core", env)).toBe(true);
73 expect(denied(dotted, "acme/app", env)).toBe(false);
74 });
75
76 test("does not let an administrator through", async () => {
77 const found = await checkCredential(pushEverywhere(dotted), admin, {
78 FORGE_ORIGIN_CLIENT_ID: "git.example=id",
79 });
80 expect(found.ok ? "passed" : found.error.error).toBe("admin_account");
81 });
82
83 test("does not let an account that can push to a protected repository through", async () => {
84 for (const rule of ["git.example:system/core", "git.example:system/*"]) {
85 const found = await checkCredential(pushEverywhere(dotted), person, {
86 FORGE_ORIGIN_DENY_PUSH: rule,
87 });
88 expect(found.ok ? "passed" : found.error.error).toBe("can_push");
89 }
90 });
91 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs