oficina / sheets Public

Sheets: pin "Sign in first." as a native contract

The native Oficina ID transport reads a 403 whose body is exactly "Sign
in first.\n" as an ended session and re-mints (ded0f68). Fable
(MED-LOW): sound today but unpinned. native_contract_test.go pins the
bytes at every guard that sends them to a native write: homeRequire and
teamRequire (an expired native session), rejectDisabledHome (a native
session with Home off) and a.forbidden's non-HTML answer. Changing the
copy in home.go or team_access.go fails it. No behaviour change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 5931b2bfa1c60414cf2fd6578196f68f239f7781 parent ded0f68
1 file changed, +85 −0
  • internal/sheets/native_contract_test.go +85 −0
diff --git a/internal/sheets/native_contract_test.go b/internal/sheets/native_contract_test.go
new file mode 100644
index 0000000..fb51583
--- /dev/null
+++ b/internal/sheets/native_contract_test.go
@@ -0,0 +1,85 @@
+package sheets
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/home/connect"
+ "amadan.net/rastrillo/rastrillo/sessions"
+)
+
+// A native contract, not copy: the native app's Oficina ID transport
+// (native/apple/Sources/SheetsKit/SheetsOficinaID.swift,
+// SheetsOficinaTransport.signInFirst) reads a 403 whose body is exactly
+// "Sign in first.\n" as an ended session and re-mints it. Every session
+// guard that refuses a non-GET request with it must keep these bytes; a
+// copy edit here strands a native window whose first request is a write.
+const nativeSignInFirst = "Sign in first.\n"
+
+func assertSignInFirst(t *testing.T, where string, rec *httptest.ResponseRecorder) {
+ t.Helper()
+ if rec.Code != http.StatusForbidden || rec.Body.String() != nativeSignInFirst {
+ t.Fatalf("%s = %d %q, want 403 %q (the native transport's re-mint signal)", where, rec.Code, rec.Body.String(), nativeSignInFirst)
+ }
+}
+
+// nativeWrite is the native app's POST: JSON wanted, from the app's origin,
+// with one session cookie.
+func nativeWrite(app *App, cookie *http.Cookie) *httptest.ResponseRecorder {
+ r := httptest.NewRequest("POST", testOrigin+"/sheets", strings.NewReader(url.Values{"title": {"x"}}.Encode()))
+ r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ r.Header.Set("Accept", "application/json")
+ r.Header.Set("Origin", testOrigin)
+ r.AddCookie(cookie)
+ rec := httptest.NewRecorder()
+ app.Router().ServeHTTP(rec, r)
+ return rec
+}
+
+func TestNativeContractHomeRequireSaysSignInFirst(t *testing.T) {
+ f := newHomeFixture(t)
+ f.local(t, signedInAddress)
+ f.link(t, "pr_linked")
+ _, answer := f.nativeExchange(t, "pr_linked", nil)
+ f.age(t, answer, connect.NativeLifetime+time.Hour)
+ assertSignInFirst(t, "homeRequire, expired native session", nativeWrite(f.app, &http.Cookie{Name: answer.Name, Value: answer.Value}))
+}
+
+func TestNativeContractTeamRequireSaysSignInFirst(t *testing.T) {
+ f := newTeamFixture(t)
+ b := teamSubject('b')
+ f.dir.Set(b, "member")
+ _, answer := f.nativeExchange(t, b, map[string]string{"choice": "new"})
+ f.age(t, answer, connect.NativeLifetime+time.Hour)
+ assertSignInFirst(t, "teamRequire, expired native session", nativeWrite(f.app, &http.Cookie{Name: answer.Name, Value: answer.Value}))
+}
+
+func TestNativeContractRejectDisabledHomeSaysSignInFirst(t *testing.T) {
+ app, _ := appWithOrigin(t, testOrigin)
+ d, err := app.db.DB()
+ if err != nil {
+ t.Fatal(err)
+ }
+ s, err := sessions.New(sessions.Config{DB: d, Origin: testOrigin, SigninPath: "/signin"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ token, err := s.Mint(sessions.Session{Subject: "pr_native", Method: connect.NativeMethod, AuthTime: time.Now()})
+ if err != nil {
+ t.Fatal(err)
+ }
+ assertSignInFirst(t, "rejectDisabledHome, native session with Home off", nativeWrite(app, &http.Cookie{Name: s.CookieName(), Value: token}))
+}
+
+func TestNativeContractForbiddenSaysSignInFirst(t *testing.T) {
+ app, _ := appWithOrigin(t, testOrigin)
+ r := httptest.NewRequest("POST", testOrigin+"/sheets", nil)
+ r.Header.Set("Accept", "application/json")
+ rec := httptest.NewRecorder()
+ app.forbidden(rec, r)
+ assertSignInFirst(t, "a.forbidden, non-HTML", rec)
+}