From db428004f4203d0c677864baf12e94d94711fb07 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Thu, 8 Mar 2018 18:00:35 +0100 Subject: [PATCH] [CRX] Disable fetch in Chrome 60- Chrome 60 and earlier does not include credentials (cookies) in requests made with fetch, regardless of extension permissions. This was fixed in 61.0.3138.0 by https://chromium.googlesource.com/chromium/src/+/2e231cf052ca5e68e22baf0008ac9e5e29121707 This patch disables the fetch backend in all affected Chrome versions. The browser detection is done by checking for a change that coincides with the release of Chrome 61. Test case: 1. Copy the `isChromeWithFetchCredentials` function from the patch. 2. Run it in the JS console of Chrome and verify the return value. Verified results: - 49.0.2623.75 - false (earliest supported version by us) - 60.0.3112.90 - false (last major version affected by bug) - 61.0.3163.100 - true (first major version without bug) - 65.0.3325.146 - true (current stable) Test case 2: 1. Build the extension (`gulp chromium`) and load it in Chrome. 2. Open the developer tools, and open any PDF file. 3. In the "Network tab" of the developer tools, look at "request type". In Chrome 60-: Should be "xhr" In Chrome 61+: Should be "fetch" --- src/pdf.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/pdf.js b/src/pdf.js index ea3a3cd83..1de68d330 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -52,8 +52,21 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) { } else if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('CHROME')) { let PDFNetworkStream = require('./display/network.js').PDFNetworkStream; let PDFFetchStream; + let isChromeWithFetchCredentials = function() { + // fetch does not include credentials until Chrome 61.0.3138.0 and later. + // https://chromium.googlesource.com/chromium/src/+/2e231cf052ca5e68e22baf0008ac9e5e29121707 + try { + // Indexed properties on window are read-only in Chrome 61.0.3151.0+ + // https://chromium.googlesource.com/chromium/src.git/+/58ab4a971b06dec13e4edf9de8382ca6847f6190 + window[999] = 123; // should throw. Note: JS strict mode MUST be enabled. + delete window[999]; + return false; + } catch (e) { + return true; + } + }; if (typeof Response !== 'undefined' && 'body' in Response.prototype && - typeof ReadableStream !== 'undefined') { + typeof ReadableStream !== 'undefined' && isChromeWithFetchCredentials()) { PDFFetchStream = require('./display/fetch_stream.js').PDFFetchStream; } pdfjsDisplayAPI.setPDFNetworkStreamFactory((params) => {