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