Skip to content

Commit b435ce7

Browse files
authored
fix: [#1048] Always include manually set authorization headers in requests (#1957)
1 parent f68210e commit b435ce7

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

packages/happy-dom/src/fetch/Fetch.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,6 @@ export default class Fetch {
974974
(this.request.credentials === 'same-origin' &&
975975
FetchCORSUtility.isCORS(this.#window.location.href, locationURL))
976976
) {
977-
headers.delete('authorization');
978-
headers.delete('www-authenticate');
979977
headers.delete('cookie');
980978
headers.delete('cookie2');
981979
}

packages/happy-dom/src/fetch/SyncFetch.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,6 @@ export default class SyncFetch {
681681
(this.request.credentials === 'same-origin' &&
682682
FetchCORSUtility.isCORS(this.#window.location.href, locationURL))
683683
) {
684-
headers.delete('authorization');
685-
headers.delete('www-authenticate');
686684
headers.delete('cookie');
687685
headers.delete('cookie2');
688686
}

packages/happy-dom/src/fetch/utilities/FetchRequestHeaderUtility.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ export default class FetchRequestHeaderUtility {
8686
const originURL = new URL(options.window.location.href);
8787
const isCORS = FetchCORSUtility.isCORS(originURL, options.request[PropertySymbol.url]);
8888

89-
// TODO: Maybe we need to add support for OPTIONS request with 'Access-Control-Allow-*' headers?
90-
if (
91-
options.request.credentials === 'omit' ||
92-
(options.request.credentials === 'same-origin' && isCORS)
93-
) {
94-
headers.delete('authorization');
95-
headers.delete('www-authenticate');
96-
}
97-
9889
headers.set('Accept-Encoding', 'gzip, deflate, br');
9990
headers.set('Connection', 'close');
10091

@@ -117,6 +108,9 @@ export default class FetchRequestHeaderUtility {
117108
if (cookies.length > 0) {
118109
headers.set('Cookie', CookieStringUtility.cookiesToString(cookies));
119110
}
111+
} else {
112+
headers.delete('Cookie');
113+
headers.delete('Cookie2');
120114
}
121115

122116
if (!headers.has('Accept')) {

packages/happy-dom/test/fetch/Fetch.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ describe('Fetch', () => {
993993
]);
994994
});
995995

996-
it(`Doesn't forward the headers "cookie", "authorization" or "www-authenticate" if request credentials are set to "omit".`, async () => {
996+
it(`Doesn't forward "cookie" headers if request credentials are set to "omit".`, async () => {
997997
const window = new Window({ url: 'https://localhost:8080/' });
998998
const url = 'https://localhost:8080/some/path';
999999

@@ -1019,7 +1019,9 @@ describe('Fetch', () => {
10191019
Connection: 'close',
10201020
Referer: 'https://localhost:8080/',
10211021
'User-Agent': window.navigator.userAgent,
1022-
'Accept-Encoding': 'gzip, deflate, br'
1022+
'Accept-Encoding': 'gzip, deflate, br',
1023+
authorization: 'authorization',
1024+
'www-authenticate': 'www-authenticate'
10231025
},
10241026
agent: false,
10251027
rejectUnauthorized: true,
@@ -1030,7 +1032,7 @@ describe('Fetch', () => {
10301032
]);
10311033
});
10321034

1033-
it('Does not forward the headers "cookie", "authorization" or "www-authenticate" if request credentials are set to "same-origin" and the request goes do a different origin than the document.', async () => {
1035+
it('Does not forward "cookie" headers if request credentials are set to "same-origin" and the request goes do a different origin than the document.', async () => {
10341036
const originURL = 'https://localhost:8080';
10351037
const window = new Window({ url: originURL });
10361038
const url = 'https://other.origin.com/some/path';
@@ -1083,7 +1085,9 @@ describe('Fetch', () => {
10831085
'User-Agent': window.navigator.userAgent,
10841086
'Accept-Encoding': 'gzip, deflate, br',
10851087
Origin: originURL,
1086-
Referer: originURL + '/'
1088+
Referer: originURL + '/',
1089+
authorization: 'authorization',
1090+
'www-authenticate': 'www-authenticate'
10871091
},
10881092
agent: false,
10891093
rejectUnauthorized: true,

packages/happy-dom/test/fetch/SyncFetch.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ describe('SyncFetch', () => {
15161516
);
15171517
});
15181518

1519-
it('Does\'nt forward the headers "cookie", "authorization" or "www-authenticate" if request credentials are set to "omit".', () => {
1519+
it('Does\'nt forward "cookie" headers if request credentials are set to "omit".', () => {
15201520
browserFrame.url = 'https://localhost:8080/';
15211521

15221522
const url = 'https://localhost:8080/some/path';
@@ -1561,14 +1561,16 @@ describe('SyncFetch', () => {
15611561
Connection: 'close',
15621562
Referer: 'https://localhost:8080/',
15631563
'User-Agent': window.navigator.userAgent,
1564-
'Accept-Encoding': 'gzip, deflate, br'
1564+
'Accept-Encoding': 'gzip, deflate, br',
1565+
authorization: 'authorization',
1566+
'www-authenticate': 'www-authenticate'
15651567
},
15661568
body: null
15671569
})
15681570
);
15691571
});
15701572

1571-
it('Does\'nt forward the headers "cookie", "authorization" or "www-authenticate" if request credentials are set to "same-origin" and the request goes do a different origin than the document.', () => {
1573+
it('Does\'nt forward "cookie" headers if request credentials are set to "same-origin" and the request goes do a different origin than the document.', () => {
15721574
const originURL = 'https://localhost:8080';
15731575

15741576
browserFrame.url = originURL;
@@ -1618,7 +1620,9 @@ describe('SyncFetch', () => {
16181620
'User-Agent': window.navigator.userAgent,
16191621
'Accept-Encoding': 'gzip, deflate, br',
16201622
Origin: originURL,
1621-
Referer: originURL + '/'
1623+
Referer: originURL + '/',
1624+
authorization: 'authorization',
1625+
'www-authenticate': 'www-authenticate'
16221626
},
16231627
body: null
16241628
})

0 commit comments

Comments
 (0)