Skip to content
go-slidesPublic

About

slides-gitd: the self-hosted git backend for the go-slides playground — forge credentials stay on the server, per-user isolation, behind oauth2-proxy. Pure Go.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

14 Commits

Folders and files

Repository files navigation

slides-gitd

The git backend for a self-hosted go-slides playground. It loads a deck's folder from a forge, proposes changes as a new branch and a pull (merge) request, and checks for upstream changes, while the forge credentials stay on the server. The browser holds only an HttpOnly session cookie.

It sits behind Caddy and oauth2-proxy (OIDC). For the public playground, which talks to GitHub and GitLab from the browser, see go-browserhttp/browsergit.

browser ──https──▶ Caddy ── forward_auth ──▶ oauth2-proxy ──▶ your IdP (OIDC)
                     │  X-Auth-Request-User (sub), X-Gitd-Proxy-Secret
                     └──────────────▶ slides-gitd 127.0.0.1:8091 ──▶ GitHub / GitLab / Gitea

API

GET /api/git/session Creates or returns this user's session. Sets __Host-gitd and returns the CSRF token and the repositories the user may use.
POST /api/git/credential Stores the user's own token for a user-token forge, sealed. It is never sent back.
POST /api/git/credential/delete Forgets it.
GET /api/git/credential/page?forge=<name> A page of this server, for a user-token forge the user is granted: a masked token field (Store) and Forget. The playground opens it in a new tab (noopener) so the token is typed where no script of the playground can read it. Any other forge: 404, no body. See below.
POST /api/git/credential/page The page's form: application/x-www-form-urlencoded, forge, action (store or forget), csrf, token. Answers a small result page.
POST /api/git/load {forge, repo, ref, dir} → {files, base}
POST /api/git/propose {forge, base, files, title, body} → {branch, url}. The server names the branch: slides/<user>-<hash>/<title>-<time>.
POST /api/git/refresh {forge, base} → {files, base} (files is null when nothing moved).

Security

