Skip to content

Commit

Permalink
fix(wx): remove bogus Origin from API requests
Browse files Browse the repository at this point in the history
For some reason `js-ipfs-api` sends requests with "Origin: null"
under Chrom which produces '403 - Forbidden' error.
This workaround removes bogus header from API requests

Part of #218 effort
  • Loading branch information
lidel committed May 24, 2017
1 parent 6432fc8 commit 6fa4be1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 22 additions & 0 deletions add-on/src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function updatePeerCountState (count) {
}

function registerListeners () {
browser.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {urls: ['<all_urls>']}, ['blocking', 'requestHeaders'])
browser.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ['<all_urls>']}, ['blocking'])
browser.storage.onChanged.addListener(onStorageChange)
browser.tabs.onUpdated.addListener(onUpdatedTab)
Expand Down Expand Up @@ -94,6 +95,27 @@ function redirectToNormalizedPath (request) {
return { redirectUrl: url.toString() }
}

// HTTP Request Hooks
// ===================================================================

function onBeforeSendHeaders (request) {
if (request.url.startsWith(state.apiURLString)) {
// For some reason js-ipfs-api sent requests with "Origin: null" under Chrome
// which produced '403 - Forbidden' error.
// This workaround removes bogus header from API requests
for (var i = 0; i < request.requestHeaders.length; i++) {
let header = request.requestHeaders[i]
if (header.name === 'Origin' && (header.value == null || header.value === 'null')) {
request.requestHeaders.splice(i, 1)
break
}
}
}
return {
requestHeaders: request.requestHeaders
}
}

function onBeforeRequest (request) {
if (request.url.startsWith('https://ipfs.io/web%2B')) {
// fix path passed via custom protocol
Expand Down
1 change: 0 additions & 1 deletion test/unit/00-init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,4 @@ describe('init.js', function () {
.catch(error => { done(error) })
})
})

})

0 comments on commit 6fa4be1

Please sign in to comment.