Skip to content

Commit

Permalink
fix: allow any content using starts-with with an empty value (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuneforme authored Oct 31, 2022
1 parent 36e5116 commit d6fbd49
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
4 changes: 0 additions & 4 deletions functional_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4204,10 +4204,6 @@ func testPresignedPostPolicy() {
logError(testName, function, args, startTime, "", "SetKey did not fail for invalid conditions", err)
return
}
if err := policy.SetKeyStartsWith(""); err == nil {
logError(testName, function, args, startTime, "", "SetKeyStartsWith did not fail for invalid conditions", err)
return
}
if err := policy.SetExpires(time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC)); err == nil {
logError(testName, function, args, startTime, "", "SetExpires did not fail for invalid conditions", err)
return
Expand Down
12 changes: 7 additions & 5 deletions post-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ func (p *PostPolicy) SetKey(key string) error {

// SetKeyStartsWith - Sets an object name that an policy based upload
// can start with.
// Can use an empty value ("") to allow any key.
func (p *PostPolicy) SetKeyStartsWith(keyStartsWith string) error {
if strings.TrimSpace(keyStartsWith) == "" || keyStartsWith == "" {
return errInvalidArgument("Object prefix is empty.")
}
policyCond := policyCondition{
matchType: "starts-with",
condition: "$key",
Expand Down Expand Up @@ -171,7 +169,7 @@ func (p *PostPolicy) SetContentType(contentType string) error {

// SetContentTypeStartsWith - Sets what content-type of the object for this policy
// based upload can start with.
// If "" is provided it allows all content-types.
// Can use an empty value ("") to allow any content-type.
func (p *PostPolicy) SetContentTypeStartsWith(contentTypeStartsWith string) error {
policyCond := policyCondition{
matchType: "starts-with",
Expand Down Expand Up @@ -283,10 +281,14 @@ func (p *PostPolicy) SetUserData(key string, value string) error {
}

// addNewPolicy - internal helper to validate adding new policies.
// Can use starts-with with an empty value ("") to allow any content within a form field.
func (p *PostPolicy) addNewPolicy(policyCond policyCondition) error {
if policyCond.matchType == "" || policyCond.condition == "" || policyCond.value == "" {
if policyCond.matchType == "" || policyCond.condition == "" {
return errInvalidArgument("Policy fields are empty.")
}
if policyCond.matchType != "starts-with" && policyCond.value == "" {
return errInvalidArgument("Policy value is empty.")
}
p.conditions = append(p.conditions, policyCond)
return nil
}
Expand Down

0 comments on commit d6fbd49

Please sign in to comment.