| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "log/slog" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/home/internal/home/logtest" |
| 12 | ) |
| 13 | |
| 14 | // TestNoContentInLogsNative is gate 9 extended to the native endpoints (§4: |
| 15 | // "Gates 1, 2 and 9 extend to this table"). TestNoContentInLogs walks the |
| 16 | // legacy role's router, where none of these mount, and its app logs to a |
| 17 | // discarded logger; this one captures both the app's logger and the |
| 18 | // default, drives a full code sign-in and assert with a canary address, |
| 19 | // then sends canary-laden garbage to every native route on both origins. |
| 20 | func TestNoContentInLogsNative(t *testing.T) { |
| 21 | rec := logtest.NewRecorder() |
| 22 | prevLogger, prevWriter, prevFlags := slog.Default(), log.Writer(), log.Flags() |
| 23 | slog.SetDefault(slog.New(rec.Handler())) |
| 24 | t.Cleanup(func() { |
| 25 | slog.SetDefault(prevLogger) |
| 26 | log.SetOutput(prevWriter) |
| 27 | log.SetFlags(prevFlags) |
| 28 | }) |
| 29 | |
| 30 | f := newMountedChooser(t) |
| 31 | app := f.p.credential |
| 32 | app.logger = slog.New(rec.Handler()) |
| 33 | f.p.directory.logger = slog.New(rec.Handler()) |
| 34 | address := strings.ToLower(logtest.Canaries[0]) |
| 35 | |
| 36 | d := newDevice(t) |
| 37 | grant, _ := d.signInByCode(t, app, address) |
| 38 | verifier, challenge := verifierFor(30) |
| 39 | w := d.assert(t, app, grant, directoryOrigin, challenge) |
| 40 | directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(decodeReply(t, w)["assertion"].(string), verifier), nil, nil) |
| 41 | start := d.start(t, app, address) |
| 42 | d.verify(t, app, start, "000000") |
| 43 | app.secrets.Mailer = failingMailer{} |
| 44 | e := newDevice(t) |
| 45 | if w := e.call(t, app, nativePrefix+"/code/start", map[string]string{"email": address, "device_key": e.public(), "platform": "ios"}, ""); w.Code != 503 { |
| 46 | t.Fatal("undeliverable start", w.Code) |
| 47 | } |
| 48 | |
| 49 | body := `{"email":"` + address + `","device_key":"` + logtest.Canaries[1] + `","platform":"` + logtest.Canaries[2] + `","start":"` + logtest.Canaries[1] + `","code":"` + logtest.Canaries[2] + `","pending":"` + logtest.Canaries[1] + `","aud":"https://` + logtest.Canaries[1] + `.example","challenge":"` + logtest.Canaries[2] + `"}` |
| 50 | for _, path := range []string{"/code/start", "/code/resend", "/code/verify", "/passkey/begin", "/passkey/finish", "/assert", "/signout"} { |
| 51 | r := httptest.NewRequest("POST", Origin+nativePrefix+path, strings.NewReader(body)) |
| 52 | r.Header.Set("Content-Type", "application/json") |
| 53 | r.Header.Set("Oficina-Proof", logtest.Canaries[0]+"."+logtest.Canaries[1]) |
| 54 | r.Header.Set("Authorization", "OficinaID "+logtest.Canaries[2]) |
| 55 | r.Header.Set("X-Forwarded-For", logtest.Canaries[0]) |
| 56 | app.Router().ServeHTTP(httptest.NewRecorder(), r) |
| 57 | } |
| 58 | for _, path := range []string{nativeExchangePath, nativeDirectorySignout, nativeTeamsPath} { |
| 59 | method := "POST" |
| 60 | if path == nativeTeamsPath { |
| 61 | method = "GET" |
| 62 | } |
| 63 | directoryCall(t, f, method, path, body, &http.Cookie{Name: f.p.directory.sessions.CookieName(), Value: logtest.Canaries[1]}, map[string]string{"X-Forwarded-For": logtest.Canaries[0]}) |
| 64 | } |
| 65 | rec.AssertNoContent(t) |
| 66 | } |
| 67 | |