╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
692 693 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 694 var out: std.ArrayList(u8) = .empty; 695 errdefer out.deinit(allocator); 696 var i: usize = 0; 697 while (i < encoded.len) : (i += 1) { 698 switch (encoded[i]) { 699 '+' => try out.append(allocator, ' '), 700 '%' => { 701 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 702 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 703 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 704 try out.append(allocator, hi * 16 + lo); 705 i += 2; 706 }, 707 else => try out.append(allocator, encoded[i]), 708 } 709 } 710 return try out.toOwnedSlice(allocator); 711 } 712 713 // disk-backed web session store. each login mints a random, opaque token 714 // (not the user's id) that is stored as a file named by the token hex, whose 715 // contents are the raw user_id bytes. authenticating a request is a lookup of 716 // the cookie's token; logout deletes the file, revoking the session. there is 717 // deliberately no expiry — a session lives until logout. 718 pub const SessionStore = struct { 719 io: std.Io, 720 dir: std.Io.Dir, 721 722 pub const token_hex_len = evt.event_id_size * 2; 723 724 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 725 try data_dir.createDirPath(io, "sessions"); 726 const dir = try data_dir.openDir(io, "sessions", .{}); 727 return .{ .io = io, .dir = dir }; 728 } 729 730 pub fn deinit(self: SessionStore) void { 731 self.dir.close(self.io); 732 } 733 734 // mint a new session for user_id, returning the token hex to set as the 735 // cookie value. 736 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 737 var token: [evt.event_id_size]u8 = undefined; 738 self.io.random(&token); 739 const token_hex = std.fmt.bytesToHex(token, .lower); 740 const file = try self.dir.createFile(self.io, &token_hex, .{}); 741 defer file.close(self.io); 742 try file.writeStreamingAll(self.io, user_id); 743 return token_hex; 744 } 745 746 // resolve a cookie's token to its user_id. returns false (logged out) for a 747 // missing, malformed, or unknown token. 748 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 749 if (!isToken(token_hex)) return false; 750 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 751 defer file.close(self.io); 752 var storage: [evt.event_id_size]u8 = undefined; 753 var file_reader = file.reader(self.io, &storage); 754 file_reader.interface.readSliceAll(out) catch return false; 755 return true; 756 } 757 758 // revoke a session. a no-op for a missing or malformed token. 759 pub fn remove(self: SessionStore, token_hex: []const u8) void { 760 if (!isToken(token_hex)) return; 761 self.dir.deleteFile(self.io, token_hex) catch {}; 762 } 763 764 // a token is exactly token_hex_len hex chars. validating before using the 765 // value as a path keeps attacker-supplied cookies from escaping the 766 // sessions dir (e.g. via "/" or ".." bytes). 767 fn isToken(token_hex: []const u8) bool { 768 if (token_hex.len != token_hex_len) return false; 769 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 770 return true; 771 } 772 }; 773