diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ef2bba..d9a5b13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,30 @@ # Changelog +## Unreleased (to be 1.0.0-alpha.13) + +### Changed + +- **The advice on `X-Requested-With` for a cookie-session browser app + was wrong and is replaced** (documentation only; no behaviour + changes). `createFetchTransport`'s comment on `credentials` said to + send `X-Requested-With: XMLHttpRequest` "so the server answers 401 + rather than a login page". A browser-app consumer ended a real + session (one product release, OIDC sign-in) and saw the opposite + effect: without the header a 401, with it a 403, both with the same + `WWW-Authenticate: Bearer` challenge and body, neither a redirect. + Since `onUnauthorized` runs for `unauthorizedStatuses`, default + `[401]`, an app that followed the advice never had its hook called + when a session ended. Migration: if you send that header, set + `unauthorizedStatuses: [401, 403]` and have the hook check that the + session is really gone before it prompts (a one-row read with + `reauthorize: false`), because a 403 may also be a permission denial; + or stop sending the header. The built-in transport never sends it by + itself. A write refused with that 403 was not applied and landed once + when replayed, so what is known about `"retry-write"` extends to it. + Not checked: other releases, other ways of signing in, and whether a + 403 for a genuine permission denial carries the same challenge (so the + SDK still classifies every 403 as `kind: "forbidden"`). + ## 1.0.0-alpha.12 ### Breaking diff --git a/README.md b/README.md index d0c26bc..1f22afd 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ await client.send([{ from: "Story:1077", update: { Status: done._oid } }]); - **Resolve before you write.** A team's process IS enforced on a write: a status outside it is refused, as a command failure `Violation'PrimaryWorkitemCannotHaveInvalidStatus`. A scheme's selection is NOT: a status the project's scheme does not select is accepted, with no violation at all. Nothing but this lookup stops a write from setting a value the project does not use. - **`lists.value` matches the whole name**, ignoring case and surrounding space. `match: "prefix"` also accepts the start of the name or of a word in it ("prog" finds "In Progress") when no name matches whole. It never picks among several: no match is an `ApiError` coded `NO_SUCH_VALUE`, more than one is `AMBIGUOUS_VALUE`, and both carry the candidates as rows in `error.details.values` (every valid value, or the ones that matched), ready to offer. - **Only lists.** The server's endpoint takes no page and no filter and would answer every member, team or iteration in the project, so for `Owners`, `Team`, `Timebox` and any relation that is not to a list type, `client.lists` throws a `UsageError` before sending anything. There is no "valid values" call for those: read them with `client.query({ from: "Member", filter, page })`, bounded like every other read. -- **Costs and limits.** One request per attribute, sent one after another, and on a cold schema cache the type loads that tell a list from another relation (for Status: `StoryStatus`, `Status`, `List`); nothing is cached, because an administrator can change a scheme at any time. `select` takes attributes of the value itself (`Order`, `RollupState`), not paths and not dates, and every value arrives as a string. On an instance without the endpoint the call fails as the server's 404; it does not fall back to every value of the list type, which would be a different answer. +- **Costs and limits.** One request per attribute, sent one after another, and on a cold schema cache the type loads that tell a list from another relation (for Status: `StoryStatus`, `Status`, `List`; a test that serves captured meta.v1 types must serve that whole chain, and the error names the type it could not load when one is missing); nothing is cached, because an administrator can change a scheme at any time. `select` takes attributes of the value itself (`Order`, `RollupState`), not paths and not dates, and every value arrives as a string. On an instance without the endpoint the call fails as the server's 404; it does not fall back to every value of the list type, which would be a different answer. - `client.fields.describe(type, scope).lists` says which of a form's attributes are list-typed and to which list type each points. ### Reference @@ -171,7 +171,7 @@ Changes between alphas are in `CHANGELOG.md`, each with its migration line. - `normalizeQuery(q)` - a query written with rest-1's spellings (`sel`, a textual `where`, comma-joined lists, a `"size,start"` page) as the `QueryBody` every read takes - Errors: everything thrown is an `ApiError` with a `kind` (`auth`, `forbidden`, `notFound`, `invalid`, `transient`, `aborted`, `commandFailure`, `conflict`, `protocol`, `usage`, `unknown`). A mistake caught before any request is a `UsageError`: `kind: "usage"` means nothing was sent (`status` is 0), so the fix is in the call, not on the server - `TypeCache` - one instance's loaded type definitions, shared by many clients (`new AgilityApiClient(config, { types })`); `TypeCache.fromMetamodel(metamodel)` seeds one so nothing is loaded -- `createFetchTransport` - configurable HTTP transport: `credentials` for same-origin cookie auth, `onUnauthorized` to re-authenticate on a 401 (resends idempotent requests once), and a per-request `signal`; `client.withSignal(signal)` ties a whole client to one +- `createFetchTransport` - configurable HTTP transport: `credentials` for same-origin cookie auth, `onUnauthorized` to re-authenticate on a 401 (resends idempotent requests once; an app that sends `X-Requested-With: XMLHttpRequest` gets a 403 for an ended session and needs `unauthorizedStatuses: [401, 403]`, see the option's comment), and a per-request `signal`; `client.withSignal(signal)` ties a whole client to one - **Timeouts are the client's business with any transport**: each attempt has `timeoutMs` (default 60000; 0 for none), and at the deadline the request rejects with an `ApiError` coded `TIMEOUT`, whether or not a custom transport lets go of it (the transport is handed a `signal` that fires then; a late answer is discarded). A timed-out write may still have been applied. A custom transport that waits for a person inside `request` (a sign-in prompt before it replays the request) sets `timeoutMs: 0`: the deadline cannot tell a person from a slow network. The built-in transport's `onUnauthorized` hook runs outside the deadline - Every `ApiError` the client lets out says what was asked: `error.request` is `{ method, path, idempotent }` (never the body or headers). `client.explain(error)` uses it: a write that got no answer, or a server failure for one, is explained as "Agility did not confirm the change. It may or may not have been made: check before trying again." with `retrySafe: false`, where a read is `retrySafe: true`, except a read the server itself gave up on (`SERVER_TIMEOUT`), which is `false`: the same request would cost and fail the same. The two questions after a failure are two fields: `retrySafe` (may the same request be sent again as it is?) and `outcome` (is it known whether the change was made?). `outcome` is `"unknown"` for a write that got no confirmation (no answer, a timeout, a 5xx: look before acting), `"notApplied"` where the server answered a refusal before doing the work (a 4xx or a 429 in answer to a write, a conflict, and each failed command in the explanation of a batch, which says nothing of the batch's other commands: those are in the Outcome), and `null` for a read or when the error does not say what was asked. A guarded write (`sendGuarded`) is a write here too: it is never sent again by the client, because a second send of one that landed applies nothing and reports a conflict, which would tell you someone else changed the asset when the change was yours. When one gets no answer (`outcome: "unknown"`), read the asset again: you see your change or you do not, and the guard makes sending it again safe. Decide on `retrySafe`, not on the sentence; it is `null` when the server answered, or when the error carries no stamp (one from a `/wire` function on a bare transport) - A config key that is not one the factory takes (`new AgilityApiClient`, `createFetchTransport`, `new FakeAgilityClient`) is refused at construction with a `UsageError` naming it, with the nearest known key when it looks like a typo. Nothing is ignored silently, and no value is repeated in the message. diff --git a/examples/09-browser-session.ts b/examples/09-browser-session.ts index a542d2d..82ab2ab 100644 --- a/examples/09-browser-session.ts +++ b/examples/09-browser-session.ts @@ -37,6 +37,11 @@ async function signInAgain(): Promise { const client = new AgilityApiClient({ baseUrl: "https://agility.example.com/MyInstance", credentials: "include", // send the session cookie; no accessToken + // No `X-Requested-With` header here, so an ended session answers 401 and + // the hook below runs. An app that sends `X-Requested-With: + // XMLHttpRequest` was seen to get a 403 for an ended session instead: it + // adds `unauthorizedStatuses: [401, 403]`, and its hook first checks that + // the session is really gone, since a 403 may also be a permission denial. onUnauthorized: async ({ retryable }) => { if (!(await signInAgain())) return "fail"; // "retry" resends what is safe to send twice: a GET, a read. Never a diff --git a/src/core/fetchTransport.ts b/src/core/fetchTransport.ts index b27e562..bde84ab 100644 --- a/src/core/fetchTransport.ts +++ b/src/core/fetchTransport.ts @@ -43,9 +43,21 @@ export interface FetchTransportConfig { /** * fetch's credentials mode. A browser app on the instance's own origin * that authenticates with the session cookie sets "same-origin" or - * "include" (pair it with defaultHeaders { "X-Requested-With": - * "XMLHttpRequest" } so the server answers 401 rather than a login page). - * Left unset, fetch's default applies. + * "include". Left unset, fetch's default applies. + * + * Whether to also send `X-Requested-With: XMLHttpRequest` (through + * `defaultHeaders`; this transport never sends it by itself) decides + * which status an ended session answers with. A consumer saw, on one + * product release with OIDC sign-in, a session that had expired answer + * 401 without that header and 403 with it, both with the same + * `WWW-Authenticate: Bearer` challenge and the same body, and neither + * with a redirect to a login page. So: without the header, an ended + * session is a 401 and `onUnauthorized` runs by default; with it, set + * `unauthorizedStatuses: [401, 403]`, or the hook is never called. + * (Earlier releases of this comment advised the header "so the server + * answers 401 rather than a login page"; that is not what was seen.) + * Not checked: other releases, other ways of signing in, and whether a + * 403 for a genuine permission denial carries the same challenge. */ credentials?: RequestCredentials; /** @@ -65,7 +77,16 @@ export interface FetchTransportConfig { * again: it means "retry-write" is the answer that resumes it. */ onUnauthorized?: (context: UnauthorizedContext) => UnauthorizedDecision | Promise; - /** Which statuses call onUnauthorized. Default [401]; a cookie-session app may add 403. */ + /** + * Which statuses call onUnauthorized. Default [401]. An app that sends + * `X-Requested-With: XMLHttpRequest` needs [401, 403]: an ended session + * was seen to answer 403 to a request with that header (see + * `credentials`). A 403 is also what a permission denial may answer, + * and it is not known whether the response tells the two apart, so a + * hook called for a 403 should ask whether the session is still alive + * (a one-row read sent with `reauthorize: false`) before it prompts + * anyone, and answer "fail" when it is. + */ unauthorizedStatuses?: readonly number[]; } @@ -88,8 +109,11 @@ export interface FetchTransportConfig { * an exact-name query afterwards); with the expired * credential a rest-1 attribute write was refused the * same way and not applied, and a query.v1 read got the - * same 401. No 403 and no redirect was seen. What is - * not known: a 403, other releases, and other ways of + * same 401. With `X-Requested-With: XMLHttpRequest` + * on the request the same ended session answered 403, + * and a write refused so was not applied either. No + * redirect was seen. What is not known: a 403 for a + * permission denial, other releases, and other ways of * signing in. The SDK's own tests run where every * request is authenticated and check none of it, so * beyond those cases it remains your assertion. Answer it