Skip to content

Commit

Permalink
std.Uri: fix parsing edge case panic
Browse files Browse the repository at this point in the history
  • Loading branch information
hspak authored Nov 22, 2023
1 parent ea4a077 commit 994e191
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/std/Uri.zig
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {

var end_of_host: usize = authority.len;

// if we see `]` first without `@`
if (authority[start_of_host] == ']') {
return error.InvalidFormat;
}

if (authority.len > start_of_host and authority[start_of_host] == '[') { // IPv6
end_of_host = std.mem.lastIndexOf(u8, authority, "]") orelse return error.InvalidFormat;
end_of_host += 1;
Expand All @@ -193,6 +198,7 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
}
}

if (start_of_host >= end_of_host) return error.InvalidFormat;
uri.host = authority[start_of_host..end_of_host];
}

Expand Down Expand Up @@ -780,3 +786,9 @@ test "format" {
try uri.format(":/?#", .{}, buf.writer());
try std.testing.expectEqualSlices(u8, "file:/foo/bar/baz", buf.items);
}

test "URI malformed input" {
try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://]["));
try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://]@["));
try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://lo]s\x85hc@[/8\x10?0Q"));
}

0 comments on commit 994e191

Please sign in to comment.