Skip to content

Commit

Permalink
Merge pull request #57 from envato/dont-decode-policy
Browse files Browse the repository at this point in the history
Dont decode S3 Bucket policies
  • Loading branch information
Patrick Robinson committed Mar 21, 2019
2 parents 87c4e01 + 59eac9c commit d00e90f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion iamy/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (a *AwsFetcher) fetchS3Data() error {
continue
}

policyDoc, err := NewPolicyDocumentFromEncodedJson(b.policyJson)
policyDoc, err := NewPolicyDocumentFromJson(b.policyJson)
if err != nil {
return errors.Wrap(err, "Error creating Policy document")
}
Expand Down
17 changes: 11 additions & 6 deletions iamy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ import (
"sort"
)

func NewPolicyDocumentFromEncodedJson(encoded string) (*PolicyDocument, error) {
jsonString, err := url.QueryUnescape(encoded)
if err != nil {
func NewPolicyDocumentFromJson(jsonString string) (*PolicyDocument, error) {
var doc PolicyDocument
if err := json.Unmarshal([]byte(jsonString), &doc); err != nil {
log.Printf("Error unmarshalling JSON %s %s", err, jsonString)
return nil, err
}

var doc PolicyDocument
if err = json.Unmarshal([]byte(jsonString), &doc); err != nil {
return &doc, nil
}

func NewPolicyDocumentFromEncodedJson(encoded string) (*PolicyDocument, error) {
jsonString, err := url.QueryUnescape(encoded)
if err != nil {
return nil, err
}

return &doc, nil
return NewPolicyDocumentFromJson(jsonString)
}

// PolicyDocument represents an AWS policy document.
Expand Down
7 changes: 7 additions & 0 deletions iamy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,10 @@ Actual: %#v`, nt.description, nt.input, nt.expected, result)
}
}
}

func TestNewPolicyDocumentFromJson(t *testing.T) {
_, err := NewPolicyDocumentFromJson(`{"Version":"2012-10-17","Id":"AllowPublicRead","Statement":[{"Sid":"PublicReadBucketObjects","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::example.com/*","Condition":{"StringEquals":{"aws:Referer":"%zz"}}}]}`)
if err != nil {
t.Errorf("Error decoding policy %s", err)
}
}

0 comments on commit d00e90f

Please sign in to comment.