╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
882 if (!std.ascii.eqlIgnoreCase(header.name, "cookie")) continue; 883 var pairs = std.mem.splitScalar(u8, header.value, ';'); 884 while (pairs.next()) |pair| { 885 const trimmed = std.mem.trim(u8, pair, " \t"); 886 const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; 887 if (std.mem.eql(u8, trimmed[0..eq], name)) { 888 return trimmed[eq + 1 ..]; 889 } 890 } 891 } 892 return null; 893 } 894 895 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 896 var iter = std.mem.splitScalar(u8, body, '&'); 897 while (iter.next()) |pair| { 898 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 899 if (std.mem.eql(u8, pair[0..eq], key)) { 900 return try decodeFormValue(allocator, pair[eq + 1 ..]); 901 } 902 } 903 return null; 904 } 905 906 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 907 var out: std.ArrayList(u8) = .empty; 908 errdefer out.deinit(allocator); 909 var i: usize = 0; 910 while (i < encoded.len) : (i += 1) { 911 switch (encoded[i]) { 912 '+' => try out.append(allocator, ' '), 913 '%' => { 914 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 915 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 916 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 917 try out.append(allocator, hi * 16 + lo); 918 i += 2; 919 }, 920 else => try out.append(allocator, encoded[i]), 921 } 922 } 923 return try out.toOwnedSlice(allocator); 924 } 925 926 // disk-backed web session store. each login mints a random, opaque token 927 // (not the user's id) that is stored as a file named by the token hex, whose 928 // contents are the raw user_id bytes. authenticating a request is a lookup of 929 // the cookie's token; logout deletes the file, revoking the session. there is 930 // deliberately no expiry — a session lives until logout. 931 pub const SessionStore = struct { 932 io: std.Io, 933 dir: std.Io.Dir, 934 935 pub const token_hex_len = evt.event_id_size * 2; 936 937 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 938 try data_dir.createDirPath(io, "sessions"); 939 const dir = try data_dir.openDir(io, "sessions", .{}); 940 return .{ .io = io, .dir = dir }; 941 } 942 943 pub fn deinit(self: SessionStore) void { 944 self.dir.close(self.io); 945 } 946 947 // mint a new session for user_id, returning the token hex to set as the 948 // cookie value. 949 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 950 var token: [evt.event_id_size]u8 = undefined; 951 self.io.random(&token); 952 const token_hex = std.fmt.bytesToHex(token, .lower); 953 const file = try self.dir.createFile(self.io, &token_hex, .{}); 954 defer file.close(self.io); 955 try file.writeStreamingAll(self.io, user_id); 956 return token_hex; 957 } 958 959 // resolve a cookie's token to its user_id. returns false (logged out) for a 960 // missing, malformed, or unknown token. 961 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 962 if (!isToken(token_hex)) return false; 963 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 964 defer file.close(self.io); 965 var storage: [evt.event_id_size]u8 = undefined; 966 var file_reader = file.reader(self.io, &storage); 967 file_reader.interface.readSliceAll(out) catch return false; 968 return true; 969 } 970 971 // revoke a session. a no-op for a missing or malformed token. 972 pub fn remove(self: SessionStore, token_hex: []const u8) void { 973 if (!isToken(token_hex)) return; 974 self.dir.deleteFile(self.io, token_hex) catch {}; 975 } 976 977 // a token is exactly token_hex_len hex chars. validating before using the 978 // value as a path keeps attacker-supplied cookies from escaping the 979 // sessions dir (e.g. via "/" or ".." bytes). 980 fn isToken(token_hex: []const u8) bool { 981 if (token_hex.len != token_hex_len) return false; 982 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 983 return true; 984 } 985 }; 986