╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ web.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1576 pub fn lookup(self: SessionStore, token_hex: []const u8, out: *[evt.event_id_size]u8) bool { 1577 if (!isToken(token_hex)) return false; 1578 const file = self.dir.openFile(self.io, token_hex, .{ .mode = .read_only }) catch return false; 1579 defer file.close(self.io); 1580 var storage: [evt.event_id_size]u8 = undefined; 1581 var file_reader = file.reader(self.io, &storage); 1582 file_reader.interface.readSliceAll(out) catch return false; 1583 return true; 1584 } 1585 1586 // revoke a session. a no-op for a missing or malformed token. 1587 pub fn remove(self: SessionStore, token_hex: []const u8) void { 1588 if (!isToken(token_hex)) return; 1589 self.dir.deleteFile(self.io, token_hex) catch {}; 1590 } 1591 1592 // leave `token_hex`'s session for the next visitor arriving without one. 1593 // only the `try` fixture writes one, and only a debug build honors it. the 1594 // name isn't token-shaped, so it can't collide with a session file. 1595 pub fn offerAutoLogin(self: SessionStore, token_hex: *const [token_hex_len]u8) !void { 1596 const file = try self.dir.createFile(self.io, auto_login_name, .{}); 1597 defer file.close(self.io); 1598 try file.writeStreamingAll(self.io, token_hex); 1599 } 1600 1601 // claim the offered session, if one is there. the delete picks the winner 1602 // of a race, so the offer is only ever taken once. a release build never 1603 // looks, because that would be an extra syscall for every GET request. 1604 pub fn claimAutoLogin(self: SessionStore, out: *[token_hex_len]u8) bool { 1605 if (builtin.mode != .Debug) return false; 1606 { 1607 const file = self.dir.openFile(self.io, auto_login_name, .{ .mode = .read_only }) catch return false; 1608 defer file.close(self.io); 1609 var storage: [token_hex_len]u8 = undefined; 1610 var file_reader = file.reader(self.io, &storage); 1611 file_reader.interface.readSliceAll(out) catch return false; 1612 } 1613 self.dir.deleteFile(self.io, auto_login_name) catch return false; 1614 return true; 1615 } 1616 1617 const auto_login_name = "auto-login"; 1618 1619 // a token is exactly token_hex_len hex chars. validating before using the 1620 // value as a path keeps attacker-supplied cookies from escaping the 1621 // sessions dir (e.g. via "/" or ".." bytes). 1622 fn isToken(token_hex: []const u8) bool { 1623 if (token_hex.len != token_hex_len) return false; 1624 for (token_hex) |c| if (!std.ascii.isHex(c)) return false; 1625 return true; 1626 } 1627 }; 1628