feat: add JWT expiry validation and refactor magic numbers#612
Merged
Conversation
- Decode JWT in WebSocket auth to reject expired tokens and log auth failures. - Introduce constants `OUTPUT_BUFFER_MAX_LENGTH`, `BACKUP_UPDATE_DELAY_MS`, and `JWT_EXP_MS_FACTOR`. - Replace hardcoded buffer length and delay values with the new constants. - Coerce `AUTH_ENABLED` and `AUTH_SETUP_COMPLETED` to boolean and add `AUTH_SESSION_DURATION_DAYS` env.
MickLesk
approved these changes
Jun 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Summary
✍️ Description
Fix: WebSocket 401 Unauthorized — JWT auth mismatch due to
.envparsingProblem: When installing an LXC container, the WebSocket connection to
ws://host:3000/ws/script-executionwas rejected with HTTP 401 even though the user was authenticated with a validauth-tokencookie.Root cause:
getAuthConfig()andgetJwtSecret()insrc/lib/auth.tsparsed the.envfile directly with regex instead of reading fromprocess.env(already populated bydotenv.config()). On startup, if the regex failed to match (encoding, whitespace, quotes),getJwtSecret()auto-generated a new secret and overwrote the.envfile. On the next restart,dotenvloaded the new secret, but the session cookie was signed with the old one → verification failed → 401.Changes
src/env.js— Fixed zod schema for auth variables:AUTH_ENABLED,AUTH_SETUP_COMPLETED: changed fromz.string()toz.coerce.boolean()(handles"true"/"false"strings properly)AUTH_SESSION_DURATION_DAYS:z.coerce.number().int().positive()src/lib/auth.ts— Simplified config reading, removed regex fallbacks:getJwtSecret(): reads exclusively fromprocess.env.JWT_SECRET, auto-generates only when absent. No.envfile regex parsing.getAuthConfig(): reads all 5 auth variables fromprocess.envdirectly with inline conversions. RemovedreadString/readBool/readInthelpers and the.envfile fallback regex.updateAuthCredentials,setSetupCompleted, etc.) unchanged — their regex usage is necessary for in-place.envediting.server.js— Logging & magic number cleanup:isWebSocketUpgradeAuthorized(): added 3 security-safe warning logs (missing auth-token cookie,token expired,token verification failed) without exposing token contentsrequire()ofdecodeToken→ static ES import1000) with 3 named constants:JWT_EXP_MS_FACTOR,OUTPUT_BUFFER_MAX_LENGTH,BACKUP_UPDATE_DELAY_MS🔗 Related PR / Issue
Fixes: #611
✅ Prerequisites (X in brackets)
🛠️ Type of Change (X in brackets)