╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3037 return try decodeFormValue(allocator, pair[eq + 1 ..]); 3038 } 3039 } 3040 return null; 3041 } 3042 3043 fn readFormBody(request: *std.http.Server.Request, allocator: std.mem.Allocator) ![]u8 { 3044 var buffer: [256]u8 = undefined; 3045 return request.readerExpectNone(&buffer).allocRemaining(allocator, .limited(65536)); 3046 } 3047 3048 fn decodeFormValue(allocator: std.mem.Allocator, encoded: []const u8) ![]u8 { 3049 var out: std.ArrayList(u8) = .empty; 3050 errdefer out.deinit(allocator); 3051 var i: usize = 0; 3052 while (i < encoded.len) : (i += 1) { 3053 switch (encoded[i]) { 3054 '+' => try out.append(allocator, ' '), 3055 '%' => { 3056 if (i + 2 >= encoded.len) return error.InvalidPercentEncoding; 3057 const hi = std.fmt.charToDigit(encoded[i + 1], 16) catch return error.InvalidPercentEncoding; 3058 const lo = std.fmt.charToDigit(encoded[i + 2], 16) catch return error.InvalidPercentEncoding; 3059 try out.append(allocator, hi * 16 + lo); 3060 i += 2; 3061 }, 3062 else => try out.append(allocator, encoded[i]), 3063 } 3064 } 3065 return try out.toOwnedSlice(allocator); 3066 } 3067 3068 // disk-backed web session store. each login mints a random, opaque token 3069 // (not the user's id) that is stored as a file named by the token hex, whose 3070 // contents are the raw user_id bytes. authenticating a request is a lookup of 3071 // the cookie's token; logout deletes the file, revoking the session. there is 3072 // deliberately no expiry — a session lives until logout. form failures use 3073 // files in the flash subdirectory that the redirected GET consumes. the 3074 // directory is cleared whenever the server starts. 3075 pub const SessionStore = struct { 3076 io: std.Io, 3077 dir: std.Io.Dir, 3078 flash_dir: std.Io.Dir, 3079 auto_login: ?[token_hex_len]u8, 3080 3081 pub const token_hex_len = evt.event_id_size * 2; 3082 3083 pub fn init(io: std.Io, data_dir: std.Io.Dir) !SessionStore { 3084 try data_dir.createDirPath(io, "sessions"); 3085 const dir = try data_dir.openDir(io, "sessions", .{}); 3086 errdefer dir.close(io); 3087 try dir.deleteTree(io, "flash"); 3088 try dir.createDirPath(io, "flash"); 3089 const flash_dir = try dir.openDir(io, "flash", .{}); 3090 errdefer flash_dir.close(io); 3091 const auto_login = blk: { 3092 const file = dir.openFile(io, auto_login_name, .{ .mode = .read_only }) catch break :blk null; 3093 defer file.close(io); 3094 var token: [token_hex_len]u8 = undefined; 3095 var storage: [token_hex_len]u8 = undefined; 3096 var reader = file.reader(io, &storage); 3097 reader.interface.readSliceAll(&token) catch break :blk null; 3098 break :blk token; 3099 }; 3100 return .{ .io = io, .dir = dir, .flash_dir = flash_dir, .auto_login = auto_login }; 3101 } 3102 3103 pub fn deinit(self: SessionStore) void { 3104 self.flash_dir.close(self.io); 3105 self.dir.close(self.io); 3106 } 3107 3108 // mint a new session for user_id, returning the token hex to set as the 3109 // cookie value. 3110 pub fn create(self: SessionStore, user_id: *const [evt.event_id_size]u8) ![token_hex_len]u8 { 3111 var token: [evt.event_id_size]u8 = undefined; 3112 self.io.random(&token); 3113 const token_hex = std.fmt.bytesToHex(token, .lower); 3114 const file = try self.dir.createFile(self.io, &token_hex, .{}); 3115 defer file.close(self.io); 3116 try file.writeStreamingAll(self.io, user_id); 3117 return token_hex; 3118 } 3119 3120 fn createFlash(self: SessionStore, allocator: std.mem.Allocator, value: ui.Session.FormFeedback) ![token_hex_len]u8 { 3121 var token: [evt.event_id_size]u8 = undefined; 3122 self.io.random(&token); 3123 const token_hex = std.fmt.bytesToHex(token, .lower); 3124 const file = try self.flash_dir.createFile(self.io, &token_hex, .{}); 3125 errdefer self.flash_dir.deleteFile(self.io, &token_hex) catch {}; 3126 defer file.close(self.io); 3127 3128 var json: std.Io.Writer.Allocating = .init(allocator); 3129 defer json.deinit(); 3130 try std.json.Stringify.value(value, .{}, &json.writer); 3131 try file.writeStreamingAll(self.io, json.written()); 3132 return token_hex; 3133 } 3134 3135 fn takeFlash(self: SessionStore, allocator: std.mem.Allocator, token_hex: []const u8) ?ui.Session.FormFeedback { 3136 if (!isToken(token_hex)) return null; 3137 const file = self.flash_dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return null; 3138 defer self.flash_dir.deleteFile(self.io, token_hex) catch {}; 3139 defer file.close(self.io); 3140 3141 var storage: [4096]u8 = undefined; 3142 var reader = file.reader(self.io, &storage); 3143 const bytes = reader.interface.allocRemaining(allocator, .limited(512 * 1024)) catch return null; 3144 return std.json.parseFromSliceLeaky(ui.Session.FormFeedback, allocator, bytes, .{}) catch null; 3145 } 3146 3147 // resolve a cookie's token to its user_id. returns false (logged out) for a 3148 // missing, malformed, or unknown token. 3149 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 3150 if (!isToken(token_hex)) return false; 3151 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 3152 defer file.close(self.io); 3153 var storage: [evt.event_id_size]u8 = undefined; 3154 var file_reader = file.reader(self.io, &storage); 3155 file_reader.interface.readSliceAll(out) catch return false; 3156 return true; 3157 } 3158 3159 // revoke a session. a no-op for a missing or malformed token. 3160 pub fn remove(self: SessionStore, token_hex: []const u8) void { 3161 if (!isToken(token_hex)) return; 3162 self.dir.deleteFile(self.io, token_hex) catch {}; 3163 } 3164 3165 // leave `token_hex`'s session for visitors arriving without one. only the 3166 // `try` fixture writes one. the name can't collide with a session token. 3167 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 3168 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 3169 defer file.close(self.io); 3170 try file.writeStreamingAll(self.io, token_hex); 3171 } 3172 3173 pub fn autoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 3174 out.* = self.auto_login orelse return false; 3175 return true; 3176 } 3177 3178 const auto_login_name = "auto-login"; 3179 3180 // a token is exactly token_hex_len hex chars. validating before using the 3181 // value as a path keeps attacker-supplied cookies from escaping the 3182 // sessions dir (e.g. via "/" or ".." bytes). 3183 fn isToken(token_hex: []const u8) bool { 3184 if (token_hex.len != token_hex_len) return false; 3185 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 3186 return true; 3187 } 3188 }; 3189