Skip to content

Commit 6abcf80

Browse files
authored
catch errors attempting to access document.cookie (#15788)
behave as if cookie is empty accessing `document.cookie` can raise SecurityError if page is served by `Content-Security-Policy: sandbox`
1 parent 21919ee commit 6abcf80

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/services/src/contents/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,13 @@ export class Drive implements Contents.IDrive {
11691169
getDownloadUrl(localPath: string): Promise<string> {
11701170
const baseUrl = this.serverSettings.baseUrl;
11711171
let url = URLExt.join(baseUrl, FILES_URL, URLExt.encodeParts(localPath));
1172-
const xsrfTokenMatch = document.cookie.match('\\b_xsrf=([^;]*)\\b');
1172+
let cookie = '';
1173+
try {
1174+
cookie = document.cookie;
1175+
} catch (e) {
1176+
// e.g. SecurityError in case of CSP Sandbox
1177+
}
1178+
const xsrfTokenMatch = cookie.match('\\b_xsrf=([^;]*)\\b');
11731179
if (xsrfTokenMatch) {
11741180
const fullUrl = new URL(url);
11751181
fullUrl.searchParams.append('_xsrf', xsrfTokenMatch[1]);

packages/services/src/serverconnection.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ namespace Private {
306306
authenticated = true;
307307
request.headers.append('Authorization', `token ${settings.token}`);
308308
}
309-
if (typeof document !== 'undefined' && document?.cookie) {
309+
if (typeof document !== 'undefined') {
310310
const xsrfToken = getCookie('_xsrf');
311311
if (xsrfToken !== undefined) {
312312
authenticated = true;
@@ -334,7 +334,14 @@ namespace Private {
334334
*/
335335
function getCookie(name: string): string | undefined {
336336
// From http://www.tornadoweb.org/en/stable/guide/security.html
337-
const matches = document.cookie.match('\\b' + name + '=([^;]*)\\b');
337+
let cookie = '';
338+
try {
339+
cookie = document.cookie;
340+
} catch (e) {
341+
// e.g. SecurityError in case of CSP Sandbox
342+
return;
343+
}
344+
const matches = cookie.match('\\b' + name + '=([^;]*)\\b');
338345
return matches?.[1];
339346
}
340347
}

0 commit comments

Comments
 (0)