Skip to content

Commit b4d99d5

Browse files
Merge branch 'main' into fix/implicit-search-fixedstring-hastoken
2 parents d5aa218 + 76d33d7 commit b4d99d5

14 files changed

Lines changed: 258 additions & 16 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@hyperdx/api': minor
3+
---
4+
5+
The API no longer falls back to the public session secret `hyperdx is cool 👋`.
6+
If `EXPRESS_SESSION_SECRET` is unset, the API generates a random secret and
7+
reports it once at startup when authentication is enabled. Authenticated
8+
deployments that explicitly set the old public value also get a random secret
9+
and the same warning; the public value remains available only in the no-auth
10+
local demo. Set it to a random string (`openssl rand -hex 32`) in any deployment
11+
with authentication enabled — without it, restarting the API signs every user
12+
out, and replicas do not share sessions.

‎.env‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ WEBHOOK_HOSTNAME_ALLOWLIST=
3434
# Generate with `openssl rand -base64 32`. Unset means plain text.
3535
TOKEN_ENCRYPTION_KEY=
3636

37+
# Generate once with openssl rand -hex 32; reuse across restarts and replicas.
38+
# Configure in .env.local with docker compose --env-file .env --env-file .env.local up -d.
39+
# EXPRESS_SESSION_SECRET=
40+
3741
# Docker service ports (overridden by scripts/dev-env.sh for isolation)
3842
HDX_DEV_MONGO_PORT=27017
3943
HDX_DEV_CH_HTTP_PORT=8123

‎DEPLOY.md‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ You'll need to set the `MONGO_URI` environment variable to the URI of your
2727
MongoDB instance. Afterwards, you'll want to visit http://localhost:8080 to set
2828
up your connection with ClickHouse.
2929

30-
Before deploying into production, you'll want to set the
31-
`EXPRESS_SESSION_SECRET` environment variable to a random string.
30+
Before deploying into production, set the `EXPRESS_SESSION_SECRET` environment
31+
variable to a random string (e.g. `openssl rand -hex 32`). Without it the API
32+
generates one per process, which signs users out on every restart and prevents
33+
replicas from sharing sessions. The old public value `hyperdx is cool 👋` is
34+
also replaced with a per-process secret when authentication is enabled.
3235

