Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/core/client/localstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ func readStore() (stored, error) {
return stored{}, fmt.Errorf("cannot load the machine signing key: %w", err)
}
_, signed, valid, err := pcrypto.VerifyDocument(b, kp.Public)
if err != nil {
// A malformed signature comes back as signed && !valid and is reported
// as tampering below, not as a JSON syntax error.
if err != nil && !errors.Is(err, pcrypto.ErrMalformedSignature) {
return stored{}, fmt.Errorf("%s is not valid JSON: %w", PolicyPath(), err)
}
if signed && !valid {
Expand Down
33 changes: 33 additions & 0 deletions packages/core/client/localstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,39 @@ func TestReadStoreRefusesATamperedFile(t *testing.T) {
}
}

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

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.

corrupted, err := json.Marshal(doc)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(PolicyPath(), corrupted, 0o600); err != nil {
t.Fatal(err)
}

_, err = readStore()
if err == nil {
t.Fatal("a file with a corrupted signature was accepted without complaint")
}
if strings.Contains(err.Error(), "not valid JSON") || !strings.Contains(err.Error(), "failed signature verification") {
t.Errorf("valid JSON with a corrupted signature should be reported as a signature failure, got: %v", err)
}
}

func TestToLayersWithNoSecurityIsEverythingOff(t *testing.T) {
l := toLayers(nil)
if l.RateLimit.Mode != LayerOff {
Expand Down
16 changes: 16 additions & 0 deletions packages/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package crypto
import (
"bytes"
"encoding/json"
"errors"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -150,6 +151,21 @@ func TestVerifyDocumentDetectsTamper(t *testing.T) {
}
}

func TestVerifyDocumentReportsMalformedSignature(t *testing.T) {
kp, err := GenerateKeyPair()
if err != nil {
t.Fatal(err)
}
raw := []byte(`{"_sig":"not base64!","policy":"default"}`)
_, signed, valid, err := VerifyDocument(raw, kp.Public)
if !errors.Is(err, ErrMalformedSignature) {
t.Fatalf("expected ErrMalformedSignature, got %v", err)
}
if !signed || valid {
t.Fatalf("expected signed=true valid=false for a malformed signature, got signed=%v valid=%v", signed, valid)
}
}

func TestVerifyDocumentTreatsMissingSignatureAsUnsigned(t *testing.T) {
kp, err := GenerateKeyPair()
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion packages/crypto/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const (
sigPubField = "_sig_pub"
)

// ErrMalformedSignature is returned by VerifyDocument when the document is
// valid JSON but its _sig field is not valid base64. Callers can tell it apart
// from a JSON syntax error and report it as tampering.
var ErrMalformedSignature = errors.New("crypto: malformed signature encoding")

// Sign returns the Ed25519 signature of payload under priv.
func Sign(priv ed25519.PrivateKey, payload []byte) []byte {
return ed25519.Sign(priv, payload)
Expand Down Expand Up @@ -66,7 +71,7 @@ func VerifyDocument(raw []byte, trusted ed25519.PublicKey) (payload map[string]a
}
sig, err := base64.StdEncoding.DecodeString(sigStr)
if err != nil {
return nil, true, false, errors.New("crypto: malformed signature encoding")
return nil, true, false, ErrMalformedSignature
}

clean := stripSigFields(doc)
Expand Down