Skip to content

Commit

Permalink
Canonicalize JSON before inserting into trillian (#445)
Browse files Browse the repository at this point in the history
Each of the supported types has a Canonicalize() method that generates a
JSON representation of the entry. If the golang library were to make a
change to the order of keys when marshalling an object, it would cause
a duplicate entry in the log for a semantically equivalent object.

This change simply transforms the JSON into RFC8785-compliant
canonicalized JSON protecting against any changes in JSON libraries
going forward.

Signed-off-by: Bob Callaway <[email protected]>
  • Loading branch information
bobcallaway authored Oct 6, 2021
1 parent 58652e3 commit 070f83e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/api/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func createLogEntry(params entries.CreateLogEntryParams) (models.LogEntry, middl
if err != nil {
return nil, handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf(validationError, err))
}
leaf, err := entry.Canonicalize(ctx)
leaf, err := types.CanonicalizeEntry(ctx, entry)
if err != nil {
if _, ok := (err).(types.ValidationError); ok {
return nil, handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf(validationError, err))
Expand Down Expand Up @@ -315,7 +315,7 @@ func SearchLogQueryHandler(params entries.SearchLogQueryParams) middleware.Respo
return err
}

leaf, err := entry.Canonicalize(httpReqCtx)
leaf, err := types.CanonicalizeEntry(httpReqCtx, entry)
if err != nil {
code = http.StatusInternalServerError
return err
Expand Down
12 changes: 12 additions & 0 deletions pkg/types/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/url"
"reflect"

"github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
"github.com/go-openapi/strfmt"
"github.com/mitchellh/mapstructure"
"github.com/sigstore/rekor/pkg/generated/models"
Expand Down Expand Up @@ -105,6 +106,17 @@ func DecodeEntry(input, output interface{}) error {
return dec.Decode(input)
}

// CanonicalizeEntry returns the entry marshalled in JSON according to the
// canonicalization rules of RFC8785 to protect against any changes in golang's JSON
// marshalling logic that may reorder elements
func CanonicalizeEntry(ctx context.Context, entry EntryImpl) ([]byte, error) {
canonicalEntry, err := entry.Canonicalize(ctx)
if err != nil {
return nil, err
}
return jsoncanonicalizer.Transform(canonicalEntry)
}

// ArtifactProperties provide a consistent struct for passing values from
// CLI flags to the type+version specific CreateProposeEntry() methods
type ArtifactProperties struct {
Expand Down

0 comments on commit 070f83e

Please sign in to comment.