Skip to content

Commit 0414ca3

Browse files
committed
fix(xhr-request): Make sure to also allow strings as url
Our utility to add `X-Requested-With` headers on API calls intercepts calls to `window.fetch`, so we must ensure we allow all parameters that the default browser provided `window.fetch` allows. In this case make sure to allow all stringify-able objects. Signed-off-by: Ferdinand Thiessen <[email protected]> Signed-off-by: nextcloud-command <[email protected]>
1 parent fbf8a46 commit 0414ca3

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

core/src/utils/xhr-request.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
import { getRootUrl } from '@nextcloud/router'
2323

24+
/**
25+
* @param {string} url The URL to check
26+
* @return {boolean} true if the URL points to this nextcloud instance
27+
*/
2428
const isNextcloudUrl = (url) => {
2529
const nextcloudBaseUrl = window.location.protocol + '//' + window.location.host + getRootUrl()
2630
// try with relative and absolute URL
@@ -43,24 +47,25 @@ export const interceptRequests = () => {
4347
})(XMLHttpRequest.prototype.open)
4448

4549
window.fetch = (function(fetch) {
46-
return (input, init) => {
47-
if (!isNextcloudUrl(input.url)) {
48-
return fetch(input, init)
50+
return (resource, options) => {
51+
// fetch allows the `input` to be either a Request object or any stringifyable value
52+
if (!isNextcloudUrl(resource.url ?? resource.toString())) {
53+
return fetch(resource, options)
4954
}
50-
if (!init) {
51-
init = {}
55+
if (!options) {
56+
options = {}
5257
}
53-
if (!init.headers) {
54-
init.headers = new Headers()
58+
if (!options.headers) {
59+
options.headers = new Headers()
5560
}
5661

57-
if (init.headers instanceof Headers && !init.headers.has('X-Requested-With')) {
58-
init.headers.append('X-Requested-With', 'XMLHttpRequest')
59-
} else if (init.headers instanceof Object && !init.headers['X-Requested-With']) {
60-
init.headers['X-Requested-With'] = 'XMLHttpRequest'
62+
if (options.headers instanceof Headers && !options.headers.has('X-Requested-With')) {
63+
options.headers.append('X-Requested-With', 'XMLHttpRequest')
64+
} else if (options.headers instanceof Object && !options.headers['X-Requested-With']) {
65+
options.headers['X-Requested-With'] = 'XMLHttpRequest'
6166
}
6267

63-
return fetch(input, init)
68+
return fetch(resource, options)
6469
}
6570
})(window.fetch)
6671
}

dist/core-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)