Skip to content

Commit

Permalink
fix(auth-server): handle preflight requests (#1040)
Browse files Browse the repository at this point in the history
* fix(auth-server): handle preflight requests
  • Loading branch information
cristiand391 authored Mar 22, 2024
1 parent 37302d3 commit 7a109d9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/webOAuthServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ export class WebOAuthServer extends AsyncCreatable<WebOAuthServer.Options> {
const errMessage = messages.getMessage(errName, [url.pathname]);
reject(new SfError(errMessage, errName));
}
} else if (
request.method === 'OPTIONS' &&
request.headers['access-control-request-private-network'] === 'true' &&
request.headers['access-control-request-method']
) {
this.webServer.handlePreflightRequest(response);
} else {
this.webServer.sendError(405, 'Unsupported http methods', response);
const errName = 'invalidRequestMethod';
Expand Down Expand Up @@ -399,6 +405,22 @@ export class WebServer extends AsyncCreatable<WebServer.Options> {
this.redirectStatus.emit('complete');
}

/**
* Preflight request:
*
* https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
* https://www.w3.org/TR/2020/SPSD-cors-20200602/#resource-preflight-requests
*/
public handlePreflightRequest(response: http.ServerResponse): void {
// We don't validate the origin here because:
// 1. The default login URL (login.salesforce.com) will not match after a redirect or if user choose a custom domain in login.
// 2. There's no fixed list of auth URLs we could check against.
response.statusCode = 204; // No Content response
response.setHeader('Access-Control-Allow-Methods', 'GET');
response.setHeader('Access-Control-Request-Headers', 'GET');
response.end();
}

public async handleSuccess(response: http.ServerResponse): Promise<void> {
return this.handleRedirect(response, '/OauthSuccess');
}
Expand Down

0 comments on commit 7a109d9

Please sign in to comment.