Reject duplicate JSON members when parsing signatures#252
Merged
mtrmac merged 5 commits intocontainers:masterfrom Mar 14, 2017
Merged
Reject duplicate JSON members when parsing signatures#252mtrmac merged 5 commits intocontainers:masterfrom
mtrmac merged 5 commits intocontainers:masterfrom
Conversation
This is a combination of json.Unmarshal + validateExactMapKeys
except using the paranoidUnmarshalJSONObject parser (refusing duplicate
and unknown fields),
+ unmarshaling into typed values instead of a map[string]interface{},
i.e. making int64Field/mapField/stringField unnecessary.
This does not add any user yet.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
…ctUnmarshalJSON Signed-off-by: Miloslav Trmač <mitr@redhat.com>
i.e. validateExactMapKeys, int64Field, mapField, stringField. untrustedSignature.strictUnmarshalJSON no longer uses them. The test scenarios are covered by tests of untrustedSignature.strictUnmarshalJSON already (except for testing the full range of int64 timestamps). Signed-off-by: Miloslav Trmač <mitr@redhat.com>
A test was referring to the old "specific" name for the "transports" member. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
runcom
reviewed
Mar 13, 2017
| // (including duplicated keys, unrecognized keys, and non-matching types). Each of the fields in exactFields | ||
| // must be present exactly once, and none other fields are accepted. | ||
| func paranoidUnmarshalJSONObjectExactFields(data []byte, exactFields map[string]interface{}) error { | ||
| seenKeys := map[string]struct{}{} |
Member
There was a problem hiding this comment.
It might be more idiomatic, but a map[string]bool is better suited here and allows to do
If seenkeys[i]...
W/o playing around with !ok below
Collaborator
Author
There was a problem hiding this comment.
I somewhat prefer the struct{} to make the type closer to the true type (set of string), see also earlier conversation in #223 , and perhaps earlier.
I don’t feel all that strongly about this, but if you can live with it, I’ll merge as is.
Member
In most of the cases, the original code did not explicitly require that a (string) field is present, but empty strings were later rejected (because the "type" field was required to be equal to a constant, or because parsing a docker/reference would fail). Either way, there were already tests verifying that missing fields are rejected; now the code is just a bit more explicit about it. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
9353672 to
3994e56
Compare
Collaborator
Author
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.
Instead of relying on
json.Unmarshalinto amap[string]interface{}, and dealing with field presence and correct types manually, build a helper based onparanoidUnmarshalJSONObjectswhich verifies field presence automatically, relies onjson.Unmarshalfor enforcing types of unstructured values, and newly rejects JSON objects with duplicate members.Besides the primary benefit, rejecting ambiguous signatures, this allows us to get rid of the infrastructure for safely manually parsing
map[string]interface{}. The newparanoidUnmarshalJSONObjectExactFieldscan also be used to simplifypolicy.jsonparsing a bit.Also includes one unrelated test bug fix.
See the individual commits for more details.