Something went wrong. Try again.
My version of https://github.com/vercel-labs/fx
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292const std = @import("std");const debug_trace = @import("../shared/debug_trace.zig");const host_target = @import("../hosts/target.zig");const io_mod = @import("../shared/io.zig");const profile_paths = @import("../shared/profile_paths.zig");const secret = @import("secret.zig");
const Allocator = std.mem.Allocator;const schema_version: i64 = 1;const max_auth_file_bytes: usize = 64 * 1024;const expiry_skew_ms: i64 = 60 * 1000;const mutation_lock_file_name = "grok-auth.lock";const mutation_lock_deadline_ms: u64 = 2000;const max_account_id_bytes: usize = 1024;
const auth_file_name = profile_paths.grok_auth_file_name;
pub fn refreshDeadlineMs(expires_at_ms: i64) i64 { return @max(expires_at_ms - expiry_skew_ms, 0);}
pub fn validAccountId(account_id: []const u8) bool { if (account_id.len == 0 or account_id.len > max_account_id_bytes) return false; for (account_id) |byte| { if (byte < 0x21 or byte > 0x7e) return false; } return true;}
pub const Session = struct { access_token: []u8, refresh_token: []u8, expires_at_ms: i64, account_id: []u8,
pub fn deinit(self: *Session, alloc: Allocator) void { secret.zeroAndFree(alloc, self.access_token); secret.zeroAndFree(alloc, self.refresh_token); alloc.free(self.account_id); self.* = undefined; }
pub fn expired(self: Session, now_ms: i64) bool { return refreshDeadlineMs(self.expires_at_ms) <= now_ms; }};
pub const DeleteOutcome = enum { deleted, missing, deleted_not_durable,};
pub const Mutation = struct { fx_dir: io_mod.VerifiedDir, lock: io_mod.TimedAdvisoryLock,
pub fn deinit(self: *Mutation) void { self.lock.release(); self.fx_dir.close(); self.* = undefined; }
pub fn load(self: *Mutation, alloc: Allocator) !?Session { return loadFromDir(alloc, &self.fx_dir.dir, true); }
pub fn save(self: *Mutation, alloc: Allocator, session: Session) !void { const text = try stringify(alloc, session); defer secret.zeroAndFree(alloc, text); try io_mod.durableReplaceVerified(alloc, &self.fx_dir, auth_file_name, text); }
pub fn delete(self: *Mutation) !DeleteOutcome { self.fx_dir.dir.deleteFile(io_mod.getIo(), auth_file_name) catch |err| switch (err) { error.FileNotFound => return .missing, else => return err, }; const durable: io_mod.DurableOps = .{}; durable.sync_dir(durable.ctx, self.fx_dir.dir) catch return .deleted_not_durable; return .deleted; }};
pub fn load(alloc: Allocator) !?Session { if (comptime host_target.is_wasm) return null; const home = io_mod.getenv("HOME") orelse return null; var home_dir = std.Io.Dir.openDirAbsolute(io_mod.getIo(), home, .{ .iterate = true }) catch |err| { debug_trace.logf("auth", "Grok session load failed step=open_home err={s}", .{@errorName(err)}); return null; }; defer home_dir.close(io_mod.getIo());
var fx_dir = home_dir.openDir(io_mod.getIo(), profile_paths.root_dir_name, .{ .iterate = true, .follow_symlinks = false, }) catch |err| { if (err != error.FileNotFound) { debug_trace.logf("auth", "Grok session load failed step=open_profile err={s}", .{@errorName(err)}); } return null; }; defer fx_dir.close(io_mod.getIo()); return loadFromDir(alloc, &fx_dir, false);}
fn loadFromDir(alloc: Allocator, fx_dir: *std.Io.Dir, report_open_failure: bool) !?Session { var file = fx_dir.openFile(io_mod.getIo(), auth_file_name, .{ .mode = .read_only, .allow_directory = false, .follow_symlinks = false, .resolve_beneath = true, }) catch |err| switch (err) { error.FileNotFound => return null, else => { debug_trace.logf("auth", "Grok session load failed step=open_file err={s}", .{@errorName(err)}); if (report_open_failure) return err; return null; }, }; defer file.close(io_mod.getIo());
const stat = try file.stat(io_mod.getIo()); if (stat.kind != .file or stat.permissions.toMode() & 0o077 != 0) { debug_trace.logf("auth", "Grok session load failed step=permissions err=InsecureAuthFile", .{}); return null; }
const bytes = try io_mod.readFileToEnd(alloc, &file, max_auth_file_bytes); defer secret.zeroAndFree(alloc, bytes); return parse(alloc, bytes) catch |err| switch (err) { error.OutOfMemory => return err, else => { debug_trace.logf("auth", "Grok session load failed step=parse err={s}", .{@errorName(err)}); return null; }, };}
pub fn saveNewSession(alloc: Allocator, session: Session) !void { if (comptime host_target.is_wasm) return error.GrokOAuthUnavailable; var mutation = try beginMutation(); defer mutation.deinit(); try mutation.save(alloc, session);}
pub fn beginExistingMutation() !?Mutation { if (comptime host_target.is_wasm) return null; const home = io_mod.getenv("HOME") orelse return error.HomeNotSet; var home_dir = io_mod.VerifiedDir{ .dir = try std.Io.Dir.openDirAbsolute(io_mod.getIo(), home, .{ .iterate = true }), }; defer home_dir.close();
const fx_dir = openExistingPrivateFxDir(&home_dir) catch |err| switch (err) { error.FileNotFound => return null, else => return err, }; return try lockMutation(fx_dir);}
fn beginMutation() !Mutation { const home = io_mod.getenv("HOME") orelse return error.HomeNotSet; var home_dir = io_mod.VerifiedDir{ .dir = try std.Io.Dir.openDirAbsolute(io_mod.getIo(), home, .{ .iterate = true }), }; defer home_dir.close();
const fx_dir = try io_mod.openOrCreateVerifiedPrivateDir(&home_dir, profile_paths.root_dir_name); return lockMutation(fx_dir);}
fn lockMutation(open_fx_dir: io_mod.VerifiedDir) !Mutation { var fx_dir = open_fx_dir; errdefer fx_dir.close(); var lock = try io_mod.acquireTimedAdvisoryLock( &fx_dir, mutation_lock_file_name, mutation_lock_deadline_ms, ); errdefer lock.release(); return .{ .fx_dir = fx_dir, .lock = lock };}
fn openExistingPrivateFxDir(home_dir: *io_mod.VerifiedDir) !io_mod.VerifiedDir { var dir = try home_dir.dir.openDir(io_mod.getIo(), profile_paths.root_dir_name, .{ .iterate = true, .follow_symlinks = false, }); errdefer dir.close(io_mod.getIo());
const initial_stat = try dir.stat(io_mod.getIo()); if (initial_stat.kind != .directory) return error.DurablePathUnsafe; if (initial_stat.permissions.toMode() & 0o200 == 0) return error.PrivateStatePermissionsUnsupported; dir.setPermissions(io_mod.getIo(), std.Io.File.Permissions.fromMode(0o700)) catch { return error.PrivateStatePermissionsUnsupported; }; const stat = try dir.stat(io_mod.getIo()); if (stat.kind != .directory or stat.permissions.toMode() & 0o777 != 0o700) { return error.PrivateStatePermissionsUnsupported; } return .{ .dir = dir };}
pub fn parse(alloc: Allocator, bytes: []const u8) !Session { var parsed = try std.json.parseFromSlice(std.json.Value, alloc, bytes, .{}); defer parsed.deinit(); if (parsed.value != .object) return error.InvalidGrokAuthSession; const object = parsed.value.object; const version = object.get("version") orelse return error.InvalidGrokAuthSession; if (version != .integer or version.integer != schema_version) return error.InvalidGrokAuthSession;
const access_token = try dupeRequiredString(alloc, object, "access_token"); errdefer secret.zeroAndFree(alloc, access_token); const refresh_token = try dupeRequiredString(alloc, object, "refresh_token"); errdefer secret.zeroAndFree(alloc, refresh_token); const account_id = try dupeRequiredString(alloc, object, "account_id"); errdefer alloc.free(account_id); if (!validAccountId(account_id)) return error.InvalidGrokAuthSession; const expires_at_ms = try requiredInteger(object, "expires_at_ms"); return .{ .access_token = access_token, .refresh_token = refresh_token, .expires_at_ms = expires_at_ms, .account_id = account_id, };}
pub fn stringify(alloc: Allocator, session: Session) ![]u8 { if (!validAccountId(session.account_id)) return error.InvalidGrokAuthSession; var out: std.Io.Writer.Allocating = .init(alloc); errdefer out.deinit(); try out.writer.writeAll("{\"version\":1,\"access_token\":"); try std.json.Stringify.value(session.access_token, .{}, &out.writer); try out.writer.writeAll(",\"refresh_token\":"); try std.json.Stringify.value(session.refresh_token, .{}, &out.writer); try out.writer.print(",\"expires_at_ms\":{d},\"account_id\":", .{session.expires_at_ms}); try std.json.Stringify.value(session.account_id, .{}, &out.writer); try out.writer.writeAll("}\n"); return out.toOwnedSlice();}
fn dupeRequiredString(alloc: Allocator, object: std.json.ObjectMap, key: []const u8) ![]u8 { const value = object.get(key) orelse return error.InvalidGrokAuthSession; if (value != .string or value.string.len == 0) return error.InvalidGrokAuthSession; return alloc.dupe(u8, value.string);}
fn requiredInteger(object: std.json.ObjectMap, key: []const u8) !i64 { const value = object.get(key) orelse return error.InvalidGrokAuthSession; if (value != .integer) return error.InvalidGrokAuthSession; return value.integer;}
test "Grok auth session round trips without exposing token fields to structure" { const alloc = std.testing.allocator; var session = Session{ .access_token = try alloc.dupe(u8, "header.payload.signature"), .refresh_token = try alloc.dupe(u8, "refresh"), .expires_at_ms = 1234, .account_id = try alloc.dupe(u8, "acct_123"), }; defer session.deinit(alloc);
const encoded = try stringify(alloc, session); defer secret.zeroAndFree(alloc, encoded); var decoded = try parse(alloc, encoded); defer decoded.deinit(alloc);
try std.testing.expectEqualStrings(session.access_token, decoded.access_token); try std.testing.expectEqualStrings(session.refresh_token, decoded.refresh_token); try std.testing.expectEqualStrings(session.account_id, decoded.account_id); try std.testing.expectEqual(session.expires_at_ms, decoded.expires_at_ms);}
test "Grok account identity is bounded and safe for HTTP headers" { try std.testing.expect(validAccountId("acct_123")); try std.testing.expect(!validAccountId("")); try std.testing.expect(!validAccountId("acct\r\ninjected")); try std.testing.expect(!validAccountId("a" ** (max_account_id_bytes + 1)));
const invalid = \\{"version":1,"access_token":"access","refresh_token":"refresh","expires_at_ms":1234,"account_id":"acct\ninjected"} ; try std.testing.expectError(error.InvalidGrokAuthSession, parse(std.testing.allocator, invalid));}
test "Grok session refresh deadline keeps a one minute safety margin" { try std.testing.expectEqual(@as(i64, 40_000), refreshDeadlineMs(100_000)); try std.testing.expectEqual(@as(i64, 0), refreshDeadlineMs(10_000));}