Repository navigation
core: report a malformed policy signature as tampering, not bad JSON - #401
mertdurukan wants to merge 1 commit into
Conversation
readStore treated every VerifyDocument error as a JSON syntax error, so a policy.json with a corrupted _sig was reported as "not valid JSON". VerifyDocument now returns an exported ErrMalformedSignature for that case, and readStore lets it fall through to the signature-verification error. Signed-off-by: Mert Durukan <mert@durukan.dev>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 14 |
| Duplication | 4 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull Request Overview
✅ No blocking issues noticed
No blocking findings were identified. The implementation and focused tests cover malformed signatures as tampering, including the exported error behavior.
A regression test for genuinely malformed policy JSON is still missing and should be added to protect the distinction between parse errors and signature tampering. The only code comment is a low-severity suggestion to reduce duplicated test setup.
Test suggestions
- VerifyDocument returns ErrMalformedSignature with signed=true and valid=false for an invalid base64 signature
- readStore reports a valid JSON policy with a corrupted signature as signature verification failure, not invalid JSON
- readStore continues to report malformed policy JSON as invalid JSON
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. readStore continues to report malformed policy JSON as invalid JSON
TIP Add review instructions in .codacy/instructions/review.md, or an AGENTS.md, so the reviewer knows your conventions.
TIP How was this review? Give us feedback
| func TestReadStoreReportsAMalformedSignatureAsTampering(t *testing.T) { | ||
| newHome(t) | ||
| policy := &PolicySet{ID: "local", Name: "local"} | ||
| if err := writeStore(stored{Policy: policy, envelope: true}); err != nil { | ||
| t.Fatalf("writeStore: %v", err) | ||
| } | ||
|
|
||
| raw, err := os.ReadFile(PolicyPath()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var doc map[string]any | ||
| if err := json.Unmarshal(raw, &doc); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| doc["_sig"] = "not base64!" |
There was a problem hiding this comment.
⚪ LOW RISK
Suggestion: Consider extracting the duplicated policy creation, loading, mutation, and rewrite setup into a small test helper so each test focuses on its distinct assertion.
TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.
Fixes #186.
readStoretreated everyVerifyDocumenterror as a JSON syntax error. Apolicy.jsonthat is valid JSON but has a corrupted_sigwas reported as "not valid JSON".VerifyDocumentnow returns an exportedErrMalformedSignaturefor that case (same message as before).readStorelets it fall through to the existing "failed signature verification" error.The guard's
verifyHomePolicyis unchanged: it already treats anyVerifyDocumenterror as tampering.Added a test in
cryptofor the sentinel and one incore/clientthat corrupts_sig; the latter failed before the fix.go vetandgo testpass in all nine modules,sg-parity-check.sh10/10,sg-layers-check.sh25/25 (Linux container). Not tried by hand against a real agent; the node test was not run (no JS change).Prepared with an AI coding agent (Claude) on my account. Tests run locally in a container.