╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
705 else => try out.append(allocator, ch), 706 } 707 } 708 } 709 710 // --- helpers -------------------------------------------------------------- 711 712 fn getCookieValue(request: *std.http.Server.Request, name: []const u8) ?[]const u8 { 713 var iter = request.iterateHeaders(); 714 while (iter.next()) |header| { 715 if (!std.ascii.eqlIgnoreCase(header.name, "cookie")) continue; 716 var pairs = std.mem.splitScalar(u8, header.value, ';'); 717 while (pairs.next()) |pair| { 718 const trimmed = std.mem.trim(u8, pair, " \t"); 719 const eq = std.mem.indexOfScalar(u8, trimmed, '=') orelse continue; 720 if (std.mem.eql(u8, trimmed[0..eq], name)) { 721 return trimmed[eq + 1 ..]; 722 } 723 } 724 } 725 return null; 726 } 727 728 fn parseFormField(allocator: std.mem.Allocator, body: []const u8, key: []const u8) !?[]u8 { 729 var iter = std.mem.splitScalar(u8, body, '&'); 730 while (iter.next()) |pair| { 731 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 732 if (std.mem.eql(u8, pair[0..eq], key)) { 733 return try decodeFormValue(allocator, pair[eq + 1 ..]); 734 } 735 } 736 return null; 737 } 738 739 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 740 var out: std.ArrayList(u8) = .empty; 741 errdefer out.deinit(allocator); 742 var i: usize = 0; 743 while (i < encoded.len) : (i += 1) { 744 switch (encoded[i]) { 745 '+' => try out.append(allocator, ' '), 746 '%' => { 747 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 748 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 749 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 750 try out.append(allocator, hi * 16 + lo); 751 i += 2; 752 }, 753 else => try out.append(allocator, encoded[i]), 754 } 755 } 756 return try out.toOwnedSlice(allocator); 757 } 758 759 // disk-backed web session store. each login mints a random, opaque token 760 // (not the user's id) that is stored as a file named by the token hex, whose 761 // contents are the raw user_id bytes. authenticating a request is a lookup of 762 // the cookie's token; logout deletes the file, revoking the session. there is 763 // deliberately no expiry — a session lives until logout. 764 pub const SessionStore = struct { 765 io: std.Io, 766 dir: std.Io.Dir, 767 768 pub const token_hex_len = evt.event_id_size * 2; 769 770 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 771 try data_dir.createDirPath(io, "sessions"); 772 const dir = try data_dir.openDir(io, "sessions", .{}); 773 return .{ .io = io, .dir = dir }; 774 } 775 776 pub fn deinit(self: SessionStore) void { 777 self.dir.close(self.io); 778 } 779 780 // mint a new session for user_id, returning the token hex to set as the 781 // cookie value. 782 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 783 var token: [evt.event_id_size]u8 = undefined; 784 self.io.random(&token); 785 const token_hex = std.fmt.bytesToHex(token, .lower); 786 const file = try self.dir.createFile(self.io, &token_hex, .{}); 787 defer file.close(self.io); 788 try file.writeStreamingAll(self.io, user_id); 789 return token_hex; 790 } 791 792 // resolve a cookie's token to its user_id. returns false (logged out) for a 793 // missing, malformed, or unknown token. 794 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 795 if (!isToken(token_hex)) return false; 796 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 797 defer file.close(self.io); 798 var storage: [evt.event_id_size]u8 = undefined; 799 var file_reader = file.reader(self.io, &storage); 800 file_reader.interface.readSliceAll(out) catch return false; 801 return true; 802 } 803 804 // revoke a session. a no-op for a missing or malformed token. 805 pub fn remove(self: SessionStore, token_hex: []const u8) void { 806 if (!isToken(token_hex)) return; 807 self.dir.deleteFile(self.io, token_hex) catch {}; 808 } 809 810 // a token is exactly token_hex_len hex chars. validating before using the 811 // value as a path keeps attacker-supplied cookies from escaping the 812 // sessions dir (e.g. via "/" or ".." bytes). 813 fn isToken(token_hex: []const u8) bool { 814 if (token_hex.len != token_hex_len) return false; 815 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 816 return true; 817 } 818 }; 819