oficina / sheets Public

Sheets native: a "Sign in first." 403 is an ended session

Sheets' session guards answer a non-GET request whose session has ended
or been refused with 403 "Sign in first." (home.go, team_access.go), not
401. NativeHTTP reads 403 as .forbidden, so SheetsOficinaTransport took
it for a refused request and refreshed the teams: a window open past
the native session's seven days whose first request was a cell write
never re-minted (Fable, coordinator's note on d37b269). That exact 403
now re-mints, as a 401 does, and reaches the caller as 401. Any other
403 is still a refusal. The server is unchanged.

Test: the sign-in-first 403 is rejected and surfaces as 401; another 403
stays a refusal. swift test passes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev ded0f68335b0499b30636ebf332d95730ffb232a parent 0f7ee02
2 files changed, +45 −3
  • native/apple/Sources/SheetsKit/SheetsOficinaID.swift +21 −3
  • native/apple/Tests/SheetsKitTests/OficinaIDAdoptionTests.swift +24 −0

CI passed — run details

CI log
amadan: .amadan/timeout: this job's budget is 15m0s (this runner allows up to 30m0s)
=== vet ===
CGO_ENABLED=0 go vet ./...
go: downloading amadan.net/oficina/home/connect v0.0.0-20261114110939-e03a01a1881c
go: downloading amadan.net/oficina/home/handoff v0.0.0-20261114110939-e03a01a1881c
CGO_ENABLED=0 go vet -tags browser ./internal/sheets/
=== fmt ===
=== build ===
CGO_ENABLED=0 go build ./...
=== migrations ===
go tool rastrillo migration check
rastrillo migration check: models and migrations agree
=== go-test ===
CGO_ENABLED=0 go test -count=1 ./...
?   	amadan.net/oficina/sheets/cmd/sheets	[no test files]
?   	amadan.net/oficina/sheets/cmd/sheets-contract	[no test files]
ok  	amadan.net/oficina/sheets/internal/guardstest	0.158s
ok  	amadan.net/oficina/sheets/internal/sheets	15.234s
ok  	amadan.net/oficina/sheets/internal/sheets/calc	0.011s
ok  	amadan.net/oficina/sheets/internal/sheets/format	0.003s
?   	amadan.net/oficina/sheets/internal/sheets/keymailtest	[no test files]
ok  	amadan.net/oficina/sheets/internal/sheets/live	0.054s
ok  	amadan.net/oficina/sheets/internal/sheets/seal	0.002s
ok  	amadan.net/oficina/sheets/internal/sheets/sealtest	0.003s
ok  	amadan.net/oficina/sheets/internal/sheets/store	6.037s
ok  	amadan.net/oficina/sheets/internal/sheets/teamstate	0.095s
ok  	amadan.net/oficina/sheets/internal/sheets/xlsx	4.738s
?   	amadan.net/oficina/sheets/tools/gencorpus	[no test files]
=== race ===
go test -race -count=1 ./internal/sheets/store/... ./internal/sheets/live/...
ok  	amadan.net/oficina/sheets/internal/sheets/store	71.644s
ok  	amadan.net/oficina/sheets/internal/sheets/live	1.064s
diff --git a/native/apple/Sources/SheetsKit/SheetsOficinaID.swift b/native/apple/Sources/SheetsKit/SheetsOficinaID.swift
index b141a81..5e93d86 100644
--- a/native/apple/Sources/SheetsKit/SheetsOficinaID.swift
+++ b/native/apple/Sources/SheetsKit/SheetsOficinaID.swift
@@ -133,7 +133,7 @@ public enum SheetsSignInRoute: String, Sendable {
/// 401 re-mints silently (spec §7), a 403 refreshes the teams; after
/// sign-out every request fails as `.unauthorized`.
public func transport(for account: String, origin: URL) -> SheetsOficinaTransport {
- SheetsOficinaTransport(origin: origin, current: { [weak self] in self?.sessions[account]?.client },
+ SheetsOficinaTransport(origin: origin, current: { [weak self] in self?.sessions[account]?.client as (any NativeTransport)? },
rejected: { [weak self] in Task { @MainActor in await self?.rejected(account) } },
refused: { [weak self] in Task { @MainActor in await self?.refused() } })
}
@@ -190,19 +190,37 @@ public enum SheetsSignInRoute: String, Sendable {
/// client on every request, and word back when the instance refuses the
/// session (401, or the 303 to sign-in `NativeClientError` also reads as an
/// ended session) or a request (403).
+///
+/// Sheets' session guards answer a non-GET request with an ended or refused
+/// session as 403 "Sign in first." (home.go, team_access.go), not 401: read
+/// as a refused request, a window open past the session's seven days whose
+/// first request is a cell write would never re-mint. That one 403 is an
+/// ended session, re-minted and surfaced as the 401 it means.
public struct SheetsOficinaTransport: NativeTransport {
public let origin: URL
- let current: @MainActor @Sendable () -> NativeHTTP?
+ let current: @MainActor @Sendable () -> (any NativeTransport)?
let rejected: @Sendable () -> Void
let refused: @Sendable () -> Void
- private func client() async throws -> NativeHTTP {
+ public init(origin: URL, current: @escaping @MainActor @Sendable () -> (any NativeTransport)?,
+ rejected: @escaping @Sendable () -> Void, refused: @escaping @Sendable () -> Void) {
+ self.origin = origin; self.current = current; self.rejected = rejected; self.refused = refused
+ }
+
+ /// The session guards' refusal, byte for byte (`http.Error` adds the newline).
+ static let signInFirst = Data("Sign in first.\n".utf8)
+
+ private func client() async throws -> any NativeTransport {
guard let client = await current() else { throw NativeClientError.unauthorized }
return client
}
public func responseDetails(_ path: String, method: String, body: Data?, contentType: String, maxBytes: Int) async throws -> NativeResponse {
let reply = try await client().responseDetails(path, method: method, body: body, contentType: contentType, maxBytes: maxBytes)
+ if reply.status == 403, reply.data == Self.signInFirst {
+ rejected()
+ return NativeResponse(data: reply.data, status: 401, headers: reply.headers)
+ }
if reply.status == 401 || reply.status == 303 { rejected() }
if reply.status == 403 { refused() }
return reply
diff --git a/native/apple/Tests/SheetsKitTests/OficinaIDAdoptionTests.swift b/native/apple/Tests/SheetsKitTests/OficinaIDAdoptionTests.swift
index 2014f9e..9718567 100644
--- a/native/apple/Tests/SheetsKitTests/OficinaIDAdoptionTests.swift
+++ b/native/apple/Tests/SheetsKitTests/OficinaIDAdoptionTests.swift
@@ -137,6 +137,30 @@ import XCTest
} catch NativeClientError.unauthorized {}
}
+ /// A non-GET request with an ended session gets 403 "Sign in first."
+ /// from Sheets' guards: that is the session, not the request, so it
+ /// re-mints and reads as unauthorized. Any other 403 stays a refusal.
+ func testASignInFirst403IsAnEndedSession() async throws {
+ final class Counts: @unchecked Sendable { var rejected = 0; var refused = 0 }
+ let counts = Counts()
+ let server = ScriptedTransport(origin: Self.sheetsOrigin, [
+ .respond(status: 403, body: Data("Sign in first.\n".utf8)),
+ .respond(status: 403, body: Data(#"{"error":"not yours"}"#.utf8)),
+ ])
+ let transport = SheetsOficinaTransport(origin: Self.sheetsOrigin, current: { server },
+ rejected: { counts.rejected += 1 }, refused: { counts.refused += 1 })
+ let ended = try await transport.responseDetails("/sheets/1/cells", method: "POST", body: Data("{}".utf8),
+ contentType: "application/json", maxBytes: 1024)
+ XCTAssertEqual(ended.status, 401)
+ XCTAssertEqual(counts.rejected, 1)
+ XCTAssertEqual(counts.refused, 0)
+ let refusal = try await transport.responseDetails("/sheets/1/cells", method: "POST", body: Data("{}".utf8),
+ contentType: "application/json", maxBytes: 1024)
+ XCTAssertEqual(refusal.status, 403)
+ XCTAssertEqual(counts.rejected, 1)
+ XCTAssertEqual(counts.refused, 1)
+ }
+
// MARK: "Use another Oficina ID" in the account menu
func testUseAnotherFromTheMenuAsksThenReplaces() async throws {