╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2232 while (i < encoded.len) : (i += 1) { 2233 switch (encoded[i]) { 2234 '+' => try out.append(allocator, ' '), 2235 '%' => { 2236 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 2237 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 2238 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 2239 try out.append(allocator, hi * 16 + lo); 2240 i += 2; 2241 }, 2242 else => try out.append(allocator, encoded[i]), 2243 } 2244 } 2245 return try out.toOwnedSlice(allocator); 2246 } 2247 2248 // disk-backed web session store. each login mints a random, opaque token 2249 // (not the user's id) that is stored as a file named by the token hex, whose 2250 // contents are the raw user_id bytes. authenticating a request is a lookup of 2251 // the cookie's token; logout deletes the file, revoking the session. there is 2252 // deliberately no expiry — a session lives until logout. form failures use 2253 // files in the flash subdirectory that the redirected GET consumes. the 2254 // directory is cleared whenever the server starts. 2255 pub const SessionStore = struct { 2256 io: std.Io, 2257 dir: std.Io.Dir, 2258 flash_dir: std.Io.Dir, 2259 auto_login: ?[token_hex_len]u8, 2260 2261 pub const token_hex_len = evt.event_id_size * 2; 2262 2263 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2264 try data_dir.createDirPath(io, "sessions"); 2265 const dir = try data_dir.openDir(io, "sessions", .{}); 2266 errdefer dir.close(io); 2267 try dir.deleteTree(io, "flash"); 2268 try dir.createDirPath(io, "flash"); 2269 const flash_dir = try dir.openDir(io, "flash", .{}); 2270 errdefer flash_dir.close(io); 2271 const auto_login = blk: { 2272 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2273 defer file.close(io); 2274 var token: [token_hex_len]u8 = undefined; 2275 var storage: [token_hex_len]u8 = undefined; 2276 var reader = file.reader(io, &storage); 2277 reader.interface.readSliceAll(&token) catch break :blk null; 2278 break :blk token; 2279 }; 2280 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2281 } 2282 2283 pub fn deinit(self: SessionStore) void { 2284 self.flash_dir.close(self.io); 2285 self.dir.close(self.io); 2286 } 2287 2288 // mint a new session for user_id, returning the token hex to set as the 2289 // cookie value. 2290 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2291 var token: [evt.event_id_size]u8 = undefined; 2292 self.io.random(&token); 2293 const token_hex = std.fmt.bytesToHex(token, .lower); 2294 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2295 defer file.close(self.io); 2296 try file.writeStreamingAll(self.io, user_id); 2297 return token_hex; 2298 } 2299 2300 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2301 var token: [evt.event_id_size]u8 = undefined; 2302 self.io.random(&token); 2303 const token_hex = std.fmt.bytesToHex(token, .lower); 2304 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2305 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2306 defer file.close(self.io); 2307 2308 var json: std.Io.Writer.Allocating = .init(allocator); 2309 defer json.deinit(); 2310 try std.json.Stringify.value(value, .{}, &json.writer); 2311 try file.writeStreamingAll(self.io, json.written()); 2312 return token_hex; 2313 } 2314 2315 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2316 if (!isToken(token_hex)) return null; 2317 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2318 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2319 defer file.close(self.io); 2320 2321 var storage: [4096]u8 = undefined; 2322 var reader = file.reader(self.io, &storage); 2323 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2324 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2325 } 2326 2327 // resolve a cookie's token to its user_id. returns false (logged out) for a 2328 // missing, malformed, or unknown token. 2329 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2330 if (!isToken(token_hex)) return false; 2331 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2332 defer file.close(self.io); 2333 var storage: [evt.event_id_size]u8 = undefined; 2334 var file_reader = file.reader(self.io, &storage); 2335 file_reader.interface.readSliceAll(out) catch return false; 2336 return true; 2337 } 2338 2339 // revoke a session. a no-op for a missing or malformed token. 2340 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2341 if (!isToken(token_hex)) return; 2342 self.dir.deleteFile(self.io, token_hex) catch {}; 2343 } 2344 2345 // leave `token_hex`'s session for visitors arriving without one. only the 2346 // `try` fixture writes one. the name can't collide with a session token. 2347 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2348 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2349 defer file.close(self.io); 2350 try file.writeStreamingAll(self.io, token_hex); 2351 } 2352 2353 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2354 out.* = self.auto_login orelse return false; 2355 return true; 2356 } 2357 2358 const auto_login_name = "auto-login"; 2359 2360 // a token is exactly token_hex_len hex chars. validating before using the 2361 // value as a path keeps attacker-supplied cookies from escaping the 2362 // sessions dir (e.g. via "/" or ".." bytes). 2363 fn isToken(token_hex: []const u8) bool { 2364 if (token_hex.len != token_hex_len) return false; 2365 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2366 return true; 2367 } 2368 }; 2369