╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2803 } 2804 } 2805 return null; 2806 } 2807 2808 fn readFormBody(request: *std.http.Server.Request, allocator: std.mem.Allocator) ![]u8 { 2809 var buffer: [256]u8 = undefined; 2810 return request.readerExpectNone(&buffer).allocRemaining(allocator, .limited(65536)); 2811 } 2812 2813 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 2814 var out: std.ArrayList(u8) = .empty; 2815 errdefer out.deinit(allocator); 2816 var i: usize = 0; 2817 while (i < encoded.len) : (i += 1) { 2818 switch (encoded[i]) { 2819 '+' => try out.append(allocator, ' '), 2820 '%' => { 2821 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 2822 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 2823 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 2824 try out.append(allocator, hi * 16 + lo); 2825 i += 2; 2826 }, 2827 else => try out.append(allocator, encoded[i]), 2828 } 2829 } 2830 return try out.toOwnedSlice(allocator); 2831 } 2832 2833 // disk-backed web session store. each login mints a random, opaque token 2834 // (not the user's id) that is stored as a file named by the token hex, whose 2835 // contents are the raw user_id bytes. authenticating a request is a lookup of 2836 // the cookie's token; logout deletes the file, revoking the session. there is 2837 // deliberately no expiry — a session lives until logout. form failures use 2838 // files in the flash subdirectory that the redirected GET consumes. the 2839 // directory is cleared whenever the server starts. 2840 pub const SessionStore = struct { 2841 io: std.Io, 2842 dir: std.Io.Dir, 2843 flash_dir: std.Io.Dir, 2844 auto_login: ?[token_hex_len]u8, 2845 2846 pub const token_hex_len = evt.event_id_size * 2; 2847 2848 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 2849 try data_dir.createDirPath(io, "sessions"); 2850 const dir = try data_dir.openDir(io, "sessions", .{}); 2851 errdefer dir.close(io); 2852 try dir.deleteTree(io, "flash"); 2853 try dir.createDirPath(io, "flash"); 2854 const flash_dir = try dir.openDir(io, "flash", .{}); 2855 errdefer flash_dir.close(io); 2856 const auto_login = blk: { 2857 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 2858 defer file.close(io); 2859 var token: [token_hex_len]u8 = undefined; 2860 var storage: [token_hex_len]u8 = undefined; 2861 var reader = file.reader(io, &storage); 2862 reader.interface.readSliceAll(&token) catch break :blk null; 2863 break :blk token; 2864 }; 2865 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 2866 } 2867 2868 pub fn deinit(self: SessionStore) void { 2869 self.flash_dir.close(self.io); 2870 self.dir.close(self.io); 2871 } 2872 2873 // mint a new session for user_id, returning the token hex to set as the 2874 // cookie value. 2875 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 2876 var token: [evt.event_id_size]u8 = undefined; 2877 self.io.random(&token); 2878 const token_hex = std.fmt.bytesToHex(token, .lower); 2879 const file = try self.dir.createFile(self.io, &token_hex, .{}); 2880 defer file.close(self.io); 2881 try file.writeStreamingAll(self.io, user_id); 2882 return token_hex; 2883 } 2884 2885 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 2886 var token: [evt.event_id_size]u8 = undefined; 2887 self.io.random(&token); 2888 const token_hex = std.fmt.bytesToHex(token, .lower); 2889 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 2890 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 2891 defer file.close(self.io); 2892 2893 var json: std.Io.Writer.Allocating = .init(allocator); 2894 defer json.deinit(); 2895 try std.json.Stringify.value(value, .{}, &json.writer); 2896 try file.writeStreamingAll(self.io, json.written()); 2897 return token_hex; 2898 } 2899 2900 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 2901 if (!isToken(token_hex)) return null; 2902 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 2903 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 2904 defer file.close(self.io); 2905 2906 var storage: [4096]u8 = undefined; 2907 var reader = file.reader(self.io, &storage); 2908 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 2909 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 2910 } 2911 2912 // resolve a cookie's token to its user_id. returns false (logged out) for a 2913 // missing, malformed, or unknown token. 2914 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 2915 if (!isToken(token_hex)) return false; 2916 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 2917 defer file.close(self.io); 2918 var storage: [evt.event_id_size]u8 = undefined; 2919 var file_reader = file.reader(self.io, &storage); 2920 file_reader.interface.readSliceAll(out) catch return false; 2921 return true; 2922 } 2923 2924 // revoke a session. a no-op for a missing or malformed token. 2925 pub fn remove(self: SessionStore, token_hex: []const u8) void { 2926 if (!isToken(token_hex)) return; 2927 self.dir.deleteFile(self.io, token_hex) catch {}; 2928 } 2929 2930 // leave `token_hex`'s session for visitors arriving without one. only the 2931 // `try` fixture writes one. the name can't collide with a session token. 2932 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 2933 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 2934 defer file.close(self.io); 2935 try file.writeStreamingAll(self.io, token_hex); 2936 } 2937 2938 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 2939 out.* = self.auto_login orelse return false; 2940 return true; 2941 } 2942 2943 const auto_login_name = "auto-login"; 2944 2945 // a token is exactly token_hex_len hex chars. validating before using the 2946 // value as a path keeps attacker-supplied cookies from escaping the 2947 // sessions dir (e.g. via "/" or ".." bytes). 2948 fn isToken(token_hex: []const u8) bool { 2949 if (token_hex.len != token_hex_len) return false; 2950 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 2951 return true; 2952 } 2953 }; 2954