3336
To customize the frontend URL, set the `FRONTEND_URL` environment variable to
3437
the URL your HyperDX instance is hosted on.
@@ -55,8 +58,18 @@ such as ufw. See the
5558
[Docker docs](https://docs.docker.com/engine/network/packet-filtering-firewalls/#docker-and-ufw)
5659
for more information.
5760

58-
Additionally, you'll want to set the `EXPRESS_SESSION_SECRET` environment
59-
variable to a random string.
61+
Generate a secret once with `openssl rand -hex 32`, then set
62+
`EXPRESS_SESSION_SECRET=<generated value>` in `.env.local`, which Git ignores.
63+
Do not put the secret in the tracked root `.env`. Compose forwards this value to
64+
the app container when you load `.env.local` after `.env`:
65+
66+
```bash
67+
docker compose --env-file .env --env-file .env.local up -d
68+
```
69+
70+
Reuse the same secret across restarts and all replicas. Without it the API
71+
generates one per process, which signs users out on every restart and prevents
72+
replicas from sharing sessions. The old public value is not a production secret.
6073

6174
## Local Development Mode
6275

‎agent_docs/development.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ serves both the app and the API.
323323
| -------------------------------------------- | ----------------------------------------------------------------- |
324324
| `HDX_PREVIEW_INLINE_API` | `true` — turns on the inline path |
325325
| `MONGO_URI` | Preview MongoDB connection string |
326-
| `EXPRESS_SESSION_SECRET` | Random 32+ char string |
326+
| `EXPRESS_SESSION_SECRET` | Random 32+ char string shared by all instances; unset generates one per process, invalidating sessions on restart and between instances |
327327
| `DISABLED_AUTH_METHODS` | `google,saml` (avoids per-preview OAuth callback URL config) |
328328
| `AI_PROVIDER`, `AI_API_KEY`, `AI_MODEL_NAME` | Optional, only if AI features should work |
329329
| `FRONTEND_URL` | Optional. Leave unset to use host-only cookies on the preview URL |

‎docker-compose.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ services:
8282
# 32-byte base64 or hex key. Setting it encrypts stored Slack and OAuth
8383
# tokens; unset means they are stored in plain text.
8484
TOKEN_ENCRYPTION_KEY: ${TOKEN_ENCRYPTION_KEY:-}
85+
EXPRESS_SESSION_SECRET: ${EXPRESS_SESSION_SECRET:-}
8586
# Uncomment the next two lines to enable PromQL (Prometheus-compatible metrics)
8687
# ENABLE_PROMQL: 'true'
8788
# NEXT_PUBLIC_ENABLE_PROMQL: 'true'

‎docker/hyperdx/entry.local.base.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export CLICKHOUSE_PROMETHEUS_METRICS_ENDPOINT="${CLICKHOUSE_PROMETHEUS_METRICS_E
2222
# (IS_LOCAL_IMAGE / IS_ALL_IN_ONE_IMAGE). The auth variant must report
2323
# "all-in-one-auth" so that the INGESTION_API_KEY bootstrap path is enabled.
2424
export HYPERDX_IMAGE=$([[ "${IS_LOCAL_APP_MODE}" == "DANGEROUSLY_is_local_app_mode💀" ]] && echo "all-in-one-noauth" || echo "all-in-one-auth")
25-
export EXPRESS_SESSION_SECRET="hyperdx is cool 👋"
25+
# Keep the public demo default only when authentication is disabled.
26+
if [[ "${IS_LOCAL_APP_MODE}" == "DANGEROUSLY_is_local_app_mode💀" ]]; then
27+
export EXPRESS_SESSION_SECRET="${EXPRESS_SESSION_SECRET:-hyperdx is cool 👋}"
28+
fi
2629
# IS_LOCAL_APP_MODE should be set by the calling script
2730
# Default to dangerous mode if not set
2831
export IS_LOCAL_APP_MODE="${IS_LOCAL_APP_MODE}"

‎packages/api/.env.development‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Ports come from scripts/dev-env.sh (worktree isolation) or root .env (defaults)
22
HYPERDX_LOG_LEVEL=debug
3-
EXPRESS_SESSION_SECRET="hyperdx is cool 👋"
3+
# Development only: keep sessions valid across nodemon restarts.
4+
EXPRESS_SESSION_SECRET="local-development-only"
45
FRONTEND_URL="http://localhost:${HYPERDX_APP_PORT}"
56
HDX_NODE_ADVANCED_NETWORK_CAPTURE=1
67
HDX_NODE_BETA_MODE=1

‎packages/api/.env.test‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ CLICKHOUSE_HOST=http://localhost:${HDX_CI_CH_PORT:-18123}
66
CLICKHOUSE_PASSWORD=api
77
CLICKHOUSE_USER=api
88
RUN_SCHEDULED_TASKS_EXTERNALLY=true
9-
EXPRESS_SESSION_SECRET="hyperdx is cool 👋"
109
FRONTEND_URL=http://app:8080
1110
MONGO_URI=mongodb://localhost:${HDX_CI_MONGO_PORT:-39999}/hyperdx-test
1211
NODE_ENV=test
@@ -16,4 +15,4 @@ OPAMP_PORT=${HDX_CI_OPAMP_PORT:-14320}
1615
# Note: the logger module is mocked in jest.setup.ts to suppress expected
1716
# operational noise (validation errors, MCP tool errors, etc.) during tests.
1817
HYPERDX_LOG_LEVEL=error
19-
ENABLE_PROMQL=true
18+
ENABLE_PROMQL=true

‎packages/api/src/__tests__/config.test.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,85 @@ describe('config', () => {
123123
},
124124
);
125125
});
126+
127+
describe('EXPRESS_SESSION_SECRET', () => {
128+
const ORIGINAL_SECRET = process.env.EXPRESS_SESSION_SECRET;
129+
const ORIGINAL_LOCAL_APP_MODE = process.env.IS_LOCAL_APP_MODE;
130+
131+
beforeEach(() => {
132+
delete process.env.IS_LOCAL_APP_MODE;
133+
});
134+
135+
afterEach(() => {
136+
if (ORIGINAL_SECRET === undefined) {
137+
delete process.env.EXPRESS_SESSION_SECRET;
138+
} else {
139+
process.env.EXPRESS_SESSION_SECRET = ORIGINAL_SECRET;
140+
}
141+
if (ORIGINAL_LOCAL_APP_MODE === undefined) {
142+
delete process.env.IS_LOCAL_APP_MODE;
143+
} else {
144+
process.env.IS_LOCAL_APP_MODE = ORIGINAL_LOCAL_APP_MODE;
145+
}
146+
jest.resetModules();
147+
});
148+
149+
// The `jest` object's isolateModules returns `jest` itself rather than the
150+
// callback's value, so the module has to be captured from inside it.
151+
const loadConfig = () => {
152+
let loaded!: typeof import('@/config');
153+
jest.isolateModules(() => {
154+
loaded = jest.requireActual<typeof import('@/config')>('@/config');
155+
});
156+
return loaded;
157+
};
158+
159+
it('uses a configured secret verbatim', () => {
160+
process.env.EXPRESS_SESSION_SECRET = 'a-deployment-specific-secret';
161+
162+
const config = loadConfig();
163+
expect(config.EXPRESS_SESSION_SECRET).toBe(
164+
'a-deployment-specific-secret',
165+
);
166+
expect(config.IS_EXPRESS_SESSION_SECRET_GENERATED).toBe(false);
167+
});
168+
169+
it('preserves a configured secret verbatim', () => {
170+
process.env.EXPRESS_SESSION_SECRET = ' verbatim secret 👋 ';
171+
const config = loadConfig();
172+
expect(config.EXPRESS_SESSION_SECRET).toBe(' verbatim secret 👋 ');
173+
expect(config.IS_EXPRESS_SESSION_SECRET_GENERATED).toBe(false);
174+
});
175+
176+
it('generates a secret when unset, empty, or publicly known', () => {
177+
for (const value of [undefined, '', 'hyperdx is cool 👋']) {
178+
if (value === undefined) {
179+
delete process.env.EXPRESS_SESSION_SECRET;
180+
} else {
181+
process.env.EXPRESS_SESSION_SECRET = value;
182+
}
183+
184+
const config = loadConfig();
185+
expect(config.EXPRESS_SESSION_SECRET).toMatch(/^[0-9a-f]{64}$/);
186+
expect(config.IS_EXPRESS_SESSION_SECRET_GENERATED).toBe(true);
187+
}
188+
});
189+
190+
it('keeps the public demo secret only without authentication', () => {
191+
process.env.IS_LOCAL_APP_MODE = 'DANGEROUSLY_is_local_app_mode💀';
192+
process.env.EXPRESS_SESSION_SECRET = 'hyperdx is cool 👋';
193+
194+
const config = loadConfig();
195+
expect(config.EXPRESS_SESSION_SECRET).toBe('hyperdx is cool 👋');
196+
expect(config.IS_EXPRESS_SESSION_SECRET_GENERATED).toBe(false);
197+
});
198+
199+
it('generates a different secret on each load', () => {
200+
delete process.env.EXPRESS_SESSION_SECRET;
201+
202+
expect(loadConfig().EXPRESS_SESSION_SECRET).not.toBe(
203+
loadConfig().EXPRESS_SESSION_SECRET,
204+
);
205+
});
206+
});
126207
});
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { IncomingMessage, ServerResponse } from 'http';
2+
import { Socket } from 'net';
3+
4+
jest.mock('@/api-app', () => ({ __esModule: true, default: jest.fn() }));
5+
jest.mock('@/controllers/team', () => ({
6+
LOCAL_APP_TEAM: { _id: { toString: () => 'local-team' } },
7+
}));
8+
jest.mock('@/migrations', () => ({ runStartupMigrations: jest.fn() }));
9+
jest.mock('@/models', () => ({
10+
connectDB: jest.fn().mockResolvedValue(undefined),
11+
connectDBWithRetry: jest.fn().mockResolvedValue(undefined),
12+
}));
13+
jest.mock('@/opamp/app', () => ({ __esModule: true, default: jest.fn() }));
14+
jest.mock('@/setupDefaults', () => ({ setupTeamDefaults: jest.fn() }));
15+
jest.mock('@/utils/tokenEncryption', () => ({
16+
verifyTokenEncryption: jest.fn(),
17+
}));
18+
jest.mock('@/utils/logger', () => ({
19+
__esModule: true,
20+
default: { error: jest.fn(), info: jest.fn() },
21+
}));
22+
jest.mock('@/utils/instrumentation', () => ({
23+
getCounter: jest.fn(() => ({ add: jest.fn() })),
24+
}));
25+
jest.mock('http-graceful-shutdown', () => jest.fn());
26+
jest.mock('http', () => ({
27+
...jest.requireActual('http'),
28+
createServer: jest.fn(() => ({ listen: jest.fn() })),
29+
}));
30+
31+
describe.each(['server', 'serverless'])(
32+
'session secret startup diagnostics (%s first)',
33+
first => {
34+
const originalEnv = { ...process.env };
35+
36+
afterEach(() => {
37+
process.env = { ...originalEnv };
38+
jest.resetModules();
39+
});
40+
41+
it.each([
42+
[false, undefined, 1],
43+
[false, '', 1],
44+
[false, 'hyperdx is cool 👋', 1],
45+
[false, 'startup-secret-sentinel', 0],
46+
[true, undefined, 0],
47+
[true, 'hyperdx is cool 👋', 0],
48+
[true, 'startup-secret-sentinel', 0],
49+
])(
50+
'local=%s, secret=%s logs %s times across both entrypoints',
51+
async (local, secret, count) => {
52+
process.env.APP_TYPE = 'api';
53+
if (local) {
54+
process.env.IS_LOCAL_APP_MODE = 'DANGEROUSLY_is_local_app_mode💀';
55+
} else {
56+
delete process.env.IS_LOCAL_APP_MODE;
57+
}
58+
if (typeof secret === 'undefined')
59+
delete process.env.EXPRESS_SESSION_SECRET;
60+
else process.env.EXPRESS_SESSION_SECRET = secret;
61+
62+
const { default: logger } =
63+
jest.requireMock<typeof import('@/utils/logger')>('@/utils/logger');
64+
const { getCounter } = jest.requireMock<
65+
typeof import('@/utils/instrumentation')
66+
>('@/utils/instrumentation');
67+
const { default: Server } =
68+
jest.requireActual<typeof import('@/server')>('@/server');
69+
const { serverlessHandler } =
70+
jest.requireActual<typeof import('@/serverless')>('@/serverless');
71+
const config =
72+
jest.requireActual<typeof import('@/config')>('@/config');
73+
expect(logger.error).not.toHaveBeenCalled();
74+
75+
const req = new IncomingMessage(new Socket());
76+
req.url = '/api/health';
77+
const res = new ServerResponse(req);
78+
if (first === 'server') await new Server().start();
79+
await serverlessHandler(req, res);
80+
await new Server().start();
81+
await serverlessHandler(req, res);
82+
83+
expect(logger.error).toHaveBeenCalledTimes(count);
84+
const counter = jest.mocked(getCounter).mock.results[0].value;
85+
expect(counter.add).toHaveBeenCalledTimes(count);
86+
expect(
87+
JSON.stringify(jest.mocked(logger.error).mock.calls),
88+
).not.toContain(config.EXPRESS_SESSION_SECRET);
89+
},
90+
);
91+
},
92+
);

0 commit comments

Comments
 (0)