Skip to content

Commit 089bcfc

Browse files
committed
feat(proxy): validate proxy URL before saving setting
Add scheme and host validation to the proxy set command. Only http, https, socks5, socks5h, socks4, and socks4a schemes are accepted. Invalid URLs are rejected with a helpful error message listing supported formats. This prevents users from accidentally saving malformed proxy addresses that would silently fail at download time.
1 parent b842293 commit 089bcfc

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

src/command/proxy.zig

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55

66
const std = @import("std");
77
const zvm_mod = @import("../core/zvm.zig");
8+
const terminal = @import("../core/terminal.zig");
9+
10+
/// Supported proxy schemes.
11+
const valid_schemes = [_][]const u8{ "http://", "https://", "socks5://", "socks5h://", "socks4://", "socks4a://" };
12+
13+
/// Validate that a proxy URL has a supported scheme and a host component.
14+
fn validateProxyUrl(url: []const u8) !void {
15+
// Must have a recognized scheme
16+
var has_scheme = false;
17+
for (valid_schemes) |scheme| {
18+
if (std.mem.startsWith(u8, url, scheme)) {
19+
has_scheme = true;
20+
break;
21+
}
22+
}
23+
if (!has_scheme) return error.InvalidUrl;
24+
25+
// Must parse as a valid URI with a host
26+
const uri = std.Uri.parse(url) catch return error.InvalidUrl;
27+
if (uri.host == null) return error.InvalidUrl;
28+
}
829

930
/// Set or display the HTTP/HTTPS proxy.
1031
/// "default" clears the proxy (auto-detect from env vars).
@@ -16,13 +37,23 @@ pub fn run(
1637
stdout: *std.Io.Writer,
1738
stderr: *std.Io.Writer,
1839
) !void {
19-
_ = stderr;
20-
2140
if (url) |u| {
2241
if (std.mem.eql(u8, u, "default")) {
2342
try zvm.settings.setProxy(allocator, zvm.io, "");
2443
try stdout.print("Reset proxy to auto-detect (from environment).\n", .{});
2544
} else {
45+
validateProxyUrl(u) catch {
46+
try terminal.printError(stderr, "Invalid proxy URL");
47+
try stderr.print(
48+
\\Supported formats:
49+
\\ http://host:port
50+
\\ https://host:port
51+
\\ socks5://host:port
52+
\\
53+
, .{});
54+
try stderr.flush();
55+
return;
56+
};
2657
try zvm.settings.setProxy(allocator, zvm.io, u);
2758
try stdout.print("Set proxy to {s}\n", .{u});
2859
}

0 commit comments

Comments
 (0)