╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1076 i += 1; 1077 return buf[0..i]; 1078 } 1079 1080 fn appendEscapedHtml(allocator: std.mem.Allocator, out: *std.ArrayList(u8), input: []const u8) !void { 1081 for (input) |ch| { 1082 switch (ch) { 1083 '&' => try out.appendSlice(allocator, "&amp;"), 1084 '<' => try out.appendSlice(allocator, "&lt;"), 1085 '>' => try out.appendSlice(allocator, "&gt;"), 1086 '"' => try out.appendSlice(allocator, "&quot;"), 1087 '\'' => try out.appendSlice(allocator, "&#39;"), 1088 else => try out.append(allocator, ch), 1089 } 1090 } 1091 } 1092 1093 // --- helpers -------------------------------------------------------------- 1094 1095 fn getCookieValue(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 1096 var iter = request.iterateHeaders(); 1097 while (iter.next()) |header| { 1098 if (!std.ascii.eqlIgnoreCase(header.name, "cookie")) continue; 1099 var pairs = std.mem.splitScalar(u8, header.value, ';'); 1100 while (pairs.next()) |pair| { 1101 const trimmed = std.mem.trim(u8, pair, " \t"); 1102 const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; 1103 if (std.mem.eql(u8, trimmed[0..eq], name)) { 1104 return trimmed[eq + 1 ..]; 1105 } 1106 } 1107 } 1108 return null; 1109 } 1110 1111 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 1112 var iter = std.mem.splitScalar(u8, body, '&'); 1113 while (iter.next()) |pair| { 1114 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 1115 if (std.mem.eql(u8, pair[0..eq], key)) { 1116 return try decodeFormValue(allocator, pair[eq + 1 ..]); 1117 } 1118 } 1119 return null; 1120 } 1121 1122 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 1123 var out: std.ArrayList(u8) = .empty; 1124 errdefer out.deinit(allocator); 1125 var i: usize = 0; 1126 while (i < encoded.len) : (i += 1) { 1127 switch (encoded[i]) { 1128 '+' => try out.append(allocator, ' '), 1129 '%' => { 1130 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 1131 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 1132 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 1133 try out.append(allocator, hi * 16 + lo); 1134 i += 2; 1135 }, 1136 else => try out.append(allocator, encoded[i]), 1137 } 1138 } 1139 return try out.toOwnedSlice(allocator); 1140 } 1141 1142 // disk-backed web session store. each login mints a random, opaque token 1143 // (not the user's id) that is stored as a file named by the token hex, whose 1144 // contents are the raw user_id bytes. authenticating a request is a lookup of 1145 // the cookie's token; logout deletes the file, revoking the session. there is 1146 // deliberately no expiry — a session lives until logout. 1147 pub const SessionStore = struct { 1148 io: std.Io, 1149 dir: std.Io.Dir, 1150 1151 pub const token_hex_len = evt.event_id_size * 2; 1152 1153 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 1154 try data_dir.createDirPath(io, "sessions"); 1155 const dir = try data_dir.openDir(io, "sessions", .{}); 1156 return .{ .io = io, .dir = dir }; 1157 } 1158 1159 pub fn deinit(self: SessionStore) void { 1160 self.dir.close(self.io); 1161 } 1162 1163 // mint a new session for user_id, returning the token hex to set as the 1164 // cookie value. 1165 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 1166 var token: [evt.event_id_size]u8 = undefined; 1167 self.io.random(&token); 1168 const token_hex = std.fmt.bytesToHex(token, .lower); 1169 const file = try self.dir.createFile(self.io, &token_hex, .{}); 1170 defer file.close(self.io); 1171 try file.writeStreamingAll(self.io, user_id); 1172 return token_hex; 1173 } 1174 1175 // resolve a cookie's token to its user_id. returns false (logged out) for a 1176 // missing, malformed, or unknown token. 1177 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 1178 if (!isToken(token_hex)) return false; 1179 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 1180 defer file.close(self.io); 1181 var storage: [evt.event_id_size]u8 = undefined; 1182 var file_reader = file.reader(self.io, &storage); 1183 file_reader.interface.readSliceAll(out) catch return false; 1184 return true; 1185 } 1186 1187 // revoke a session. a no-op for a missing or malformed token. 1188 pub fn remove(self: SessionStore, token_hex: []const u8) void { 1189 if (!isToken(token_hex)) return; 1190 self.dir.deleteFile(self.io, token_hex) catch {}; 1191 } 1192 1193 // a token is exactly token_hex_len hex chars. validating before using the 1194 // value as a path keeps attacker-supplied cookies from escaping the 1195 // sessions dir (e.g. via "/" or ".." bytes). 1196 fn isToken(token_hex: []const u8) bool { 1197 if (token_hex.len != token_hex_len) return false; 1198 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 1199 return true; 1200 } 1201 }; 1202