╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2697 var i: usize = 0; 2698 while (i < encoded.len) : (i += 1) { 2699 switch (encoded[i]) { 2700 '+' => try out.append(allocator, ' '), 2701 '%' => { 2702 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 2703 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 2704 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 2705 try out.append(allocator, hi * 16 + lo); 2706 i += 2; 2707 }, 2708 else => try out.append(allocator, encoded[i]), 2709 } 2710 } 2711 return try out.toOwnedSlice(allocator); 2712 } 2713 2714 // disk-backed web session store. each login mints a random, opaque token 2715 // (not the user's id) that is stored as a file named by the token hex, whose 2716 // contents are the raw user_id bytes. authenticating a request is a lookup of 2717 // the cookie's token; logout deletes the file, revoking the session. there is 2718 // deliberately no expiry — a session lives until logout. form failures use 2719 // files in the flash subdirectory that the redirected GET consumes. the 2720 // directory is cleared whenever the server starts. 2721 pub const SessionStore = struct { 2722 io: std.Io, 2723 dir: std.Io.Dir, 2724 flash_dir: std.Io.Dir, 2725 auto_login: ?[token_hex_len]u8, 2726 2727 pub const token_hex_len = evt.event_id_size * 2; 2728 2729 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2730 try data_dir.createDirPath(io, "sessions"); 2731 const dir = try data_dir.openDir(io, "sessions", .{}); 2732 errdefer dir.close(io); 2733 try dir.deleteTree(io, "flash"); 2734 try dir.createDirPath(io, "flash"); 2735 const flash_dir = try dir.openDir(io, "flash", .{}); 2736 errdefer flash_dir.close(io); 2737 const auto_login = blk: { 2738 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2739 defer file.close(io); 2740 var token: [token_hex_len]u8 = undefined; 2741 var storage: [token_hex_len]u8 = undefined; 2742 var reader = file.reader(io, &storage); 2743 reader.interface.readSliceAll(&token) catch break :blk null; 2744 break :blk token; 2745 }; 2746 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2747 } 2748 2749 pub fn deinit(self: SessionStore) void { 2750 self.flash_dir.close(self.io); 2751 self.dir.close(self.io); 2752 } 2753 2754 // mint a new session for user_id, returning the token hex to set as the 2755 // cookie value. 2756 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2757 var token: [evt.event_id_size]u8 = undefined; 2758 self.io.random(&token); 2759 const token_hex = std.fmt.bytesToHex(token, .lower); 2760 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2761 defer file.close(self.io); 2762 try file.writeStreamingAll(self.io, user_id); 2763 return token_hex; 2764 } 2765 2766 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2767 var token: [evt.event_id_size]u8 = undefined; 2768 self.io.random(&token); 2769 const token_hex = std.fmt.bytesToHex(token, .lower); 2770 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2771 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2772 defer file.close(self.io); 2773 2774 var json: std.Io.Writer.Allocating = .init(allocator); 2775 defer json.deinit(); 2776 try std.json.Stringify.value(value, .{}, &json.writer); 2777 try file.writeStreamingAll(self.io, json.written()); 2778 return token_hex; 2779 } 2780 2781 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2782 if (!isToken(token_hex)) return null; 2783 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2784 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2785 defer file.close(self.io); 2786 2787 var storage: [4096]u8 = undefined; 2788 var reader = file.reader(self.io, &storage); 2789 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2790 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2791 } 2792 2793 // resolve a cookie's token to its user_id. returns false (logged out) for a 2794 // missing, malformed, or unknown token. 2795 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2796 if (!isToken(token_hex)) return false; 2797 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2798 defer file.close(self.io); 2799 var storage: [evt.event_id_size]u8 = undefined; 2800 var file_reader = file.reader(self.io, &storage); 2801 file_reader.interface.readSliceAll(out) catch return false; 2802 return true; 2803 } 2804 2805 // revoke a session. a no-op for a missing or malformed token. 2806 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2807 if (!isToken(token_hex)) return; 2808 self.dir.deleteFile(self.io, token_hex) catch {}; 2809 } 2810 2811 // leave `token_hex`'s session for visitors arriving without one. only the 2812 // `try` fixture writes one. the name can't collide with a session token. 2813 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2814 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2815 defer file.close(self.io); 2816 try file.writeStreamingAll(self.io, token_hex); 2817 } 2818 2819 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2820 out.* = self.auto_login orelse return false; 2821 return true; 2822 } 2823 2824 const auto_login_name = "auto-login"; 2825 2826 // a token is exactly token_hex_len hex chars. validating before using the 2827 // value as a path keeps attacker-supplied cookies from escaping the 2828 // sessions dir (e.g. via "/" or ".." bytes). 2829 fn isToken(token_hex: []const u8) bool { 2830 if (token_hex.len != token_hex_len) return false; 2831 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2832 return true; 2833 } 2834 }; 2835