diff --git a/.github/workflows/sso-image.yml b/.github/workflows/sso-image.yml index 289db4b5c6..bb4438e3b7 100644 --- a/.github/workflows/sso-image.yml +++ b/.github/workflows/sso-image.yml @@ -37,27 +37,33 @@ jobs: runs-on: ${{ matrix.runner }} steps: - name: Checkout fork (with sso/ overlay) - uses: actions/checkout@v7 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 - name: Resolve tag & version id: ver + # Inputs reach the script through env, never ${{ }} interpolation, + # so a crafted tag cannot inject shell. + env: + TAG: ${{ inputs.upstream_tag || '@hyperdx/app@2.42.0' }} run: | - TAG="${{ github.event.inputs.upstream_tag || '@hyperdx/app@2.42.0' }}" + [[ "$TAG" =~ ^@hyperdx/app@[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "invalid tag: $TAG"; exit 1; } VERSION="${TAG##*@}" # @hyperdx/app@2.42.0 -> 2.42.0 echo "tag=$TAG" >> "$GITHUB_OUTPUT" echo "version=$VERSION" >> "$GITHUB_OUTPUT" echo "Building $TAG (version $VERSION)" - name: Integrate SSO overlay onto upstream tag - run: ./sso/apply.sh "${{ steps.ver.outputs.tag }}" build + env: + TAG: ${{ steps.ver.outputs.tag }} + run: ./sso/apply.sh "$TAG" build - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v4 + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 - name: Log in to GHCR - uses: docker/login-action@v4 + uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0 with: registry: ghcr.io username: ${{ github.actor }} @@ -68,7 +74,7 @@ jobs: run: echo "name=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" - name: Build and push prod image - uses: docker/build-push-action@v7 + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 with: context: build file: build/docker/hyperdx/Dockerfile @@ -89,20 +95,22 @@ jobs: runs-on: ubuntu-26.04 steps: - name: Log in to GHCR - uses: docker/login-action@v4 + uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v4 + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 # Also tag -sso.: immutable, so IfNotPresent nodes never reuse a # stale cached image after a rebuild of the same upstream version. - name: Create multi-arch manifest + env: + TAG: ${{ inputs.upstream_tag || '@hyperdx/app@2.42.0' }} run: | - TAG="${{ github.event.inputs.upstream_tag || '@hyperdx/app@2.42.0' }}" + [[ "$TAG" =~ ^@hyperdx/app@[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "invalid tag: $TAG"; exit 1; } IMAGE="ghcr.io/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" SRC="${IMAGE}:${TAG##*@}-sso" docker buildx imagetools create \ diff --git a/sso/README.md b/sso/README.md index 2f25d02b48..b86c22d16b 100644 --- a/sso/README.md +++ b/sso/README.md @@ -30,7 +30,10 @@ Files: - `integrate.mjs` — copies the overlay and applies idempotent, anchor-based codemods to `auth.ts`, `api-app.ts` and `mcp/utils/registerTool.ts`. Fails loudly if upstream changed those files' shape. -- `apply.sh` — checks out an upstream tag into a git worktree and runs the codemod. +- `apply.sh` — checks out an upstream tag into a git worktree, cherry-picks the + upstream security commits listed in `UPSTREAM_PATCHES` that the tag lacks, and + runs the codemod. The tag is fetched from hyperdxio/hyperdx into + `refs/sso-upstream/`, so same-named tags in this fork are never built. ## Build locally @@ -104,6 +107,17 @@ Write decisions are logged (`Proxy auth: write`, `... write denied`, The UI is unchanged: non-admins still see edit buttons, but saving returns 403 naming the required role. +The guard also, while proxy auth is on: +- matches paths case-insensitively, as Express routes do; +- rejects browser writes from another site (`Sec-Fetch-Site`, else `Origin` + vs `FRONTEND_URL`), since the oauth2-proxy cookie alone authenticates; +- strips the team ingestion key (`apiKey`, `hookId`) from `GET /me` and + `GET /team` for non-admins; +- returns 404 for password login, registration and invite setup. + +`FRONTEND_URL` must be the public URL (`https://click.topia.io`): it drives the +`Origin` check and the session cookie's `secure` flag. + ### Access keys, API v2 and MCP `/api/v2/*` and `/mcp` authenticate by personal access key, not by the proxy, @@ -187,4 +201,6 @@ set tfvars → apply. 1. Pick the new `@hyperdx/app@` tag (match the ClickStack chart `appVersion`). 2. Run the workflow with that tag. If `integrate.mjs` errors on a missing anchor, upstream reshaped `isUserAuthenticated` — update the anchors in `integrate.mjs`. -3. Point the chart at the new image tag and upgrade chart + image together. +3. Drop entries from `UPSTREAM_PATCHES` in `apply.sh` once the tag contains + them (they are skipped automatically, so this is only tidying). +4. Point the chart at the new image tag and upgrade chart + image together. diff --git a/sso/apply.sh b/sso/apply.sh index f7114b749b..753d4d420a 100755 --- a/sso/apply.sh +++ b/sso/apply.sh @@ -2,37 +2,58 @@ # Produce an SSO-integrated HyperDX source tree at a chosen upstream tag. # # sso/apply.sh [out-dir] -# sso/apply.sh '@hyperdx/app@2.28.0' build +# sso/apply.sh '@hyperdx/app@2.42.0' build # -# Checks out the requested upstream tag into a clean git worktree, then runs -# the deterministic overlay+codemod. The result in is ready to feed -# to `docker build` (docker/hyperdx/Dockerfile, target prod). +# Checks out the requested upstream tag into a clean git worktree, applies the +# upstream security patches it lacks, then runs the deterministic +# overlay+codemod. The result in is ready to feed to `docker build` +# (docker/hyperdx/Dockerfile, target prod). set -euo pipefail TAG="${1:?usage: sso/apply.sh [out-dir]}" OUT="${2:-build}" +if [[ ! "$TAG" =~ ^@hyperdx/app@[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "[sso] invalid upstream tag: $TAG" >&2 + exit 1 +fi + +# Upstream commits not yet in a release, cherry-picked onto the tag. Each is +# skipped once the tag contains it, so this list only needs pruning, not care. +UPSTREAM_PATCHES=( + 947b0678f5647d0efcba174d2854329545beb8d0 # next 16.3.8 -> 16.4.1 (RCE advisories) +) + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel)" cd "$REPO_ROOT" UPSTREAM_URL="https://github.com/hyperdxio/hyperdx.git" -if ! git remote get-url upstream >/dev/null 2>&1; then - echo "[sso] adding 'upstream' remote -> $UPSTREAM_URL" - git remote add upstream "$UPSTREAM_URL" -fi +# Fetched into a private namespace: the fork's CI checkout (fetch-depth 0) +# carries every fork tag, including ones with upstream names (e.g. made by +# inherited release workflows). Fetching into refs/tags/ would either refuse to +# clobber them or overwrite them; a separate ref sidesteps both, and those fork +# tags are never what gets built. +REF="refs/sso-upstream/tags/${TAG}" -echo "[sso] fetching tag $TAG" -# Forced (+): a same-named tag may already exist locally (e.g. the CI checkout -# of the fork, fetch-depth 0, copies every tag the fork holds). Without + git -# refuses with "would clobber existing tag" and the build dies before the -# overlay is applied. Upstream is the source of truth for upstream tags. -git fetch --no-tags upstream "+refs/tags/${TAG}:refs/tags/${TAG}" +echo "[sso] fetching tag $TAG from $UPSTREAM_URL" +git fetch --no-tags --force "$UPSTREAM_URL" "refs/tags/${TAG}:${REF}" +echo "[sso] $TAG -> $(git rev-parse "${REF}^{commit}")" echo "[sso] preparing worktree at $OUT" git worktree remove --force "$OUT" 2>/dev/null || true rm -rf "$OUT" -git worktree add --detach "$OUT" "refs/tags/${TAG}" +git worktree add --detach "$OUT" "${REF}^{commit}" + +for sha in "${UPSTREAM_PATCHES[@]}"; do + git fetch --no-tags --quiet "$UPSTREAM_URL" "$sha" + if git merge-base --is-ancestor "$sha" "${REF}^{commit}"; then + echo "[sso] $TAG already contains $sha, skipping" + continue + fi + echo "[sso] applying upstream patch $sha" + git -C "$OUT" cherry-pick --no-commit "$sha" +done node "$SCRIPT_DIR/integrate.mjs" "$OUT" diff --git a/sso/overlay/packages/api/src/middleware/__tests__/proxyAuthRoles.test.ts b/sso/overlay/packages/api/src/middleware/__tests__/proxyAuthRoles.test.ts index 3d7f53d4e3..2c018d1162 100644 --- a/sso/overlay/packages/api/src/middleware/__tests__/proxyAuthRoles.test.ts +++ b/sso/overlay/packages/api/src/middleware/__tests__/proxyAuthRoles.test.ts @@ -31,6 +31,7 @@ function load(adminGroups = 'topia-io:devops,metaversecloud-com:devops') { process.env.PROXY_AUTH_ADMIN_GROUPS = adminGroups; process.env.PROXY_AUTH_MEMBER_GROUPS = 'topia-io:dev'; process.env.PROXY_AUTH_EMAIL_HEADER = 'x-forwarded-email'; + process.env.FRONTEND_URL = 'https://click.topia.io'; // eslint-disable-next-line @typescript-eslint/no-require-imports mod = require('../proxyAuthRoles'); }); @@ -55,6 +56,8 @@ function buildApp( originalUrl: req.originalUrl, user: req.user ?? null, auth: (req as { auth?: unknown }).auth ?? null, + apiKey: 'ingest', + team: { name: 'HyperDX', apiKey: 'ingest', hookId: 'hook' }, }); }); return app; @@ -175,6 +178,93 @@ describe('proxyAuthRoleGuard (session)', () => { }); }); +describe('proxyAuthRoleGuard (hardening)', () => { + const app = buildApp(); + + it.each([ + ['post', '/clickhouse-proxy/TEST', VIEWER], + ['post', '/Clickhouse-Proxy/test', MEMBER], + ['put', '/Sources/1', MEMBER], + ['post', '/DASHBOARDS', VIEWER], + ] as const)( + 'applies roles to case variant %s %s', + async (method, path, groups) => { + await request(app) + [method](path) + .set('x-forwarded-groups', groups) + .expect(403); + }, + ); + + it('forces readonly=2 on case-variant ClickHouse paths', async () => { + const res = await request(app) + .post('/CLICKHOUSE-PROXY/?query_id=1') + .set('x-forwarded-groups', VIEWER) + .expect(200); + expect(res.body.url).toContain('readonly=2'); + }); + + it.each([ + [{ 'sec-fetch-site': 'cross-site' }], + [{ 'sec-fetch-site': 'same-site' }], + [{ origin: 'https://evil.example' }], + ])('blocks cross-site writes %j', async headers => { + await request(app) + .post('/alerts/1/silenced') + .set('x-forwarded-groups', ADMIN) + .set(headers) + .expect(403); + }); + + it.each([ + [{ 'sec-fetch-site': 'same-origin' }], + [{ origin: 'https://click.topia.io' }], + [{}], + ])('allows same-origin and non-browser writes %j', async headers => { + await request(app) + .post('/alerts/1/silenced') + .set('x-forwarded-groups', ADMIN) + .set(headers) + .expect(200); + }); + + it('ignores Sec-Fetch-Site on cross-site reads', async () => { + await request(app) + .get('/dashboards') + .set('x-forwarded-groups', VIEWER) + .set('sec-fetch-site', 'cross-site') + .expect(200); + }); + + it.each(['/me', '/team'])( + 'hides the ingestion key from non-admins on GET %s', + async path => { + for (const groups of [VIEWER, MEMBER]) { + const res = await request(app) + .get(path) + .set('x-forwarded-groups', groups) + .expect(200); + expect(res.body.apiKey).toBeUndefined(); + expect(res.body.team).toEqual({ name: 'HyperDX' }); + } + const admin = await request(app) + .get(path) + .set('x-forwarded-groups', ADMIN) + .expect(200); + expect(admin.body.team.apiKey).toBe('ingest'); + }, + ); + + it.each([ + '/login/password', + '/register/password', + '/team/setup/abc', + '/Login/Password', + ])('disables password auth route %s', async path => { + await request(app).post(path).set('x-forwarded-groups', ADMIN).expect(404); + }); +}); + describe('proxyAuthRoleGuard (session identity)', () => { const user = { _id: 'u1', email: 'dev@topia.io' }; diff --git a/sso/overlay/packages/api/src/middleware/proxyAuthRoles.ts b/sso/overlay/packages/api/src/middleware/proxyAuthRoles.ts index ace6d4b61e..31d1f10474 100644 --- a/sso/overlay/packages/api/src/middleware/proxyAuthRoles.ts +++ b/sso/overlay/packages/api/src/middleware/proxyAuthRoles.ts @@ -4,6 +4,7 @@ import type { ToolAnnotations } from '@modelcontextprotocol/sdk/types.js'; import type { NextFunction, Request, Response } from 'express'; import type { AnyZodObject } from 'zod'; +import { FRONTEND_URL } from '@/config'; import ProxyAuthRole, { PROXY_AUTH_ROLES, type ProxyAuthRoleName, @@ -50,13 +51,16 @@ const ACCESS_KEY_PATH = /^\/(mcp|api\/v2)(\/|$)/; const SAFE_METHODS = ['GET', 'HEAD', 'OPTIONS']; +// Path patterns below are matched against the lowercased path, so they must be +// written in lowercase. + // Non-GET routes that only read data or touch the caller's own state. // /mcp is gated per tool by guardMcpTool. const VIEWER_WRITES = [ /^\/clickhouse-proxy\/(?!test(\/|$))/, /^\/v1\/prometheus\/query/, /^\/ai\/assistant$/, - /^\/me\/(onboarding\/|accessKey$)/, + /^\/me\/(onboarding\/|accesskey$)/, /^\/favorites(\/|$)/, /^\/api\/v2\/(search|charts\/series|dashboards\/validate)\/?$/, /^\/mcp\/?$/, @@ -70,6 +74,25 @@ const MEMBER_WRITES = [ /^\/api\/v2\/(dashboards|saved-searches|alerts|webhooks)(\/|$)/, ]; +// Password sign-in and invite flows bypass the proxy identity, so they are +// switched off while proxy auth is on. +const PASSWORD_AUTH_PATH = + /^\/(login\/password|register\/password|team\/setup\/)/; + +// Responses that embed the team ingestion key, which lets anyone write +// telemetry, so only admins receive it. +const TEAM_KEY_PATH = /^\/(me|team)\/?$/; + +// Unset in some environments (tests, local mode); then any Origin counts as +// cross-site. +const FRONTEND_ORIGIN = (() => { + try { + return new URL(FRONTEND_URL).origin; + } catch { + return undefined; + } +})(); + // Refresh the stored role at most this often per user unless it changes. const RECORD_INTERVAL_MS = 60 * 60 * 1000; const lastRecorded = new Map(); @@ -91,14 +114,37 @@ function roleForGroups(groups: string[]): ProxyAuthRoleName { return groups.some(g => MEMBER_GROUPS.includes(g)) ? 'member' : 'viewer'; } -function requiredRole(req: Request): ProxyAuthRoleName { +function requiredRole(method: string, path: string): ProxyAuthRoleName { if ( - SAFE_METHODS.includes(req.method) || - VIEWER_WRITES.some(re => re.test(req.path)) + SAFE_METHODS.includes(method) || + VIEWER_WRITES.some(re => re.test(path)) ) { return 'viewer'; } - return MEMBER_WRITES.some(re => re.test(req.path)) ? 'member' : 'admin'; + return MEMBER_WRITES.some(re => re.test(path)) ? 'member' : 'admin'; +} + +// oauth2-proxy authenticates any request carrying its cookie, so a form posted +// from another site would act as the victim. Browsers label every request with +// Sec-Fetch-Site; Origin covers the ones that don't. +function isCrossSite(req: Request) { + const site = headerValue(req, 'sec-fetch-site'); + if (site) { + return site !== 'same-origin' && site !== 'none'; + } + const origin = headerValue(req, 'origin'); + return !!origin && origin !== FRONTEND_ORIGIN; +} + +function withoutIngestionKey(body: unknown) { + const data = JSON.parse(JSON.stringify(body ?? null)); + for (const team of [data, data?.team]) { + if (team && typeof team === 'object') { + delete team.apiKey; + delete team.hookId; + } + } + return data; } function recordRole( @@ -146,11 +192,18 @@ export async function proxyAuthRoleGuard( return next(); } + // Express matches routes case-insensitively, so every check below must too. + const path = req.path.toLowerCase(); + + if (PASSWORD_AUTH_PATH.test(path)) { + return res.sendStatus(404); + } + try { let role: ProxyAuthRoleName; let email: string | undefined; - if (ACCESS_KEY_PATH.test(req.path)) { + if (ACCESS_KEY_PATH.test(path)) { const key = req.headers.authorization?.split('Bearer ')[1]; const user = key ? await User.findOne({ accessKey: key }) : null; if (!key || !user) { @@ -196,10 +249,17 @@ export async function proxyAuthRoleGuard( } else if (req.user) { recordRole(req.user, role, groups); } + + if (!SAFE_METHODS.includes(req.method) && isCrossSite(req)) { + logger.warn( + { method: req.method, path, email, origin: req.headers.origin }, + 'Proxy auth: cross-site write blocked', + ); + return res.status(403).json({ message: 'Cross-site request blocked' }); + } } - const needed = requiredRole(req); - const path = req.path; + const needed = requiredRole(req.method, path); if (!hasRole(role, needed)) { writeDecisions.add(1, { role, outcome: 'denied' }); logger.warn( @@ -218,6 +278,11 @@ export async function proxyAuthRoleGuard( req.originalUrl = req.url; } + if (role !== 'admin' && req.method === 'GET' && TEAM_KEY_PATH.test(path)) { + const json = res.json.bind(res); + res.json = body => json(withoutIngestionKey(body)); + } + if (needed !== 'viewer') { writeDecisions.add(1, { role, outcome: 'allowed' }); res.on('finish', () => {