Skip to content

Commit 008a155

Browse files
committed
feat(auth): include credentials for fetch request
1 parent 0226462 commit 008a155

7 files changed

Lines changed: 38 additions & 22 deletions

File tree

packages/fxa-auth-client/lib/client.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,27 @@ export default class AuthClient {
264264
}
265265

266266
extraHeaders.set('Content-Type', 'application/json');
267+
268+
const requestOptions: RequestInit = {
269+
method,
270+
headers: extraHeaders,
271+
body: cleanStringify(payload),
272+
};
273+
274+
// For specific endpoints + HTTPS, upgrade credentials to include cookies for WAF challenges
275+
const includeCredentials = [
276+
'/account/create',
277+
'/password/forgot/send_otp',
278+
].some((endpoint) => path.startsWith(endpoint));
279+
280+
if (includeCredentials && new URL(this.uri).protocol === 'https:') {
281+
requestOptions.credentials = 'include';
282+
}
283+
267284
try {
268285
const response = await fetchOrTimeout(
269286
this.url(path),
270-
{
271-
method,
272-
headers: extraHeaders,
273-
body: cleanStringify(payload),
274-
},
287+
requestOptions,
275288
this.timeout
276289
);
277290
const result = JSON.parse(await response.text());

packages/fxa-auth-client/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,11 @@
4545
},
4646
"author": "",
4747
"license": "MPL-2.0",
48-
"dependencies": {
49-
"node-fetch": "^2.6.7"
50-
},
5148
"devDependencies": {
5249
"@types/assert": "^1.5.4",
5350
"@types/fast-text-encoding": "^1",
5451
"@types/mocha": "^10",
5552
"@types/node": "^22.13.5",
56-
"@types/node-fetch": "^2.5.7",
5753
"@types/prettier": "^2",
5854
"esbuild": "^0.17.15",
5955
"esbuild-register": "^3.5.0",

packages/fxa-auth-client/server.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import http from 'http';
22
import https from 'https';
3-
import fetch, { Headers } from 'node-fetch';
43
import AuthClient from './lib/client';
54

65
http.globalAgent = new http.Agent({
@@ -10,11 +9,6 @@ https.globalAgent = new https.Agent({
109
keepAlive: true,
1110
});
1211

13-
// @ts-ignore
14-
global.fetch = fetch;
15-
// @ts-ignore
16-
global.Headers = Headers;
17-
1812
export default AuthClient;
1913
export * from './lib/client';
2014
export * from './lib/hawk';

packages/fxa-auth-client/tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
4-
"types": ["mocha"]
4+
"types": ["mocha", "node"]
55
},
66
"include": ["./lib/**/*", "./server.ts"],
77
"exclude": ["dist", "node_modules"]

packages/fxa-auth-server/lib/routes/account.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,7 @@ export class AccountHandler {
417417
service: form.service || query.service,
418418
});
419419
} else {
420-
console.debug('falling back')
421-
const sent = await this.mailer.sendVerifyEmail([], account, {
420+
await this.mailer.sendVerifyEmail([], account, {
422421
code: account.emailCode,
423422
service: form.service || query.service,
424423
redirectTo: form.redirectTo,
@@ -440,7 +439,6 @@ export class AccountHandler {
440439
uaDeviceType: sessionToken.uaDeviceType,
441440
uid: sessionToken.uid,
442441
});
443-
console.debug('falling back sent!', sent);
444442
}
445443
}
446444
}
@@ -2321,12 +2319,21 @@ export const accountRoutes = (
23212319
statsd,
23222320
authServerCacheRedis
23232321
);
2322+
2323+
// Enable CORS credentials only when using explicit origins (not wildcard, per CORS spec)
2324+
const enableCredentials = config.corsOrigin && config.corsOrigin[0] !== '*';
2325+
23242326
const routes = [
23252327
{
23262328
method: 'POST',
23272329
path: '/account/create',
23282330
options: {
23292331
...ACCOUNT_DOCS.ACCOUNT_CREATE_POST,
2332+
...(enableCredentials && {
2333+
cors: {
2334+
credentials: true,
2335+
},
2336+
}),
23302337
validate: {
23312338
query: isA.object({
23322339
keys: isA.boolean().optional().description(DESCRIPTION.keys),

packages/fxa-auth-server/lib/routes/password.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ module.exports = function (
7979
: db.updatePasswordForgotToken(passwordForgotToken);
8080
}
8181

82+
// Enable CORS credentials only when using explicit origins (not wildcard, per CORS spec)
83+
const enableCredentials = config.corsOrigin && config.corsOrigin[0] !== '*';
84+
8285
const routes = [
8386
{
8487
method: 'POST',
@@ -995,6 +998,11 @@ module.exports = function (
995998
path: '/password/forgot/send_otp',
996999
options: {
9971000
...PASSWORD_DOCS.PASSWORD_FORGOT_SEND_OTP_POST,
1001+
...(enableCredentials && {
1002+
cors: {
1003+
credentials: true,
1004+
},
1005+
}),
9981006
validate: {
9991007
query: isA.object({
10001008
service: validators.service.description(DESCRIPTION.serviceRP),

yarn.lock

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22944,7 +22944,7 @@ __metadata:
2294422944
languageName: node
2294522945
linkType: hard
2294622946

22947-
"@types/node-fetch@npm:^2.5.7, @types/node-fetch@npm:^2.6.1, @types/node-fetch@npm:^2.6.4":
22947+
"@types/node-fetch@npm:^2.6.1, @types/node-fetch@npm:^2.6.4":
2294822948
version: 2.6.12
2294922949
resolution: "@types/node-fetch@npm:2.6.12"
2295022950
dependencies:
@@ -35436,15 +35436,13 @@ __metadata:
3543635436
"@types/fast-text-encoding": "npm:^1"
3543735437
"@types/mocha": "npm:^10"
3543835438
"@types/node": "npm:^22.13.5"
35439-
"@types/node-fetch": "npm:^2.5.7"
3544035439
"@types/prettier": "npm:^2"
3544135440
esbuild: "npm:^0.17.15"
3544235441
esbuild-register: "npm:^3.5.0"
3544335442
eslint: "npm:^8.38.0"
3544435443
eslint-config-react-app: "npm:^7.0.1"
3544535444
fast-text-encoding: "npm:^1.0.4"
3544635445
mocha: "npm:^10.4.0"
35447-
node-fetch: "npm:^2.6.7"
3544835446
prettier: "npm:^3.5.3"
3544935447
typescript: "npm:5.5.3"
3545035448
languageName: unknown

0 commit comments

Comments
 (0)