╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1162 while (pairs.next()) |pair| { 1163 const trimmed = std.mem.trim(u8, pair, " \t"); 1164 const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; 1165 if (std.mem.eql(u8, trimmed[0..eq], name)) { 1166 return trimmed[eq + 1 ..]; 1167 } 1168 } 1169 } 1170 return null; 1171 } 1172 1173 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 1174 var iter = std.mem.splitScalar(u8, body, '&'); 1175 while (iter.next()) |pair| { 1176 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 1177 if (std.mem.eql(u8, pair[0..eq], key)) { 1178 return try decodeFormValue(allocator, pair[eq + 1 ..]); 1179 } 1180 } 1181 return null; 1182 } 1183 1184 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 1185 var out: std.ArrayList(u8) = .empty; 1186 errdefer out.deinit(allocator); 1187 var i: usize = 0; 1188 while (i < encoded.len) : (i += 1) { 1189 switch (encoded[i]) { 1190 '+' => try out.append(allocator, ' '), 1191 '%' => { 1192 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 1193 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 1194 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 1195 try out.append(allocator, hi * 16 + lo); 1196 i += 2; 1197 }, 1198 else => try out.append(allocator, encoded[i]), 1199 } 1200 } 1201 return try out.toOwnedSlice(allocator); 1202 } 1203 1204 // disk-backed web session store. each login mints a random, opaque token 1205 // (not the user's id) that is stored as a file named by the token hex, whose 1206 // contents are the raw user_id bytes. authenticating a request is a lookup of 1207 // the cookie's token; logout deletes the file, revoking the session. there is 1208 // deliberately no expiry — a session lives until logout. 1209 pub const SessionStore = struct { 1210 io: std.Io, 1211 dir: std.Io.Dir, 1212 1213 pub const token_hex_len = evt.event_id_size * 2; 1214 1215 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 1216 try data_dir.createDirPath(io, "sessions"); 1217 const dir = try data_dir.openDir(io, "sessions", .{}); 1218 return .{ .io = io, .dir = dir }; 1219 } 1220 1221 pub fn deinit(self: SessionStore) void { 1222 self.dir.close(self.io); 1223 } 1224 1225 // mint a new session for user_id, returning the token hex to set as the 1226 // cookie value. 1227 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 1228 var token: [evt.event_id_size]u8 = undefined; 1229 self.io.random(&token); 1230 const token_hex = std.fmt.bytesToHex(token, .lower); 1231 const file = try self.dir.createFile(self.io, &token_hex, .{}); 1232 defer file.close(self.io); 1233 try file.writeStreamingAll(self.io, user_id); 1234 return token_hex; 1235 } 1236 1237 // resolve a cookie's token to its user_id. returns false (logged out) for a 1238 // missing, malformed, or unknown token. 1239 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 1240 if (!isToken(token_hex)) return false; 1241 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 1242 defer file.close(self.io); 1243 var storage: [evt.event_id_size]u8 = undefined; 1244 var file_reader = file.reader(self.io, &storage); 1245 file_reader.interface.readSliceAll(out) catch return false; 1246 return true; 1247 } 1248 1249 // revoke a session. a no-op for a missing or malformed token. 1250 pub fn remove(self: SessionStore, token_hex: []const u8) void { 1251 if (!isToken(token_hex)) return; 1252 self.dir.deleteFile(self.io, token_hex) catch {}; 1253 } 1254 1255 // leave `token_hex`'s session for the next visitor arriving without one. 1256 // only the `try` fixture writes one, and only a debug build honors it. the 1257 // name isn't token-shaped, so it can't collide with a session file. 1258 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 1259 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 1260 defer file.close(self.io); 1261 try file.writeStreamingAll(self.io, token_hex); 1262 } 1263 1264 // claim the offered session, if one is there. the delete picks the winner 1265 // of a race, so the offer is only ever taken once. a release build never 1266 // looks, because that would be an extra syscall for every GET request. 1267 pub fn claimAutoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 1268 if (builtin.mode != .Debug) return false; 1269 { 1270 const file = self.dir.openFile(self.io, auto_login_name, .{ .mode = .read_only }) catch return false; 1271 defer file.close(self.io); 1272 var storage: [token_hex_len]u8 = undefined; 1273 var file_reader = file.reader(self.io, &storage); 1274 file_reader.interface.readSliceAll(out) catch return false; 1275 } 1276 self.dir.deleteFile(self.io, auto_login_name) catch return false; 1277 return true; 1278 } 1279 1280 const auto_login_name = "auto-login"; 1281 1282 // a token is exactly token_hex_len hex chars. validating before using the 1283 // value as a path keeps attacker-supplied cookies from escaping the 1284 // sessions dir (e.g. via "/" or ".." bytes). 1285 fn isToken(token_hex: []const u8) bool { 1286 if (token_hex.len != token_hex_len) return false; 1287 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 1288 return true; 1289 } 1290 }; 1291