Dough Git

commit

oop

commit
f8de98e7d0b54f17fa62b14022b40e3ccd611133
author
Clove Twilight <[email protected]>
date
2026-09-03 21:38

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2a3205d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,154 @@
+# dough-git
+
+A minimal self-hosted git forge: HTTP clone/pull/push, a cgit-style read-only
+viewer, per-user access tokens, and PocketID (OIDC) for browser login. About
+6k lines of TypeScript, four runtime dependencies, one SQLite file, and a
+directory of bare repositories that is the whole of your state.
+
+It is a **mirror and a backup target** first. There are no issues, no pull
+requests and no CI — the things it does have are the things a backup of your
+work actually needs: it stores every ref you push, it tells you when a copy on
+GitHub or Codeberg has drifted, and it does not lose a repository you delete by
+accident.
+
+## Quick start
+
+```sh
+cp .env.example .env      # fill in OIDC_* and SESSION_SECRET at minimum
+docker compose up -d
+```
+
+`compose.yml` binds to `127.0.0.1:4010` and expects a reverse proxy in front of
+it for TLS. Your bare repositories live in `./data/repos`; back that directory
+up and you have backed up the forge.
+
+Without Docker:
+
+```sh
+npm ci
+npm run build
+npm start
+```
+
+Node 22+ is required — the token store uses the built-in `node:sqlite`, so
+there is no native module to compile.
+
+| Command | What it does |
+| --- | --- |
+| `npm run build` | Compiles the Vanilla Extract styles and bundles the server to `dist/` |
+| `npm start` | Runs `dist/server.js` |
+| `npm run dev` | Build, then start |
+| `npm run typecheck` | `tsc --noEmit` |
+| `npm test` | Runs every file in `test/` |
+
+## How it is put together
+
+The single most useful thing to know is where the line between the filesystem
+and the database falls, because everything else follows from it.
+
+**The filesystem is authoritative for what exists and who may see it.** A
+repository is a directory, `<repos>/<owner>/<name>.git`. It is public if it
+contains a marker file (`minigit-public` by default) and private if it does
+not. A deleted repository is a directory that has been moved to
+`<repos>/.trash/`. You can inspect, back up, and repair all of this with `ls`
+and `mv`.
+
+**SQLite holds identity, grants, and caches.** The user directory (OIDC `sub`
+to a stable owner slug), token hashes, collaborator grants, per-account
+settings, and the mirror-status cache. Every one of these describes a
+repository the filesystem already says exists — none of them can conjure one.
+When a repository goes away its rows go with it, through the single function in
+`src/repometa.ts`.
+
+| File | Responsibility |
+| --- | --- |
+| `server.ts` | Routes, middleware, authorization decisions |
+| `git.ts` | Every shell-out to git; repo names, trash, refs, trees, blobs |
+| `smart-http.ts` | The git transport (`upload-pack` / `receive-pack`) |
+| `auth.ts` | OIDC login, signed session cookies, Basic-auth for git |
+| `access.ts` | Who may read and write a repository |
+| `urls.ts` | Validation of every URL that reaches git or `fetch()` |
+| `markdown.ts` | An escape-first README renderer |
+| `views.ts` | HTML |
+| `styles/` | Vanilla Extract, compiled to one static stylesheet at build time |
+
+## Authentication
+
+Browser login is PocketID over OIDC with PKCE. The resulting session is a
+stateless HMAC-signed cookie — there is no server-side session store — and the
+account is recorded in the user directory so its **owner slug** is stable and
+unique. That slug is the identity everything else is keyed by.
+
+Git over HTTP uses Basic auth, where the password is a token minted in the
+browser at `/settings/tokens` and the username is the slug that token belongs
+to. Only hashes are stored; the plaintext is shown once. There is no
+instance-wide credential: a token is always somebody in particular.
+
+```sh
+git clone https://<slug>:<token>@git.example.com/<owner>/<repo>.git
+```
+
+A token may push to repositories under its own slug, and to any repository
+where its account was granted `write` as a collaborator. Pushing to a name that
+does not exist yet creates it, unless `MINIGIT_AUTO_CREATE=false`.
+
+## Profile READMEs and the `+` namespace
+
+A repository named **`+dough`** is special: its `README.md` is rendered on its
+owner's profile page at `/<owner>`. It is an ordinary repository in every other
+respect — clone it, push to it, make it private, delete it — it is simply
+hidden from repository listings, because it is the page you are already
+looking at.
+
+```sh
+git clone https://git.example.com/clove/+dough.git
+cd +dough && $EDITOR README.md
+git commit -am "profile" && git push
+```
+
+Or press the button on your own profile page, which creates it for you.
+
+The leading `+` is a reserved namespace. Every `+` name is refused as a
+repository except that one, which buys two things: route segments like
+`/<owner>/+repos` (the full repository list) can never collide with a real
+repository, and the profile repo cannot be squatted or created by accident. The
+rule lives in `safeName()` in `src/git.ts`; the name itself is the
+`PROFILE_REPO` constant beside it.
+
+## Security notes
+
+The recurring approach here is an allow-list rather than a sanitiser, so that
+the unsafe cases are the ones that were never constructed:
+
+- **README rendering** escapes every character *before* producing any markup and
+  parks generated HTML in a stash. Raw HTML in the source is dropped, not
+  filtered, and link schemes are allow-listed. Injection is prevented by
+  construction rather than by a sanitiser keeping step with a parser.
+- **Outbound URLs** (mirror links, the Discord webhook) are checked against a
+  fixed list of exact hosts, and the stored URL is rebuilt from validated parts
+  rather than passed through. A mirror URL reaches the git binary as a remote,
+  where `ext::` would run a shell command; a webhook URL reaches `fetch()`,
+  where an arbitrary host is an SSRF probe.
+- **Outbound git** runs with `protocol.allow=never` (plus https), no credential
+  helper, no redirects, an empty config environment, a timeout and an output
+  cap.
+- **Repository names** are validated in one place, and `..`, dotfiles and path
+  separators are refused. The trash root, the static handler and the repo root
+  each re-check that a resolved path stayed inside them.
+- Sessions are signed and compared in constant time. CSP, CSRF same-origin
+  checks, `nosniff`, `X-Frame-Options` and HSTS are applied to everything except
+  the git transport, which is authenticated by token instead.
+
+`git ls-remote` against a mirror is a **monitor, not a synchroniser**: nothing
+in this application pushes to, fetches from, creates or deletes anything on a
+remote.
+
+## Configuration
+
+Everything is environment variables, read once at startup by `src/config.ts`.
+See [`.env.example`](.env.example), which documents each one. The values you
+must set are `MINIGIT_BASE_URL`, `SESSION_SECRET`, and the `OIDC_*` group.
+
+## Licence
+
+[Doughmination Authorised Source Licence 1.1](LICENCE.md).
diff --git a/test/profile.test.mjs b/test/profile.test.mjs
new file mode 100644
index 0000000..fad5168
--- /dev/null
+++ b/test/profile.test.mjs
@@ -0,0 +1,166 @@
+/* test/profile.test.mjs
+ *
+ * Tests for the "+" namespace and the profile README that rides on it.
+ *
+ * The load-bearing case is the first group. A leading "+" is what keeps the
+ * route /<owner>/+repos from ever colliding with a repository, so the rule
+ * "every + name is refused except the one profile repo" is a routing invariant
+ * and not a cosmetic one: the moment safeRef accepts +repos, a repository can
+ * shadow that page. The rest checks that a name the forge does accept survives
+ * everything a normal repository survives — listing, trash, restore.
+ *
+ * Run:  node --experimental-strip-types test/profile.test.mjs
+ */
+
+import { execFile } from "node:child_process";
+import { promisify } from "node:util";
+import { mkdtempSync, rmSync, mkdirSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const root = mkdtempSync(join(tmpdir(), "dough-profile-"));
+process.env.MINIGIT_REPOS_ROOT = root;
+process.env.MINIGIT_DB_PATH = join(root, "test.db");
+
+const {
+  PROFILE_REPO,
+  PROFILE_REPOS_PATH,
+  isProfileRepo,
+  safeRef,
+  refDir,
+  headReadme,
+  listRepos,
+  createRepo,
+  trashRepo,
+  listTrash,
+  restoreFromTrash,
+  repoExists,
+} = await import("../src/git.ts");
+
+const exec = promisify(execFile);
+
+let failures = 0;
+function check(label, cond) {
+  console.log(`${cond ? "PASS" : "FAIL"}  ${label}`);
+  if (!cond) failures++;
+}
+
+async function git(cwd, args) {
+  const { stdout } = await exec("git", args, {
+    cwd,
+    encoding: "utf8",
+    timeout: 20000,
+    env: {
+      ...process.env,
+      GIT_AUTHOR_NAME: "t",
+      GIT_AUTHOR_EMAIL: "t@t",
+      GIT_COMMITTER_NAME: "t",
+      GIT_COMMITTER_EMAIL: "t@t",
+    },
+  });
+  return stdout;
+}
+
+// -- the "+" namespace --------------------------------------------------------
+
+console.log("-- the + namespace is reserved, with exactly one exception --");
+
+check("the profile repo is accepted", safeRef("clove", PROFILE_REPO) !== null);
+check("...and recognised", isProfileRepo(PROFILE_REPO));
+check("...and only it", !isProfileRepo("dough") && !isProfileRepo("+other"));
+
+check(
+  "the repos route name is NOT a usable repository",
+  safeRef("clove", PROFILE_REPOS_PATH) === null,
+);
+for (const name of ["+bogus", "+", "++dough", "+dough2", "+DOUGH"]) {
+  check(`refuses ${name}`, safeRef("clove", name) === null);
+}
+check("refuses + inside a name", safeRef("clove", "a+b") === null);
+check("refuses + at the end", safeRef("clove", "dough+") === null);
+
+check(
+  "the profile repo survives a .git suffix",
+  safeRef("clove", `${PROFILE_REPO}.git`)?.name === PROFILE_REPO,
+);
+check("an owner may not use the + prefix", safeRef(PROFILE_REPO, "x") === null);
+
+// The pre-existing guarantees have to still hold.
+check("still refuses traversal", safeRef("clove", "../etc") === null);
+check("still refuses a dotfile name", safeRef("clove", ".ssh") === null);
+check("still accepts an ordinary name", safeRef("clove", "my-repo.v2") !== null);
+
+// -- headReadme ---------------------------------------------------------------
+
+console.log("\n-- the profile readme is read at HEAD --");
+
+const created = await createRepo("clove", PROFILE_REPO);
+check("the profile repo can be created", created.ok === true);
+
+const ref = { owner: "clove", name: PROFILE_REPO };
+check("an empty profile repo has no readme", (await headReadme(ref)) === null);
+
+const work = join(root, "work");
+mkdirSync(work);
+await git(work, ["init", "-q", "-b", "main", "."]);
+writeFileSync(join(work, "README.md"), "# hello\n\nprofile text.\n");
+await git(work, ["add", "-A"]);
+await git(work, ["commit", "-qm", "readme"]);
+await git(work, ["push", "-q", refDir(ref), "main"]);
+
+const found = await headReadme(ref);
+check("the readme is found at HEAD", found?.path === "README.md");
+check("...with its text", found?.text.includes("profile text."));
+
+// A repo with commits but no README is the case the profile page reports
+// differently from a missing repo, so it must come back as null, not throw.
+const plain = await createRepo("clove", "plain");
+check("a second repo is created", plain.ok === true);
+await git(work, ["push", "-q", refDir({ owner: "clove", name: "plain" }), "main:main"]);
+await git(work, ["rm", "-q", "README.md"]);
+await git(work, ["commit", "-qm", "drop readme"]);
+await git(work, ["push", "-qf", refDir({ owner: "clove", name: "plain" }), "main"]);
+check(
+  "a repo with commits but no readme is null",
+  (await headReadme({ owner: "clove", name: "plain" })) === null,
+);
+
+// -- listing ------------------------------------------------------------------
+
+console.log("\n-- listing ignores names the forge would refuse --");
+
+// A directory can be created by hand; listRepos must not surface one whose
+// name safeRef would reject, or the UI would link to a page that 404s.
+mkdirSync(join(root, "clove", "+bogus.git"), { recursive: true });
+const listed = (await listRepos()).map((r) => r.name).sort();
+check("the profile repo is listed by git.ts", listed.includes(PROFILE_REPO));
+check("a stray + directory is not", !listed.includes("+bogus"));
+check("ordinary repos are", listed.includes("plain"));
+
+// -- trash round-trip ---------------------------------------------------------
+
+console.log("\n-- the profile repo trashes and restores like any other --");
+
+const trashed = await trashRepo(ref, {
+  deletedAt: Math.floor(Date.now() / 1000),
+  deletedBy: "clove",
+  grants: [],
+});
+check("it can be trashed", trashed.ok === true);
+check("...and is gone", (await repoExists(ref)) === false);
+
+const inTrash = await listTrash("clove");
+check("...and appears in the trash", inTrash.some((e) => e.name === PROFILE_REPO));
+check(
+  "...with its name intact, not degraded",
+  inTrash.find((e) => e.name === PROFILE_REPO)?.degraded === false,
+);
+
+const restored = await restoreFromTrash("clove", trashed.entry);
+check("it can be restored", restored.ok === true);
+check("...and is back", (await repoExists(ref)) === true);
+check("...with its readme", (await headReadme(ref))?.path === "README.md");
+
+rmSync(root, { recursive: true, force: true });
+console.log(failures ? `\n${failures} FAILED` : "\nALL PASSED");
+process.exit(failures ? 1 : 0);