forked from UsefulSoftwareCo/executor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth-popup.test.ts
More file actions
343 lines (317 loc) · 12.2 KB
/
Copy pathoauth-popup.test.ts
File metadata and controls
343 lines (317 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// ---------------------------------------------------------------------------
// Fidelity tests for the OAuth popup HTML generator + callback wrapper.
// Locks in the escaping rules, postMessage/BroadcastChannel behavior, and
// the completeOAuth-to-popup-payload conversion semantics so the google-
// discovery port is provably behavior-preserving.
// ---------------------------------------------------------------------------
import { describe, expect, it } from "@effect/vitest";
import { Data, Effect, Schema } from "effect";
import { encodeOAuthCallbackState } from "@executor-js/sdk";
import {
OAUTH_POPUP_MESSAGE_TYPE,
popupDocument,
runOAuthCallback,
type OAuthPopupResult,
} from "./oauth-popup";
type GoogleAuth = {
kind: "oauth2";
accessTokenSecretId: string;
refreshTokenSecretId: string | null;
};
const DomainErrorShape = Schema.Struct({
_tag: Schema.Literal("DomainError"),
message: Schema.String,
});
const isDomainError = Schema.is(DomainErrorShape);
// ---------------------------------------------------------------------------
// popupDocument
// ---------------------------------------------------------------------------
describe("popupDocument", () => {
const successPayload: OAuthPopupResult<GoogleAuth> = {
type: OAUTH_POPUP_MESSAGE_TYPE,
ok: true,
sessionId: "session-abc",
kind: "oauth2",
accessTokenSecretId: "secret_1",
refreshTokenSecretId: "secret_2",
};
it("renders a success page with Connected title and the green check icon", () => {
const html = popupDocument(successPayload, "channel-x");
expect(html).toContain("<title>Connected</title>");
expect(html).toContain("#22c55e");
expect(html).toContain("Authentication complete");
});
it("renders a failure page with the error text escaped", () => {
const html = popupDocument(
{
type: OAUTH_POPUP_MESSAGE_TYPE,
ok: false,
sessionId: null,
error: "Token endpoint returned 400 <script>alert(1)</script>",
},
"channel-x",
);
expect(html).toContain("<title>Connection failed</title>");
expect(html).toContain("#ef4444");
expect(html).toContain("Token endpoint returned 400 <script>alert(1)</script>");
expect(html).not.toContain("<script>alert(1)</script>");
});
it("renders a collapsible details disclosure when errorDetails is present", () => {
const html = popupDocument(
{
type: OAUTH_POPUP_MESSAGE_TYPE,
ok: false,
sessionId: null,
error: "Could not complete authentication",
errorDetails: "HTTP 201 Created from https://api.supabase.com/v1/oauth/token",
},
"channel-x",
);
expect(html).toContain("Could not complete authentication");
expect(html).toContain("<details");
expect(html).toContain("<summary");
expect(html).toContain("Details</summary>");
expect(html).toContain("HTTP 201 Created from https://api.supabase.com/v1/oauth/token");
});
it("escapes HTML inside errorDetails", () => {
const html = popupDocument(
{
type: OAUTH_POPUP_MESSAGE_TYPE,
ok: false,
sessionId: null,
error: "Failed",
errorDetails: "<script>alert('xss')</script>",
},
"channel-x",
);
expect(html).toContain("<script>alert('xss')</script>");
// Ensure the raw script tag does not appear inside the rendered <pre>.
const preMatch = /<pre[^>]*>([^<]*)<\/pre>/.exec(html);
expect(preMatch).not.toBeNull();
expect(preMatch![1]).not.toContain("<script>");
});
it("omits the details disclosure when errorDetails matches error", () => {
const html = popupDocument(
{
type: OAUTH_POPUP_MESSAGE_TYPE,
ok: false,
sessionId: null,
error: "Failed",
errorDetails: "Failed",
},
"channel-x",
);
expect(html).not.toContain("<details");
});
it("HTML-escapes the BroadcastChannel name so attacker-controlled names cannot break out", () => {
const html = popupDocument(successPayload, 'evil"name');
expect(html).toContain('new BroadcastChannel("evil"name")');
});
it("escapes < > & in the serialized script payload to prevent </script> breakout", () => {
const html = popupDocument(
{
type: OAUTH_POPUP_MESSAGE_TYPE,
ok: false,
sessionId: null,
error: "</script><img/src=x onerror=alert(1)>",
},
"channel-x",
);
// The raw `</script>` must not appear in the inline script literal.
const scriptLiteralMatch = /const p=(\{.*?\});/.exec(html);
expect(scriptLiteralMatch).not.toBeNull();
const scriptLiteral = scriptLiteralMatch![1]!;
expect(scriptLiteral).not.toContain("</script>");
expect(scriptLiteral).toContain("\\u003c/script\\u003e");
});
it("posts to window.opener AND falls back to BroadcastChannel with the given channel name", () => {
const html = popupDocument(successPayload, "executor:openapi-oauth-result");
expect(html).toContain("window.opener.postMessage(p,window.location.origin)");
expect(html).toContain('new BroadcastChannel("executor:openapi-oauth-result")');
expect(html).toContain("window.close()");
});
it("includes dark-mode CSS", () => {
const html = popupDocument(successPayload, "c");
expect(html).toContain("@media(prefers-color-scheme:dark)");
});
});
// ---------------------------------------------------------------------------
// runOAuthCallback
// ---------------------------------------------------------------------------
describe("runOAuthCallback", () => {
it("renders a success popup when completeOAuth succeeds", async () => {
const html = await Effect.runPromise(
runOAuthCallback<GoogleAuth, never, never>({
complete: () =>
Effect.succeed({
kind: "oauth2",
accessTokenSecretId: "s1",
refreshTokenSecretId: "s2",
}),
urlParams: { state: "session-xyz", code: "abc" },
toErrorMessage: () => ({ short: "should not reach" }),
channelName: "channel-x",
}),
);
expect(html).toContain("<title>Connected</title>");
expect(html).toContain("session-xyz");
expect(html).toContain("s1");
});
it("passes code, error, and the regional domain through to the complete callback", async () => {
const received: Array<{
state: string;
code: string | null;
error: string | null;
callbackDomain: string | null;
}> = [];
await Effect.runPromise(
runOAuthCallback<GoogleAuth, never, never>({
complete: (params) => {
received.push(params);
return Effect.succeed({
kind: "oauth2",
accessTokenSecretId: "s",
refreshTokenSecretId: null,
});
},
// Multi-site providers (Datadog) echo the org's region back as `domain`,
// which `runOAuthCallback` surfaces as `callbackDomain`.
urlParams: { state: "s1", code: "code1", error: null, domain: "us5.datadoghq.com" },
toErrorMessage: () => ({ short: "" }),
channelName: "c",
}),
);
expect(received).toEqual([
{ state: "s1", code: "code1", error: null, callbackDomain: "us5.datadoghq.com" },
]);
});
it("completes wrapped callback state with the raw OAuth session state", async () => {
const providerState = encodeOAuthCallbackState({ state: "session-raw", orgSlug: "default" });
const received: Array<{ state: string }> = [];
const html = await Effect.runPromise(
runOAuthCallback<GoogleAuth, never, never>({
complete: (params) => {
received.push({ state: params.state });
return Effect.succeed({
kind: "oauth2",
accessTokenSecretId: "s",
refreshTokenSecretId: null,
});
},
urlParams: { state: providerState, code: "code1" },
toErrorMessage: () => ({ short: "" }),
channelName: "c",
}),
);
expect(received).toEqual([{ state: "session-raw" }]);
expect(html).toContain('"sessionId":"session-raw"');
expect(html).not.toContain(`"sessionId":"${providerState}"`);
});
it("falls back to `site` for the regional domain and defaults to null", async () => {
const received: Array<{ callbackDomain: string | null }> = [];
const complete = (params: { callbackDomain: string | null }) => {
received.push(params);
return Effect.succeed({
kind: "oauth2" as const,
accessTokenSecretId: "s",
refreshTokenSecretId: null,
});
};
// `site` is the full-origin variant; used only when `domain` is absent.
await Effect.runPromise(
runOAuthCallback<GoogleAuth, never, never>({
complete,
urlParams: { state: "s1", code: "c", site: "https://eu1.datadoghq.com" },
toErrorMessage: () => ({ short: "" }),
channelName: "c",
}),
);
// No region hints at all -> null (standard single-site providers).
await Effect.runPromise(
runOAuthCallback<GoogleAuth, never, never>({
complete,
urlParams: { state: "s2", code: "c" },
toErrorMessage: () => ({ short: "" }),
channelName: "c",
}),
);
expect(received[0]!.callbackDomain).toBe("https://eu1.datadoghq.com");
expect(received[1]!.callbackDomain).toBeNull();
});
it("renders provider OAuth errors without invoking completeOAuth", async () => {
let completeCalls = 0;
const complete = () => {
completeCalls += 1;
return Effect.succeed({
kind: "oauth2" as const,
accessTokenSecretId: "s",
refreshTokenSecretId: null,
});
};
const html = await Effect.runPromise(
runOAuthCallback<GoogleAuth, never, never>({
complete,
urlParams: {
state: "s1",
error: "invalid_scope",
error_description: "unknown scope wizard_session:write",
},
toErrorMessage: () => ({ short: "" }),
channelName: "c",
}),
);
expect(completeCalls).toBe(0);
expect(html).toContain("<title>Connection failed</title>");
expect(html).toContain("OAuth provider rejected authorization");
expect(html).toContain("invalid_scope");
expect(html).toContain("unknown scope wizard_session:write");
});
it("renders a failure popup when completeOAuth fails and uses toErrorMessage", async () => {
class DomainError extends Data.TaggedError("DomainError")<{
readonly message: string;
}> {}
const html = await Effect.runPromise(
runOAuthCallback<GoogleAuth, DomainError, never>({
complete: () => Effect.fail(new DomainError({ message: "Code expired" })),
urlParams: { state: "s1" },
toErrorMessage: (error) => {
if (!isDomainError(error)) return { short: "unknown" };
// oxlint-disable-next-line executor/no-unknown-error-message -- boundary: schema guard narrows the unknown popup callback error to the public test message
return { short: "Auth failed", details: error.message };
},
channelName: "c",
}),
);
expect(html).toContain("<title>Connection failed</title>");
expect(html).toContain("Auth failed");
expect(html).toContain("<summary");
expect(html).toContain("Code expired");
});
it("omits the details disclosure when details match the short message", async () => {
class DomainError extends Data.TaggedError("DomainError")<{
readonly message: string;
}> {}
const html = await Effect.runPromise(
runOAuthCallback<GoogleAuth, DomainError, never>({
complete: () => Effect.fail(new DomainError({ message: "boom" })),
urlParams: { state: "s1" },
toErrorMessage: () => ({ short: "Same", details: "Same" }),
channelName: "c",
}),
);
expect(html).toContain("Same");
expect(html).not.toContain("<details");
});
it("never rejects — even defects are rendered as a failure popup", async () => {
const html = await Effect.runPromise(
runOAuthCallback<GoogleAuth, never, never>({
complete: () => Effect.die("boom"),
urlParams: { state: "s1" },
toErrorMessage: () => ({ short: "transport error" }),
channelName: "c",
}),
);
expect(html).toContain("<title>Connection failed</title>");
expect(html).toContain("transport error");
});
});