Env validation + structured error logs#15
Open
akshitkrnagpal wants to merge 2 commits into
Open
Conversation
The earlier cryptic "atob() called with invalid base64-encoded data" deep in the dispatch consumer was an undefined ENCRYPTION_KEY on edgepush-api. The error itself never named the variable; the operator had to trace it back through decryptCredential to figure out which secret was wrong. This adds runtime env validation that surfaces the variable name + a non-secret reason at three entry points: - HTTP routes: a Hono onError handler catches EnvValidationError and returns a structured 503 with the variable + reason instead of a generic 500. - Queue consumer (dispatch): validates required env at batch start. Misconfiguration retries the whole batch (so jobs drain once the operator fixes things) and logs a single clear line per batch instead of one cryptic atob error per job. - Scheduled handler (cron): validates env at scheduled-event entry. The probe cycle would otherwise fail mid-loop with a confusing log line per app. Also exposes the validation through /health/deep — the new "env" component reports any misconfigured required secrets by name. Validation messages never include the secret value. Reasons are limited to: "missing or empty", "not valid base64 (length N, contains characters outside [A-Za-z0-9+/=])", "decoded to N bytes, expected M". Length is fine to expose; the value is not. 19 unit tests in env-validation.test.ts cover the validators and the variable-name leakage prevention. Excluded test files from the server tsconfig so they don't pollute the build types — the package will get a proper vitest setup as a follow-up. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replaces the hand-rolled validators with @t3-oss/env-core's createEnv + a zod schema. Same surface (parseEnv / checkEnv / EnvValidationError) but the schema is declarative and the error path goes through zod's issue list instead of throw chains. - Add @t3-oss/env-core dep - Move env.ts; delete env-validation.ts/.test.ts - ENCRYPTION_KEY validator: zod.string with a superRefine that base64- decodes and asserts byte length. Other required vars use the standard zod.string().min(1) and zod.url() validators - Expose validateEncryptionKey for crypto.ts so a bad key still surfaces with the variable name (instead of having to call full parseEnv with synthetic values for the others) - 13 tests covering parseEnv (throw-on-first), checkEnv (collect-all), validateEncryptionKey, and the "value never leaks into the error" property - Add vitest as a dev dep on the server package so tests run from there directly (not pulled from the workspace root) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Owner
Author
Code Review SummaryVerdict: Changes requested Blocking
Verified
|
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.
Summary
Stops the cryptic `atob() called with invalid base64-encoded data` errors that show up deep in the dispatch consumer when an env var is missing or malformed. New validation surfaces the variable name + a non-secret reason at three entry points + via `/health/deep`.
Why
Today the path is:
After this PR:
Same for HTTP routes (`onError` returns 503 with structured detail) and cron (validation at scheduled-event entry).
Changes
Operational notes
Test plan
🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmithwith what you need.