fix(config): validate env var overrides and re-validate config (issue #13) - #33
Merged
Conversation
…13) Prevents CWE-20 bypass of Zod schema validation through TELETON_* env vars. Env var overrides now apply the same constraints as the schema before assignment, and the full config is re-validated after all overrides. Changes: - loader.ts: Replace truthy checks with `!== undefined` so empty strings are caught and validated instead of silently ignored - loader.ts: Add min-length check for TELETON_API_KEY (≥8 chars) - loader.ts: Add empty-string rejection for TELETON_TG_API_HASH, TELETON_TG_PHONE, TELETON_WEBUI_AUTH_TOKEN, TELETON_TAVILY_API_KEY, TELETON_TONAPI_KEY, TELETON_TONCENTER_API_KEY - loader.ts: Validate TELETON_WEBUI_ENABLED and TELETON_API_ENABLED are strictly "true"/"false" (case-insensitive) - loader.ts: Extract parsePort() helper enforcing 1024-65535 range, throw on invalid values instead of silent ignore - loader.ts: Hard-block non-loopback TELETON_WEBUI_HOST when auth_token is not set (was only a warning) - loader.ts: Validate TELETON_BASE_URL scheme is http/https - loader.ts: Add TELETON_WEBUI_AUTH_TOKEN env var override - loader.ts: Re-validate full config via ConfigSchema.safeParse() after all env var overrides to catch any divergence - loader.test.ts: Update tests for new validation behavior, add 17 new test cases covering empty strings, invalid values, port ranges, non-loopback blocking, and scheme validation Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Fixes issue #13 — HIGH severity CWE-20: Config env var overrides bypass Zod schema validation entirely.
TELETON_*environment variables were applied directly to the parsed config object after Zod validation, with no further validation. This allowed injection of arbitrary values (empty strings, invalid ports, non-loopback hosts, non-HTTPS base URLs) that bypass all schema constraints.Root Cause
Config validation ran once with
ConfigSchema.safeParse(raw)(line 95), then env vars directly mutated the parsed object. Zod validation was never re-run — the "split brain" anti-pattern where validated config and runtime config diverge silently.Changes
src/config/loader.tsif (process.env.X)— empty string""silently ignoredif (process.env.X !== undefined)— empty string caught and validatedTELETON_API_KEY— no format checkTELETON_TG_API_HASH,TELETON_TG_PHONE— no empty checkTELETON_WEBUI_ENABLED— any truthy value = true"true"/"false"(case-insensitive)TELETON_API_ENABLED— any truthy value = true"true"/"false"(case-insensitive)TELETON_WEBUI_PORT— invalid silently ignoredparsePort()helper throws on invalid/out-of-rangeTELETON_WEBUI_HOST=0.0.0.0— only warnedauth_tokenTELETON_BASE_URL— any valid URL acceptedhttporhttpsConfigSchema.safeParse(config)re-validates final configTELETON_WEBUI_AUTH_TOKENenv varTELETON_TAVILY_API_KEY,TELETON_TONAPI_KEY,TELETON_TONCENTER_API_KEY— no validationsrc/config/__tests__/loader.test.tsTELETON_WEBUI_ENABLED/TELETON_API_ENABLEDvaluesVerification
npx vitest run src/config/__tests__/loader.test.ts # 71 tests passed ✓Security Impact
TELETON_API_KEY=""TELETON_API_KEY="short"TELETON_WEBUI_ENABLED=maybeconfig.webui.enabled = falseTELETON_WEBUI_PORT=22TELETON_WEBUI_HOST=0.0.0.0TELETON_BASE_URL=ftp://evil.comTELETON_TONAPI_KEY=""🤖 Generated with Claude Code