Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions .github/workflows/sso-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
Expand All @@ -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 <ver>-sso.<sha>: 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 \
Expand Down
20 changes: 18 additions & 2 deletions sso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -187,4 +201,6 @@ set tfvars → apply.
1. Pick the new `@hyperdx/app@<x.y.z>` 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.
51 changes: 36 additions & 15 deletions sso/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,58 @@
# Produce an SSO-integrated HyperDX source tree at a chosen upstream tag.
#
# sso/apply.sh <upstream-tag> [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 <out-dir> 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 <out-dir> is ready to feed to `docker build`
# (docker/hyperdx/Dockerfile, target prod).
set -euo pipefail

TAG="${1:?usage: sso/apply.sh <upstream-tag> [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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand All @@ -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;
Expand Down Expand Up @@ -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' };

Expand Down
Loading