Make sure a potential port component is considered during hostname validation

This commit is contained in:
Kristófer R
2024-10-28 00:50:24 -04:00
parent e26847ccd4
commit 892dc27896

View File

@ -1057,13 +1057,36 @@ pub const StreamHandler = struct {
// Get the raw string of the URI. Its unclear to me if the various
// tags of this enum guarantee no percent-encoding so we just
// check all of it. This isn't a performance critical path.
const host = switch (host_component) {
.raw => |v| v,
.percent_encoded => |v| v,
const host = host: {
const h = switch (host_component) {
.raw => |v| v,
.percent_encoded => |v| v,
};
if (h.len == 0 or std.mem.eql(u8, "localhost", h)) {
break :host_valid true;
}
// When the "Private Wi-Fi address" setting is toggled on macOS the hostname
// is set to a string of digits separated by a colon, e.g. '12:34:56:78:90:12'.
// The URI will be parsed as if the last set o digit is a port, so we need to
// make sure that part is included when it's set.
if (uri.port) |port| {
// 65_535 is considered the highest port number on Linux.
const PORT_NUMBER_MAX_DIGITS = 5;
// Make sure there is space for a max length hostname + the max number of digits.
var host_and_port_buf: [posix.HOST_NAME_MAX + PORT_NUMBER_MAX_DIGITS]u8 = undefined;
const host_and_port = std.fmt.bufPrint(&host_and_port_buf, "{s}:{d}", .{ h, port }) catch |err| {
log.warn("failed to get full hostname for OSC 7 validation: {}", .{err});
break :host_valid false;
};
break :host host_and_port;
} else {
break :host h;
}
};
if (host.len == 0 or std.mem.eql(u8, "localhost", host)) {
break :host_valid true;
}
// Otherwise, it must match our hostname.
var buf: [posix.HOST_NAME_MAX]u8 = undefined;