-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: support non-base64 data URIs per RFC 2397 #1556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -607,7 +607,21 @@ func isDataURI(fl FieldLevel) bool { | |
| return false | ||
| } | ||
|
|
||
| return base64Regex().MatchString(uri[1]) | ||
| // After "data:", the prefix must be empty, start with ";", or contain "/" | ||
| // to form a valid mediatype. This rejects partial matches like "data:text" | ||
| // where the regex only matches the "data:" portion. | ||
| if prefix := uri[0][5:]; prefix != "" && !strings.Contains(prefix, "/") && prefix[0] != ';' { | ||
| return false | ||
| } | ||
|
|
||
| // Only validate the data portion as base64 when ;base64 is specified. | ||
| // Per RFC 2397, ;base64 is a terminal flag that appears immediately | ||
| // before the comma, so it is always a suffix of the prefix portion. | ||
| if strings.HasSuffix(uri[0], ";base64") { | ||
| return base64Regex().MatchString(uri[1]) | ||
| } | ||
|
|
||
| return true | ||
|
Comment on lines
+617
to
+624
|
||
| } | ||
|
|
||
| // hasMultiByteCharacter is the validation function for validating if the field's value has a multi byte character. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3997,8 +3997,11 @@ func TestDataURIValidation(t *testing.T) { | |||||||||
| {"data:text,:;base85,U3VzcGVuZGlzc2UgbGVjdHVzIGxlbw==", false}, | ||||||||||
| {"data:image/jpeg;key=value;base64,UEsDBBQAAAAI", true}, | ||||||||||
| {"data:image/jpeg;key=value,UEsDBBQAAAAI", true}, | ||||||||||
| {"data:;base64;sdfgsdfgsdfasdfa=s,UEsDBBQAAAAI", true}, | ||||||||||
| {"data:;base64,UEsDBBQAAAAI", true}, | ||||||||||
| {"data:,UEsDBBQAAAAI", true}, | ||||||||||
| {"data:,ohai", true}, | ||||||||||
| {"data:text/plain,hello world", true}, | ||||||||||
| {"data:text/html,<h1>Hello</h1>", true}, | ||||||||||
|
Comment on lines
+4003
to
+4004
|
||||||||||
| {"data:text/plain,hello world", true}, | |
| {"data:text/html,<h1>Hello</h1>", true}, | |
| {"data:text/plain,hello%20world", true}, | |
| {"data:text/html,%3Ch1%3EHello%3C%2Fh1%3E", true}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would change this, to disallow the non-URL encoded values, and allow URL encoded.
Uh oh!
There was an error while loading. Please reload this page.