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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Handle - AT Protocol Handle Identifier
//!
//! handles are domain-name based identifiers for accounts.
//! format: <segment>.<segment>...<tld>
//!
//! validation rules:
//! - max 253 characters
//! - ASCII only (a-z, 0-9, hyphen)
//! - 2+ segments separated by dots
//! - each segment: 1-63 chars, no leading/trailing hyphens
//! - final segment (TLD) cannot start with a digit
//! - case-insensitive, normalize to lowercase
//!
//! see: https://atproto.com/specs/handle
const std = @import("std");
pub const Handle = struct {
/// the handle string (borrowed, not owned)
raw: []const u8,
pub const max_length = 253;
/// parse a handle string. returns null if invalid.
pub fn parse(s: []const u8) ?Handle {
if (!isValid(s)) return null;
return .{ .raw = s };
}
/// validate a handle string without allocating
pub fn isValid(s: []const u8) bool {
// length check
if (s.len == 0 or s.len > max_length) return false;
// must be ascii
for (s) |c| {
if (c > 127) return false;
}
var segment_count: usize = 0;
var segment_start: usize = 0;
var last_segment_start: usize = 0;
for (s, 0..) |c, i| {
if (c == '.') {
const segment = s[segment_start..i];
if (!isValidSegment(segment)) return false;
segment_count += 1;
last_segment_start = i + 1;
segment_start = i + 1;
}
}
// validate final segment (TLD)
const tld = s[last_segment_start..];
if (!isValidSegment(tld)) return false;
if (!isValidTld(tld)) return false;
segment_count += 1;
// must have at least 2 segments
return segment_count >= 2;
}
/// get the handle string
pub fn str(self: Handle) []const u8 {
return self.raw;
}
/// the spec's initial list of disallowed TLDs: syntactically valid, but a
/// handle under one must fail registration and resolution. `.test` is not
/// here; the spec leaves it usable in development.
pub const disallowed_tlds = [_][]const u8{
"alt", "arpa", "example", "internal", "invalid", "local", "localhost", "onion",
};
/// the final segment, as written (not lowercased)
pub fn topLevelDomain(self: Handle) []const u8 {
const dot = std.mem.lastIndexOfScalar(u8, self.raw, '.').?;
return self.raw[dot + 1 ..];
}
/// whether the handle's TLD is one the spec disallows, ignoring case.
/// separate from `parse`, which accepts these: lexicon validation must
/// not reject them, while registration and resolution must.
pub fn hasDisallowedTld(self: Handle) bool {
const t = self.topLevelDomain();
for (disallowed_tlds) |disallowed| {
if (std.ascii.eqlIgnoreCase(t, disallowed)) return true;
}
return false;
}
fn isValidSegment(seg: []const u8) bool {
// 1-63 characters
if (seg.len == 0 or seg.len > 63) return false;
// cannot start or end with hyphen
if (seg[0] == '-' or seg[seg.len - 1] == '-') return false;
// only alphanumeric and hyphen
for (seg) |c| {
const valid = (c >= 'a' and c <= 'z') or
(c >= 'A' and c <= 'Z') or
(c >= '0' and c <= '9') or
c == '-';
if (!valid) return false;
}
return true;
}
fn isValidTld(tld: []const u8) bool {
if (tld.len == 0) return false;
// TLD cannot start with a digit
const first = tld[0];
return (first >= 'a' and first <= 'z') or (first >= 'A' and first <= 'Z');
}
};
// === tests from atproto.com/specs/handle ===
test "valid: simple handle" {
try std.testing.expect(Handle.parse("jay.bsky.social") != null);
try std.testing.expect(Handle.parse("alice.example.com") != null);
}
test "valid: two segments" {
try std.testing.expect(Handle.parse("example.com") != null);
try std.testing.expect(Handle.parse("test.org") != null);
}
test "valid: many segments" {
try std.testing.expect(Handle.parse("a.b.c.d.e.f") != null);
}
test "valid: with hyphens" {
try std.testing.expect(Handle.parse("my-name.example.com") != null);
try std.testing.expect(Handle.parse("test.my-domain.org") != null);
}
test "valid: with numbers" {
try std.testing.expect(Handle.parse("user123.example.com") != null);
try std.testing.expect(Handle.parse("123user.example.com") != null);
}
test "valid: uppercase (allowed, normalize to lowercase)" {
try std.testing.expect(Handle.parse("LOUD.example.com") != null);
try std.testing.expect(Handle.parse("Jay.Bsky.Social") != null);
}
test "invalid: single segment" {
try std.testing.expect(Handle.parse("example") == null);
try std.testing.expect(Handle.parse("localhost") == null);
}
test "invalid: TLD starts with digit" {
try std.testing.expect(Handle.parse("john.0") == null);
try std.testing.expect(Handle.parse("test.123") == null);
}
test "invalid: segment starts with hyphen" {
try std.testing.expect(Handle.parse("-test.example.com") == null);
try std.testing.expect(Handle.parse("test.-example.com") == null);
}
test "invalid: segment ends with hyphen" {
try std.testing.expect(Handle.parse("test-.example.com") == null);
try std.testing.expect(Handle.parse("test.example-.com") == null);
}
test "invalid: empty segment" {
try std.testing.expect(Handle.parse(".example.com") == null);
try std.testing.expect(Handle.parse("test..com") == null);
try std.testing.expect(Handle.parse("test.example.") == null);
}
test "invalid: trailing dot" {
try std.testing.expect(Handle.parse("example.com.") == null);
}
test "invalid: invalid characters" {
try std.testing.expect(Handle.parse("test_name.example.com") == null);
try std.testing.expect(Handle.parse("test@name.example.com") == null);
try std.testing.expect(Handle.parse("test name.example.com") == null);
}
test "invalid: non-ascii" {
try std.testing.expect(Handle.parse("tëst.example.com") == null);
}
test "invalid: too long" {
// create a handle longer than 253 chars
var buf: [300]u8 = undefined;
@memset(&buf, 'a');
buf[100] = '.';
buf[200] = '.';
@memcpy(buf[201..204], "com");
try std.testing.expect(Handle.parse(buf[0..254]) == null);
}
test "invalid: segment too long" {
// segment > 63 chars
var buf: [100]u8 = undefined;
@memset(&buf, 'a');
buf[64] = '.';
@memcpy(buf[65..68], "com");
try std.testing.expect(Handle.parse(buf[0..68]) == null);
}
test "disallowed TLDs parse but are flagged" {
for (Handle.disallowed_tlds) |t| {
var buf: [32]u8 = undefined;
const text = try std.fmt.bufPrint(&buf, "alice.{s}", .{t});
const handle = Handle.parse(text).?;
try std.testing.expect(handle.hasDisallowedTld());
}
try std.testing.expect(Handle.parse("handle.invalid").?.hasDisallowedTld());
try std.testing.expect(Handle.parse("printer.LOCAL").?.hasDisallowedTld());
try std.testing.expect(Handle.parse("a.b.Onion").?.hasDisallowedTld());
try std.testing.expectEqualStrings("LOCAL", Handle.parse("printer.LOCAL").?.topLevelDomain());
}
test "allowed TLDs, .test and look-alike suffixes are not flagged" {
const allowed = [_][]const u8{
"jay.bsky.social",
"alice.test",
"local.example.com", // disallowed word in a non-final segment
"foo.onions",
"foo.myalt",
"foo.xn--local",
"name.t--t",
};
for (allowed) |text| try std.testing.expect(!Handle.parse(text).?.hasDisallowedTld());
}