Synthesize DigitalOcean API CORS preflight responses (#477)

This commit is contained in:
alalamav 2019-08-22 13:10:03 -04:00 committed by GitHub
parent 0445af0a95
commit 5d083bc22c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -150,13 +150,29 @@ function getWebAppUrl() {
// Digital Ocean stopped sending 'Acces-Control-Allow-Origin' headers in some API responses
// (i.e. v2/droplets). As a workaround, intercept DO API requests and preemptively inject the
// header to allow our origin.
// header to allow our origin. Additionally, some OPTIONS requests return 403. Modify the response
// status code and inject CORS response headers.
function workaroundDigitalOceanApiCors() {
const headersFilter = {urls: ['https://api.digitalocean.com/*']};
electron.session.defaultSession.webRequest.onHeadersReceived(
// tslint:disable-next-line:no-any
headersFilter, (details: any, callback: Function) => {
details.responseHeaders['access-control-allow-origin'] = ['outline://web_app'];
if (details.method === 'OPTIONS') {
details.responseHeaders['access-control-allow-origin'] = ['outline://web_app'];
if (details.statusCode === 403) {
details.statusCode = 200;
details.statusLine = 'HTTP/1.1 200';
details.responseHeaders['status'] = ['200'];
details.responseHeaders['access-control-allow-headers'] =
[details.headers['Access-Control-Request-Headers']];
details.responseHeaders['access-control-allow-credentials'] = ['true'];
details.responseHeaders['access-control-allow-methods'] =
['GET, POST, PUT, PATCH, DELETE, OPTIONS'];
details.responseHeaders['access-control-expose-headers'] =
['RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset, Total, Link'];
details.responseHeaders['access-control-max-age'] = ['86400'];
}
}
callback(details);
});
}