Handle cancelled Google popups and restored import pages
Address the non-blocking Fable review findings: clear restored file inputs, distinguish popup cancellation, and permit the app module under a script-denying base policy. Chromium and WebKit chooser checks pass.
Co-Authored-By: Codex <noreply@openai.com>
4 files changed,
+15
−4
googleimport/google-import.browser.mjs+5 −0googleimport/google-import.mjs+2 −2googleimport/googleimport_test.go+4 −0googleimport/policy.go+4 −2
CI passed — run details
diff --git a/googleimport/google-import.browser.mjs b/googleimport/google-import.browser.mjs| index f68a70d..85bd7a1 100644 |
| --- a/googleimport/google-import.browser.mjs |
| +++ b/googleimport/google-import.browser.mjs |
| @@ -9,6 +9,7 @@ const origin = process.argv[2] |
| const gis = `window.google={accounts:{oauth2:{ |
| initTokenClient(c){return {requestAccessToken(){window.requestCount=(window.requestCount||0)+1; |
| if(new URL(location).searchParams.has('popup')){c.error_callback({type:'popup_failed_to_open'});return} |
| + if(new URL(location).searchParams.has('popup-closed')){c.error_callback({type:'popup_closed'});return} |
| setTimeout(()=>c.callback(new URL(location).searchParams.has('denied')?{error:'access_denied'}:{access_token:'test-google-bearer',scope:'https://www.googleapis.com/auth/drive.file'}),new URL(location).searchParams.has('slow-auth')?150:10)} }}, |
| hasGrantedAllScopes(r,s){return r.scope===s} |
| }}};` |
| @@ -50,6 +51,10 @@ for (const engine of (process.env.ESTILO_BROWSERS || 'chromium,webkit').split(', |
| await select(); await page.waitForURL(origin+'/upload'); assert.equal(await page.textContent('body'),'Imported') |
| assert.equal(calls,1) |
| await start('/?popup=1');await choose.click();assert.match(await status.textContent(),/popup/);assert(await choose.isEnabled()) |
| + await start('/?popup-closed=1');await choose.click();assert.match(await status.textContent(),/cancelled/);assert(await choose.isEnabled()) |
| + await page.locator('#file').setInputFiles({name:'previous.docx',mimeType:'application/octet-stream',buffer:Buffer.from('previous export')}) |
| + await page.evaluate(()=>dispatchEvent(new PageTransitionEvent('pageshow',{persisted:true}))) |
| + assert.equal(await page.locator('#file').evaluate(input=>input.files.length),0) |
| await start('/?denied=1');await choose.click();await page.waitForFunction(()=>document.querySelector('[data-google-status]').textContent.includes('did not allow'));assert(await choose.isEnabled()) |
| await start('/?shortcut=1');await select();await page.waitForFunction(()=>document.querySelector('[data-google-status]').textContent.includes('could not be exported'));assert.equal(calls,1) |
| exportMode='error';await start('/');await select();await page.waitForFunction(()=>document.querySelector('[data-google-status]').textContent.includes('did not allow'));assert(await choose.isEnabled()) |
diff --git a/googleimport/google-import.mjs b/googleimport/google-import.mjs| index 00cb345..e81d7ff 100644 |
| --- a/googleimport/google-import.mjs |
| +++ b/googleimport/google-import.mjs |
| @@ -123,7 +123,7 @@ export function mount(root) { |
| try { |
| const client = google.accounts.oauth2.initTokenClient({ client_id: data.googleClient, scope: SCOPE, |
| include_granted_scopes: false, |
| - error_callback: () => { if (attempt === generation) reset('failed') }, |
| + error_callback: error => { if (attempt === generation) reset(error.type === 'popup_closed' ? 'cancelled' : 'failed') }, |
| callback: response => { |
| if (attempt !== generation) return |
| if (response.error || !response.access_token || !google.accounts.oauth2.hasGrantedAllScopes(response, SCOPE)) { reset('denied'); return } |
| @@ -168,7 +168,7 @@ export function mount(root) { |
| cancel.addEventListener('click', () => { reset('cancelled'); choose.focus() }) |
| form.addEventListener('submit', () => { reset(); choose.disabled = true }) |
| addEventListener('pagehide', () => reset()) |
| - addEventListener('pageshow', event => { if (event.persisted) reset('ready') }) |
| + addEventListener('pageshow', event => { if (event.persisted) { input.value = ''; reset('ready') } }) |
| if (document.prerendering) document.addEventListener('prerenderingchange', initialize, { once: true }) |
| else initialize() |
| } |
diff --git a/googleimport/googleimport_test.go b/googleimport/googleimport_test.go| index 820dea4..a8f0794 100644 |
| --- a/googleimport/googleimport_test.go |
| +++ b/googleimport/googleimport_test.go |
| @@ -44,6 +44,10 @@ func TestPolicyOnlyExpandsGoogleSources(t *testing.T) { |
| if strings.Contains(policy, "unsafe-eval") || strings.Contains(policy, "*") { |
| t.Fatal("unbounded policy") |
| } |
| + strict, err := Policy("default-src 'none'; script-src-elem 'none'") |
| + if err != nil || !strings.Contains(strict, "script-src 'self' https://accounts.google.com") || !strings.Contains(strict, "script-src-elem 'self' https://accounts.google.com") { |
| + t.Fatal("chooser's own module must be allowed", strict, err) |
| + } |
| p, err := Policy(baseline + "; child-src 'none'; script-src-elem 'self'") |
| if err != nil || !strings.Contains(p, "frame-src https://accounts.google.com https://docs.google.com") || !strings.Contains(p, "script-src-elem 'self' https://accounts.google.com") { |
| t.Fatal(p, err) |
diff --git a/googleimport/policy.go b/googleimport/policy.go| index 127833a..b63dabe 100644 |
| --- a/googleimport/policy.go |
| +++ b/googleimport/policy.go |
| @@ -50,9 +50,11 @@ func Policy(base string) (string, error) { |
| } |
| directives[name] = out |
| } |
| - add("script-src", "https://accounts.google.com", "https://apis.google.com") |
| + // The chooser module is served by the app itself, even if its original |
| + // policy disallowed scripts entirely. |
| + add("script-src", "'self'", "https://accounts.google.com", "https://apis.google.com") |
| if _, exists := directives["script-src-elem"]; exists { |
| - add("script-src-elem", "https://accounts.google.com", "https://apis.google.com") |
| + add("script-src-elem", "'self'", "https://accounts.google.com", "https://apis.google.com") |
| } |
| add("connect-src", "https://accounts.google.com", "https://apis.google.com", "https://www.googleapis.com") |
| add("frame-src", "https://accounts.google.com", "https://docs.google.com") |