Skip to content

Commit

Permalink
Merge pull request #40471 from nextcloud/enh/fix-x-requested-with
Browse files Browse the repository at this point in the history
Only add x-requested-with header in requests to Nextcloud
  • Loading branch information
julien-nc authored Oct 16, 2023
2 parents 8212fee + b58c3dd commit 9bf824e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
13 changes: 12 additions & 1 deletion core/src/utils/xhr-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { getRootUrl } from '@nextcloud/router'

const isNextcloudUrl = (url) => {
const nextcloudBaseUrl = window.location.protocol + '//' + window.location.host + getRootUrl()
// try with relative and absolute URL
return url.startsWith(nextcloudBaseUrl) || url.startsWith(getRootUrl())
}

/**
* Intercept XMLHttpRequest and fetch API calls to add X-Requested-With header
*
Expand All @@ -28,14 +36,17 @@ export const interceptRequests = () => {
XMLHttpRequest.prototype.open = (function(open) {
return function(method, url, async) {
open.apply(this, arguments)
if (!this.getResponseHeader('X-Requested-With')) {
if (isNextcloudUrl(url) && !this.getResponseHeader('X-Requested-With')) {
this.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
}
}
})(XMLHttpRequest.prototype.open)

window.fetch = (function(fetch) {
return (input, init) => {
if (!isNextcloudUrl(input.url)) {
return fetch(input, init)
}
if (!init) {
init = {}
}
Expand Down
4 changes: 2 additions & 2 deletions dist/core-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/core-main.js.map

Large diffs are not rendered by default.

0 comments on commit 9bf824e

Please sign in to comment.