Skip to content

Commit

Permalink
fix: local file maybe block by origin null
Browse files Browse the repository at this point in the history
  • Loading branch information
yize committed Jul 12, 2018
1 parent 35066fb commit f2dae21
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "xswitch",
"description": "A proxy tool based on Chrome.extensions",
"author": "yize",
"version": "1.6.0",
"version": "1.6.1",
"main": "src/background.js",
"dependencies": {
"monaco-editor": "^0.13.1"
Expand Down
5 changes: 5 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ chrome.webRequest.onBeforeRequest.addListener(
chrome.webRequest.onHeadersReceived.addListener(details=>window.onHeadersReceivedCallback(details), {
urls: ['<all_urls>']
}, ["blocking", "responseHeaders"]);

chrome.webRequest.onBeforeSendHeaders.addListener(
details=>window.onBeforeSendHeadersCallback(details),
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]);
12 changes: 12 additions & 0 deletions src/defaultData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"proxy": [
[
"//alinw.alicdn.com/platform/daily-test/isDaily.js",
"//alinw.alicdn.com/platform/daily-test/isDaily.json"
],
[
"alinw.alicdn.com",
"g.alicdn.com"
]
]
}
49 changes: 41 additions & 8 deletions src/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,49 @@ window.lastRequestId = null;
window.proxyConfig = {};
window.urls = new Array(200); // for cache
window.isString = string => ({}.toString.call(string) === '[object String]');
window.originRequestId = null;
window.originValue = null;

//Breaking the CORS Limitation
window.onHeadersReceivedCallback = details => {

if (window.proxyDisabled == 'disabled') {
return {};
}

let resHeaders = [];
if(details.responseHeaders && details.responseHeaders.filter){
resHeaders = details.responseHeaders.filter((responseHeader) => {
return !~responseHeader.name.toLowerCase().indexOf('access-control-allow');
})
if (details.responseHeaders && details.responseHeaders.filter) {
resHeaders = details.responseHeaders.filter(responseHeader => {
if (
[
'access-control-allow-origin',
'access-control-allow-credentials',
'access-control-allow-methods'
].indexOf(responseHeader.name.toLowerCase()) < 0
) {
return true;
}
return false;
});
}

resHeaders.push({ name: 'Access-Control-Allow-Origin', value: details.initiator || '*' });

resHeaders.push({
name: 'Access-Control-Allow-Origin',
// when Origin has value null, CORS header must be null.
value: (window.originRequestId === details.requestId ? window.originValue : details.initiator) || '*'
});
resHeaders.push({ name: 'Access-Control-Allow-Credentials', value: 'true' });
resHeaders.push({ name: 'Access-Control-Allow-Headers', value: 'x-requested-with,Content-Type' });
resHeaders.push({
name: 'Access-Control-Allow-Methods',
value: '*'
});

return {
responseHeaders: resHeaders
};
};

window.redirectToMatchingRule = (details) => {
window.redirectToMatchingRule = details => {
const rules = window.proxyConfig.proxy;
let redirectUrl = details.url;

Expand Down Expand Up @@ -66,8 +85,22 @@ window.redirectToMatchingRule = (details) => {
} catch (e) {
console.error('rule match error', e);
}

window.lastRequestId = details.requestId;
return redirectUrl === details.url ? {} : { redirectUrl };
};

window.onBeforeSendHeadersCallback = function (details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {

if (details.requestHeaders[i].name === 'Origin') {
window.originRequestId = details.requestId;
window.originValue = details.requestHeaders[i].value;
break;
}
}

return { requestHeaders: details.requestHeaders };
}

window.onBeforeRequestCallback = details => redirectToMatchingRule(details);
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "XSwitch",
"description": "XSwitch tools for proxy web request url, support reg",
"short_name": "xs",
"version": "1.6.0",
"version": "1.6.1",
"manifest_version": 2,
"browser_action": {
"default_icon": "images/grey_128.png",
Expand Down
12 changes: 6 additions & 6 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ describe('CORS without Access-Control-Allow-Origin', () => {
value: 'true'
},
{
name: 'Access-Control-Allow-Headers',
value: 'x-requested-with,Content-Type'
name: 'Access-Control-Allow-Methods',
value: '*'
}
];
expect(
Expand Down Expand Up @@ -660,8 +660,8 @@ describe('CORS withCredentials', () => {
value: 'true'
},
{
name: 'Access-Control-Allow-Headers',
value: 'x-requested-with,Content-Type'
name: 'Access-Control-Allow-Methods',
value: '*'
}
];
expect(
Expand All @@ -683,8 +683,8 @@ describe('CORS withCredentials and no proxyConfig', () => {
value: 'true'
},
{
name: 'Access-Control-Allow-Headers',
value: 'x-requested-with,Content-Type'
name: 'Access-Control-Allow-Methods',
value: '*'
}
]);
});
Expand Down

0 comments on commit f2dae21

Please sign in to comment.