Skip to content

Commit

Permalink
Ensure requests use 'cors' mode on browsers (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni authored Jul 15, 2024
1 parent b5d6b9d commit 3bfda51
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/contentFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ export class ContentFetcher {
}

return fetch(dynamic ? this.dynamicEndpoint : this.staticEndpoint, {
// Set the request mode to 'cors' when running in the browser.
// By default, the request mode is computed based on the referrer policy
// and response-tainting rules applied to the script that ultimately
// initiated the fetch, make this prone to errors due to unrelated
// configurations on the page.
// https://fetch.spec.whatwg.org/#origin-header
mode: typeof window === 'undefined' ? undefined : 'cors',
credentials: 'omit',
...options.extra,
method: 'POST',
Expand Down
7 changes: 7 additions & 0 deletions src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ export class Evaluator {
}

return fetch(this.endpoint, {
// Set the request mode to 'cors' when running in the browser.
// By default, the request mode is computed based on the referrer policy
// and response-tainting rules applied to the script that ultimately
// initiated the fetch, make this prone to errors due to unrelated
// configurations on the page.
// https://fetch.spec.whatwg.org/#origin-header
mode: typeof window === 'undefined' ? undefined : 'cors',
credentials: 'omit',
...options.extra,
method: 'POST',
Expand Down
1 change: 1 addition & 0 deletions test/contentFetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('A content fetcher', () => {
};

const requestMatcher: MockOptions = {
functionMatcher: (_, req) => req.mode === 'cors',
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
1 change: 1 addition & 0 deletions test/evaluator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('An evaluator', () => {

const query = 'user\'s name';
const requestMatcher: MockOptions = {
functionMatcher: (_, req) => req.mode === 'cors',
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down

0 comments on commit 3bfda51

Please sign in to comment.