╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1944 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 1945 defer file.close(self.io); 1946 var storage: [evt.event_id_size]u8 = undefined; 1947 var file_reader = file.reader(self.io, &storage); 1948 file_reader.interface.readSliceAll(out) catch return false; 1949 return true; 1950 } 1951 1952 // revoke a session. a no-op for a missing or malformed token. 1953 pub fn remove(self: SessionStore, token_hex: []const u8) void { 1954 if (!isToken(token_hex)) return; 1955 self.dir.deleteFile(self.io, token_hex) catch {}; 1956 } 1957 1958 // leave `token_hex`'s session for the next visitor arriving without one. 1959 // only the `try` fixture writes one, and only a debug build honors it. the 1960 // name isn't token-shaped, so it can't collide with a session file. 1961 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 1962 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 1963 defer file.close(self.io); 1964 try file.writeStreamingAll(self.io, token_hex); 1965 } 1966 1967 // claim the offered session, if one is there. the delete picks the winner 1968 // of a race, so the offer is only ever taken once. a release build never 1969 // looks, because that would be an extra syscall for every GET request. 1970 pub fn claimAutoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 1971 if (builtin.mode != .Debug) return false; 1972 { 1973 const file = self.dir.openFile(self.io, auto_login_name, .{ .mode = .read_only }) catch return false; 1974 defer file.close(self.io); 1975 var storage: [token_hex_len]u8 = undefined; 1976 var file_reader = file.reader(self.io, &storage); 1977 file_reader.interface.readSliceAll(out) catch return false; 1978 } 1979 self.dir.deleteFile(self.io, auto_login_name) catch return false; 1980 return true; 1981 } 1982 1983 const auto_login_name = "auto-login"; 1984 1985 // a token is exactly token_hex_len hex chars. validating before using the 1986 // value as a path keeps attacker-supplied cookies from escaping the 1987 // sessions dir (e.g. via "/" or ".." bytes). 1988 fn isToken(token_hex: []const u8) bool { 1989 if (token_hex.len != token_hex_len) return false; 1990 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 1991 return true; 1992 } 1993 }; 1994