Each point has a test that fails if it is broken.

  • Identity comes from the proxy only. The request must come from a trusted address (loopback by default) and carry X-Gitd-Proxy-Secret when it is configured. The subject is X-Auth-Request-User, which oauth2-proxy fills with the OIDC sub by default (providers/provider_data.go, UserClaim). With no identity the answer is 401, so an expired IdP session reaches nothing.

  • User A can never use user B's session or token.

    • A session is bound to one subject. Another subject's cookie is refused, and the session is destroyed.
    • Session ids come from the server (32 random bytes), so a client-chosen id is replaced, never adopted.
    • The CSRF token is per session and compared in constant time.
    • Tests cover a cookie swap, a mixed cookie and CSRF token, a guessed id, fixation, a change of identity behind the browser, and 200 concurrent requests from two users, each of which must use its own credential.
  • CSRF: every mutating call needs:

    • POST with the exact configured Origin;
    • X-Requested-With: slides-gitd;
    • a JSON body;
    • the session cookie (Secure, HttpOnly, SameSite=Strict, __Host-);
    • the X-CSRF-Token header.
  • The credential page is isolated from the page that opens it. Both are on one origin, so the isolation is in what the page says, and each header has a test:

    • Cross-Origin-Opener-Policy: same-origin, so an opener has no reference;
    • Content-Security-Policy: default-src 'none'; style-src 'nonce-…'; form-action 'self'; frame-ancestors 'none'; base-uri 'none': no script runs on it; plus X-Frame-Options: DENY, Cache-Control: no-store, Referrer-Policy: no-referrer (the form itself says same-origin: a browser posts Origin: null under no-referrer, and the form checks the exact Origin);
    • a fetch (Sec-Fetch-Mode other than navigate) is answered 404, so a script cannot read the form out of the HTML;
    • the form carries its own token, apart from X-CSRF-Token: random, bound to the subject, the session, the forge and the action, single-use, valid five minutes, at most 16 per subject;
    • the submission needs the exact Origin, the content type application/x-www-form-urlencoded, the session cookie and that token;
    • the token goes through the same code as POST /api/git/credential: same grant check, bounds and audit action. It is not echoed, not in a URL, not in the audit.

    The front must pass this route through as it does the rest of /api/git/* and must not add or replace Content-Security-Policy, or the Cross-Origin-Opener-Policy, X-Frame-Options and Referrer-Policy headers for it (the Caddy example below does not).

  • Deny by default. A subject reaches only the forge:owner/name entries the configuration grants it.

  • Writes only to slides/<user>-<hash>/*. <user> is the configured slug or one derived from the subject, and <hash> is 10 hex digits of SHA-256(subject), so no two subjects share a directory. Startup refuses duplicate configured slugs and coinciding directories. The branch name is the server's, and a request field naming a branch is an unknown field, refused. The forge adapter enforces the prefix again and never writes to an existing or default branch.

  • Credentials are encrypted at rest with one key per user.

    • Each record is AES-256-GCM. Its key is derived with HKDF-SHA256 from the master key and the subject, and its additional data binds it to the subject and the forge.
    • A record copied to another user, renamed to another forge, or read with another master key does not open.
    • Directory names are hashes; files are 0600 and directories 0700. Writes are atomic.
  • GitHub uses an App. Its installation token is minted for the one repository of the call, with contents:write, pull_requests:write and metadata:read only. It lives an hour and is cached until five minutes before expiry.

  • Secrets come from files only. These are the master key, the App's private key and the proxy secret. Each file must be readable by its owner only. No flag takes a secret, nothing is read from the environment, and a secret pasted into the configuration is an unknown field.

  • Limits: a per-user token bucket (429), a bound on the request body, and timeouts on the HTTP server.

  • Audit: one JSON line per call (who, what, where, the outcome as a class). It never holds file content, a token, a cookie or an error's text.

  • Fuzzed: every request body. Chaos tests cover a client killed in the middle of a proposal (the proposal is canceled and audited, and the next one works), concurrent users, and concurrent writers to the vault.

  • govulncheck runs on every push and daily.

Configuration

{
  "listen": "127.0.0.1:8091",
  "origin": "https://slides.example.org",
  "proxy_secret_file": "/etc/slides-gitd/proxy.secret",
  "master_key_file": "/etc/slides-gitd/master.key",
  "data_dir": "/var/lib/slides-gitd",
  "audit_file": "/var/log/slides-gitd/audit.log",
  "forges": {
    "github": {"kind": "github", "api": "https://api.github.com", "auth": "github-app",
               "app_client_id": "Iv1.…", "installation_id": 12345678,
               "app_key_file": "/etc/slides-gitd/github-app.pem"},
    "gitlab": {"kind": "gitlab", "api": "https://gitlab.example.org/api/v4", "auth": "user-token"}
  },
  "users": {
    "3f2b…-oidc-sub": {"slug": "ada", "repos": ["github:lab/talks", "gitlab:team/slides"]}
  }
}

Create the master key with head -c 32 /dev/urandom > master.key && chmod 600 master.key. Then run slides-gitd check -c config.json, which loads every secret file and exits, followed by slides-gitd serve -c config.json.

Caddy and oauth2-proxy

slides.example.org {
    handle /oauth2/* {
        reverse_proxy 127.0.0.1:4180
    }
    handle /api/git/* {
        route {
            # never let a client choose its identity, whatever Caddy's version does
            request_header -X-Auth-Request-User
            request_header -X-Auth-Request-Email
            request_header -X-Gitd-Proxy-Secret
            forward_auth 127.0.0.1:4180 {
                uri /oauth2/auth
                copy_headers X-Auth-Request-User X-Auth-Request-Email
            }
            request_body {
                max_size 64MB
            }
            reverse_proxy 127.0.0.1:8091 {
                header_up X-Gitd-Proxy-Secret {file./etc/caddy/gitd-proxy.secret}
            }
        }
    }
    handle {
        root * /srv/slides
        file_server
    }
}

Run oauth2-proxy with --set-xauthrequest and your IdP.

  • route { } keeps the order written. Without it, Caddy sorts the directives of a handle by its own order, where forward_auth comes before request_header: the identity forward_auth copied would be deleted again and every call would be a 401. Do not drop the route, and do not drop the request_header -… lines to "fix" a 401: that would let a client choose its identity.
  • The client's own identity headers are deleted before forward_auth runs. Current Caddy also deletes a client-supplied header before copy_headers (forwardauth/caddyfile.go), but older versions did not, so the explicit request_header -… lines stay.
  • CI runs this exact example (extracted from this file) through Caddy v2.11.7 with a fake oauth2-proxy and a fake slides-gitd (caddytest/).
  • {file.*} reads the proxy secret from a file (Caddy placeholders), so it is not in an environment variable.

Registering the forges (needs your accounts)

  • GitHub App: go to Settings ▸ Developer settings ▸ GitHub Apps ▸ New.
    • Permissions: Contents read & write, Pull requests read & write, Metadata read. No webhook.
    • Install it on the organisation, on the deck repositories only.
    • Put the client ID and the installation ID in the configuration, and the private key in app_key_file (chmod 600).
  • GitLab: each user stores a personal or project access token with scope api. GitLab's REST API only accepts api for writes. write_repository "does not support API authentication" (it covers git over HTTP only, and GitLab serves git over HTTP without CORS). api grants "complete read and write access to the API" for the token's user, so a stolen token can do anything that user can.
    • Prefer a project or group access token scoped to the deck repositories (they carry api without the Git-over-HTTP clause and only for that project or group).
    • Otherwise use a dedicated GitLab account that is a member of those projects only.
    • Source: GitLab, access token scopes.

Testing what sits in front of it

Package gitdtest is the real server (sessions, CSRF, grants, the sealed vault, the audit) over an in-memory forge, for the tests of a playground or a proxy. A repository there has an owner token and answers only the token the vault hands over, so a server that mixed two users' credentials fails a test. It is for tests only: mount Env.Handler behind a stand-in for oauth2-proxy that sets X-Auth-Request-User, as the go-slides playground's browser suite does.

Licence

BSD-3-Clause.

About

slides-gitd: the self-hosted git backend for the go-slides playground — forge credentials stay on the server, per-user isolation, behind oauth2-proxy. Pure Go.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages