Skip to content

Commit

Permalink
Fix mime parsing to require slash in mime
Browse files Browse the repository at this point in the history
When migrating ParseContentType() from http_util to become
ParseMimeType() I noticed that code to check for a slash in the mime
type was inadvertently looking through the entire string.

Bug: 1202034
Change-Id: If78c75be6e302f6cc2314264bd68ec4090b81732
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2896198
Reviewed-by: Matt Menke <mmenke@chromium.org>
Commit-Queue: Joel Hockey <joelhockey@chromium.org>
Cr-Commit-Position: refs/heads/master@{#883248}
  • Loading branch information
Joel Hockey authored and Chromium LUCI CQ committed May 15, 2021
1 parent 757d8a1 commit f89e0d4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
5 changes: 1 addition & 4 deletions net/base/mime_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,8 @@ bool ParseMimeType(const std::string& type_str,
type_end = type_str.length();

// Reject a mime-type if it does not include a slash.
// TODO(crbug.com/1202034): This is currently matching old code to search
// anywhere in the string for a slash. Update to require the slash in the
// mime which was the intention.
size_t slash_pos = type_str.find_first_of('/');
if (slash_pos == std::string::npos)
if (slash_pos == std::string::npos || slash_pos > type_end)
return false;
if (mime_type)
*mime_type = type_str.substr(type_val, type_end - type_val);
Expand Down
3 changes: 1 addition & 2 deletions net/base/mime_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ TEST(MimeUtilTest, TestParseMimeType) {
{"/ts", "/ts", {}},
{"/s", "/s", {}},
{"/", "/", {}},
// TODO(crbug.com/1202034): This is a bug and should fail.
{"t / s", "t", {}},
};
for (const auto& test : tests) {
std::string mime_type;
Expand All @@ -258,6 +256,7 @@ TEST(MimeUtilTest, TestParseMimeType) {
// Must have slash in mime type.
"",
"ts",
"t / s",
}) {
EXPECT_FALSE(ParseMimeType(type_str, nullptr, nullptr));
}
Expand Down

0 comments on commit f89e0d4

Please sign in to comment.