Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth-server): handle preflight requests #1040

Merged
merged 7 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

point 1 refers to the oauth config object that can be accessed here.

If the value of the Origin header is not a case-sensitive match for any of the values in list of origins do not set any additional headers and terminate this set of steps.
Always matching is acceptable since the list of origins can be unbounded.

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
Loading