Skip to content

Commit 994e191

Browse files
authored
std.Uri: fix parsing edge case panic
1 parent ea4a077 commit 994e191

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/std/Uri.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
176176

177177
var end_of_host: usize = authority.len;
178178

179+
// if we see `]` first without `@`
180+
if (authority[start_of_host] == ']') {
181+
return error.InvalidFormat;
182+
}
183+
179184
if (authority.len > start_of_host and authority[start_of_host] == '[') { // IPv6
180185
end_of_host = std.mem.lastIndexOf(u8, authority, "]") orelse return error.InvalidFormat;
181186
end_of_host += 1;
@@ -193,6 +198,7 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
193198
}
194199
}
195200

201+
if (start_of_host >= end_of_host) return error.InvalidFormat;
196202
uri.host = authority[start_of_host..end_of_host];
197203
}
198204

@@ -780,3 +786,9 @@ test "format" {
780786
try uri.format(":/?#", .{}, buf.writer());
781787
try std.testing.expectEqualSlices(u8, "file:/foo/bar/baz", buf.items);
782788
}
789+
790+
test "URI malformed input" {
791+
try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://]["));
792+
try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://]@["));
793+
try std.testing.expectError(error.InvalidFormat, std.Uri.parse("http://lo]s\x85hc@[/8\x10?0Q"));
794+
}

0 commit comments

Comments
 (0)