╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2613 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 2614 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 2615 try out.append(allocator, hi * 16 + lo); 2616 i += 2; 2617 }, 2618 else => try out.append(allocator, encoded[i]), 2619 } 2620 } 2621 return try out.toOwnedSlice(allocator); 2622 } 2623 2624 // disk-backed web session store. each login mints a random, opaque token 2625 // (not the user's id) that is stored as a file named by the token hex, whose 2626 // contents are the raw user_id bytes. authenticating a request is a lookup of 2627 // the cookie's token; logout deletes the file, revoking the session. there is 2628 // deliberately no expiry — a session lives until logout. form failures use 2629 // files in the flash subdirectory that the redirected GET consumes. the 2630 // directory is cleared whenever the server starts. 2631 pub const SessionStore = struct { 2632 io: std.Io, 2633 dir: std.Io.Dir, 2634 flash_dir: std.Io.Dir, 2635 auto_login: ?[token_hex_len]u8, 2636 2637 pub const token_hex_len = evt.event_id_size * 2; 2638 2639 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2640 try data_dir.createDirPath(io, "sessions"); 2641 const dir = try data_dir.openDir(io, "sessions", .{}); 2642 errdefer dir.close(io); 2643 try dir.deleteTree(io, "flash"); 2644 try dir.createDirPath(io, "flash"); 2645 const flash_dir = try dir.openDir(io, "flash", .{}); 2646 errdefer flash_dir.close(io); 2647 const auto_login = blk: { 2648 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2649 defer file.close(io); 2650 var token: [token_hex_len]u8 = undefined; 2651 var storage: [token_hex_len]u8 = undefined; 2652 var reader = file.reader(io, &storage); 2653 reader.interface.readSliceAll(&token) catch break :blk null; 2654 break :blk token; 2655 }; 2656 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2657 } 2658 2659 pub fn deinit(self: SessionStore) void { 2660 self.flash_dir.close(self.io); 2661 self.dir.close(self.io); 2662 } 2663 2664 // mint a new session for user_id, returning the token hex to set as the 2665 // cookie value. 2666 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2667 var token: [evt.event_id_size]u8 = undefined; 2668 self.io.random(&token); 2669 const token_hex = std.fmt.bytesToHex(token, .lower); 2670 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2671 defer file.close(self.io); 2672 try file.writeStreamingAll(self.io, user_id); 2673 return token_hex; 2674 } 2675 2676 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2677 var token: [evt.event_id_size]u8 = undefined; 2678 self.io.random(&token); 2679 const token_hex = std.fmt.bytesToHex(token, .lower); 2680 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2681 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2682 defer file.close(self.io); 2683 2684 var json: std.Io.Writer.Allocating = .init(allocator); 2685 defer json.deinit(); 2686 try std.json.Stringify.value(value, .{}, &json.writer); 2687 try file.writeStreamingAll(self.io, json.written()); 2688 return token_hex; 2689 } 2690 2691 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2692 if (!isToken(token_hex)) return null; 2693 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2694 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2695 defer file.close(self.io); 2696 2697 var storage: [4096]u8 = undefined; 2698 var reader = file.reader(self.io, &storage); 2699 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2700 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2701 } 2702 2703 // resolve a cookie's token to its user_id. returns false (logged out) for a 2704 // missing, malformed, or unknown token. 2705 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2706 if (!isToken(token_hex)) return false; 2707 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2708 defer file.close(self.io); 2709 var storage: [evt.event_id_size]u8 = undefined; 2710 var file_reader = file.reader(self.io, &storage); 2711 file_reader.interface.readSliceAll(out) catch return false; 2712 return true; 2713 } 2714 2715 // revoke a session. a no-op for a missing or malformed token. 2716 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2717 if (!isToken(token_hex)) return; 2718 self.dir.deleteFile(self.io, token_hex) catch {}; 2719 } 2720 2721 // leave `token_hex`'s session for visitors arriving without one. only the 2722 // `try` fixture writes one. the name can't collide with a session token. 2723 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2724 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2725 defer file.close(self.io); 2726 try file.writeStreamingAll(self.io, token_hex); 2727 } 2728 2729 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2730 out.* = self.auto_login orelse return false; 2731 return true; 2732 } 2733 2734 const auto_login_name = "auto-login"; 2735 2736 // a token is exactly token_hex_len hex chars. validating before using the 2737 // value as a path keeps attacker-supplied cookies from escaping the 2738 // sessions dir (e.g. via "/" or ".." bytes). 2739 fn isToken(token_hex: []const u8) bool { 2740 if (token_hex.len != token_hex_len) return false; 2741 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2742 return true; 2743 } 2744 }; 2745