Skip to content

core: report a malformed policy signature as tampering, not bad JSON - #401

Closed
mertdurukan wants to merge 1 commit into
solongate:mainfrom
mertdurukan:fix/186-malformed-signature
Closed

mertdurukan wants to merge 1 commit into
solongate:mainfrom
mertdurukan:fix/186-malformed-signature

Conversation

@mertdurukan

Copy link
Copy Markdown

Fixes #186.

readStore treated every VerifyDocument error as a JSON syntax error. A policy.json that is valid JSON but has a corrupted _sig was reported as "not valid JSON".
VerifyDocument now returns an exported ErrMalformedSignature for that case (same message as before). readStore lets it fall through to the existing "failed signature verification" error.
The guard's verifyHomePolicy is unchanged: it already treats any VerifyDocument error as tampering.

Added a test in crypto for the sentinel and one in core/client that corrupts _sig; the latter failed before the fix. go vet and go test pass in all nine modules, sg-parity-check.sh 10/10, sg-layers-check.sh 25/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.

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>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 14 complexity · 4 duplication

Metric Results
Complexity 14
Duplication 4

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +113 to +128
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!"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ 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.

See Clone in Codacy

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

@mertdurukan mertdurukan closed this by deleting the head repository Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crypto VerifyDocument cannot distinguish a malformed signature from invalid JSON

1 participant