fix(api): guard validateInstallRequest against null/undefined + wrap install catch JSON.parse (PKT-472) - #405
Open
bugsyhewitt wants to merge 1 commit into
Conversation
…install catch JSON.parse (PKT-472) Part A: add typeof guards in validateInstallRequest before calling isValidApiKeyFormat/ isValidMachineHash, using local unknown-typed variables to avoid TS narrowing issues. Returns INVALID_API_KEY_FORMAT/INVALID_MACHINE_HASH with received_type detail instead of throwing a TypeError that escaped to the outer 500 handler. Part B: wrap the catch arm's JSON.parse((e as Error).message) in a nested try/catch in the /v1/install route. On inner parse failure (non-structured throw) writes to stderr only and returns a safe generic INSTALL_HANDLER_ERROR body. No e.message in response body. Adds 4 unit tests in test/api/validation.test.ts and 1 integration test in test/api/ts-api-server-route-handler-defensive-paths.test.ts confirming 400 (not 500) for null api_key. All 2166 TS tests pass; 1206 Python tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This was referenced Aug 3, 2026
fix(api): guard /v1/install catch against non-JSON Error.message (PKT-530 / PKT-472 regression)
#441
Open
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
Corrective re-author of rejected PKT-406. Fixes
POST /v1/installreturning500 INTERNAL_ERRORwhenapi_keyormachine_hashisnull/undefinedin the request body.Root cause: Two-layer failure:
validation.ts:110-118—isValidApiKeyFormat(req.api_key)calls.lengthonnull/undefined, throwingTypeErrorserver.ts:238—JSON.parse((e as Error).message)throwsSyntaxErroron the non-JSON TypeError message, escaping the catch and falling through to the outer 500 handlerFix (defense-in-depth):
validation.ts): Addtypeofguards at the top ofvalidateInstallRequestusingconst apiKey: unknown = req.api_keyto handle null/undefined — returnsINVALID_API_KEY_FORMAT/INVALID_MACHINE_HASHwithreceived_typedetail before reaching.lengthserver.ts): WrapJSON.parse((e as Error).message)in a nested try/catch — structured throws (thehandleInstallJSON-stringify convention) still reach the client as parsed objects; non-structured throws (TypeErrors, future storage plain Errors) log${e}to stderr only and return a genericINSTALL_HANDLER_ERRORbody (noe.messageleak per PKT-406 rejection directive)Tests (+5 net): 4 unit tests in
validation.test.ts(null api_key, undefined api_key, null machine_hash, numeric api_key) + 1 integration test ints-api-server-route-handler-defensive-paths.test.ts(null api_key → 400 via full server stack)TDD: All 5 tests written and verified failing before implementation.
Ship-gate results
pnpm tsc --noEmit: pre-existing 3 errors in unrelated test files (unchanged)Runtime verify
Confirmed on scratch port 19472 with live server:
Files changed
src/alienclaw/api/validation.ts(~12 LOC added)src/alienclaw/api/server.ts(~13 LOC changed)test/api/validation.test.ts(4 tests added)test/api/ts-api-server-route-handler-defensive-paths.test.ts(1 test added)Binding architectural review: APPROVED (alienclaw-architect, Opus)
🤖 Generated with Claude Code