1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! hostname — relay host parsing & validation.
//!
//! mirrors indigo relay's ParseHostname rules (reject IPs, ports, localhost,
//! malformed DNS names). this is relay policy, not generic DNS validation,
//! which is why it lives under atproto/ rather than util/.
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const HostValidationError = error{
EmptyHostname,
InvalidCharacter,
InvalidLabel,
TooFewLabels,
LooksLikeIpAddress,
PortNotAllowed,
LocalhostNotAllowed,
DomainBanned,
HostUnreachable,
NotAPds,
IsARelay,
};
/// validate and normalize a hostname for crawling.
/// rejects IPs, ports, localhost, invalid DNS names.
/// returns lowercased hostname on success (caller owns the allocation).
pub fn validateHostname(allocator: Allocator, raw: []const u8) HostValidationError![]u8 {
if (raw.len == 0) return error.EmptyHostname;
// strip scheme if present
var hostname = raw;
for ([_][]const u8{ "https://", "http://", "wss://", "ws://" }) |scheme| {
if (hostname.len > scheme.len and std.ascii.startsWithIgnoreCase(hostname, scheme)) {
hostname = hostname[scheme.len..];
break;
}
}
// strip trailing slash and path
if (std.mem.indexOfScalar(u8, hostname, '/')) |i| {
hostname = hostname[0..i];
}
// reject ports (Go relay rejects non-localhost ports)
if (std.mem.indexOfScalar(u8, hostname, ':')) |_| {
return error.PortNotAllowed;
}
// reject localhost
if (std.ascii.eqlIgnoreCase(hostname, "localhost")) {
return error.LocalhostNotAllowed;
}
// validate characters and split into labels
var label_count: usize = 0;
var all_labels_numeric = true;
var it = std.mem.splitScalar(u8, hostname, '.');
while (it.next()) |label| {
if (label.len == 0 or label.len > 63) return error.InvalidLabel;
// labels must be alphanumeric with hyphens, no leading/trailing hyphens
if (label[0] == '-' or label[label.len - 1] == '-') return error.InvalidLabel;
var is_numeric = true;
for (label) |c| {
if (!std.ascii.isAlphanumeric(c) and c != '-') return error.InvalidCharacter;
if (!std.ascii.isDigit(c)) is_numeric = false;
}
if (!is_numeric) all_labels_numeric = false;
label_count += 1;
}
// need at least 2 labels (e.g. "pds.example.com", "bsky.network")
if (label_count < 2) return error.TooFewLabels;
// all-numeric labels = IP address (e.g. "192.168.1.1")
if (all_labels_numeric) return error.LooksLikeIpAddress;
// lowercase normalize
const result = allocator.alloc(u8, hostname.len) catch return error.EmptyHostname;
for (hostname, 0..) |c, i| {
result[i] = std.ascii.toLower(c);
}
return result;
}
/// extract hostname from a URL like "https://pds.example.com" or
/// "https://pds.example.com:443/path". borrows from `url` — no allocation.
pub fn extractHostFromUrl(url: []const u8) ?[]const u8 {
// strip scheme
var rest = url;
if (std.mem.startsWith(u8, rest, "https://")) {
rest = rest["https://".len..];
} else if (std.mem.startsWith(u8, rest, "http://")) {
rest = rest["http://".len..];
}
// strip path
if (std.mem.indexOfScalar(u8, rest, '/')) |i| {
rest = rest[0..i];
}
// strip port
if (std.mem.indexOfScalar(u8, rest, ':')) |i| {
rest = rest[0..i];
}
if (rest.len == 0) return null;
return rest;
}
// --- tests ---
test "validateHostname accepts valid PDS hostnames" {
const alloc = std.testing.allocator;
const h1 = try validateHostname(alloc, "pds.example.com");
defer alloc.free(h1);
try std.testing.expectEqualStrings("pds.example.com", h1);
const h2 = try validateHostname(alloc, "bsky.network");
defer alloc.free(h2);
try std.testing.expectEqualStrings("bsky.network", h2);
// lowercases
const h3 = try validateHostname(alloc, "PDS.Example.COM");
defer alloc.free(h3);
try std.testing.expectEqualStrings("pds.example.com", h3);
// strips scheme
const h4 = try validateHostname(alloc, "https://pds.example.com");
defer alloc.free(h4);
try std.testing.expectEqualStrings("pds.example.com", h4);
// strips path
const h5 = try validateHostname(alloc, "pds.example.com/some/path");
defer alloc.free(h5);
try std.testing.expectEqualStrings("pds.example.com", h5);
}
test "validateHostname rejects invalid hostnames" {
const alloc = std.testing.allocator;
try std.testing.expectError(error.EmptyHostname, validateHostname(alloc, ""));
try std.testing.expectError(error.LocalhostNotAllowed, validateHostname(alloc, "localhost"));
try std.testing.expectError(error.TooFewLabels, validateHostname(alloc, "intranet"));
try std.testing.expectError(error.LooksLikeIpAddress, validateHostname(alloc, "192.168.1.1"));
try std.testing.expectError(error.LooksLikeIpAddress, validateHostname(alloc, "10.0.0.1"));
try std.testing.expectError(error.PortNotAllowed, validateHostname(alloc, "pds.example.com:443"));
try std.testing.expectError(error.InvalidCharacter, validateHostname(alloc, "pds.exam ple.com"));
try std.testing.expectError(error.InvalidCharacter, validateHostname(alloc, "pds.exam_ple.com"));
try std.testing.expectError(error.InvalidLabel, validateHostname(alloc, "-pds.example.com"));
try std.testing.expectError(error.InvalidLabel, validateHostname(alloc, "pds-.example.com"));
try std.testing.expectError(error.InvalidLabel, validateHostname(alloc, "pds..example.com"));
}
test "extractHostFromUrl strips scheme, path, and port" {
try std.testing.expectEqualStrings("pds.example.com", extractHostFromUrl("https://pds.example.com").?);
try std.testing.expectEqualStrings("pds.example.com", extractHostFromUrl("https://pds.example.com:443/x").?);
try std.testing.expectEqualStrings("pds.example.com", extractHostFromUrl("http://pds.example.com/path").?);
try std.testing.expect(extractHostFromUrl("https://") == null);
}