╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
802 if (std.mem.eql(u8, pair[0..eq], key)) { 803 return try decodeFormValue(allocator, pair[eq + 1 ..]); 804 } 805 } 806 return null; 807 } 808 809 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 810 var out: std.ArrayList(u8) = .empty; 811 errdefer out.deinit(allocator); 812 var i: usize = 0; 813 while (i < encoded.len) : (i += 1) { 814 switch (encoded[i]) { 815 '+' => try out.append(allocator, ' '), 816 '%' => { 817 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 818 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 819 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 820 try out.append(allocator, hi * 16 + lo); 821 i += 2; 822 }, 823 else => try out.append(allocator, encoded[i]), 824 } 825 } 826 return try out.toOwnedSlice(allocator); 827 } 828 829 // disk-backed web session store. each login mints a random, opaque token 830 // (not the user's id) that is stored as a file named by the token hex, whose 831 // contents are the raw user_id bytes. authenticating a request is a lookup of 832 // the cookie's token; logout deletes the file, revoking the session. there is 833 // deliberately no expiry — a session lives until logout. 834 pub const SessionStore = struct { 835 io: std.Io, 836 dir: std.Io.Dir, 837 838 pub const token_hex_len = evt.event_id_size * 2; 839 840 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 841 try data_dir.createDirPath(io, "sessions"); 842 const dir = try data_dir.openDir(io, "sessions", .{}); 843 return .{ .io = io, .dir = dir }; 844 } 845 846 pub fn deinit(self: SessionStore) void { 847 self.dir.close(self.io); 848 } 849 850 // mint a new session for user_id, returning the token hex to set as the 851 // cookie value. 852 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 853 var token: [evt.event_id_size]u8 = undefined; 854 self.io.random(&token); 855 const token_hex = std.fmt.bytesToHex(token, .lower); 856 const file = try self.dir.createFile(self.io, &token_hex, .{}); 857 defer file.close(self.io); 858 try file.writeStreamingAll(self.io, user_id); 859 return token_hex; 860 } 861 862 // resolve a cookie's token to its user_id. returns false (logged out) for a 863 // missing, malformed, or unknown token. 864 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 865 if (!isToken(token_hex)) return false; 866 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 867 defer file.close(self.io); 868 var storage: [evt.event_id_size]u8 = undefined; 869 var file_reader = file.reader(self.io, &storage); 870 file_reader.interface.readSliceAll(out) catch return false; 871 return true; 872 } 873 874 // revoke a session. a no-op for a missing or malformed token. 875 pub fn remove(self: SessionStore, token_hex: []const u8) void { 876 if (!isToken(token_hex)) return; 877 self.dir.deleteFile(self.io, token_hex) catch {}; 878 } 879 880 // a token is exactly token_hex_len hex chars. validating before using the 881 // value as a path keeps attacker-supplied cookies from escaping the 882 // sessions dir (e.g. via "/" or ".." bytes). 883 fn isToken(token_hex: []const u8) bool { 884 if (token_hex.len != token_hex_len) return false; 885 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 886 return true; 887 } 888 }; 889