From 7ae49c361126986f9bab0130a6e4c3cbf3b8f02b Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 3 Feb 2022 12:15:41 +0100 Subject: [PATCH] fix: savePage throw on relative paths --- docs/api/web-contents.md | 2 +- shell/browser/api/electron_api_web_contents.cc | 5 +++++ spec-main/api-browser-window-spec.ts | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index ac44742ac59c1..c5500d0acb8f9 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1856,7 +1856,7 @@ the cursor when dragging. #### `contents.savePage(fullPath, saveType)` -* `fullPath` string - The full file path. +* `fullPath` string - The absolute file path. * `saveType` string - Specify the save type. * `HTMLOnly` - Save only the HTML of the page. * `HTMLComplete` - Save complete-html page. diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index dd53ce0dba80e..c3c0f8a9d47ed 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -2353,6 +2353,11 @@ v8::Local WebContents::SavePage( gin_helper::Promise promise(isolate); v8::Local handle = promise.GetHandle(); + if (!full_file_path.IsAbsolute()) { + promise.RejectWithErrorMessage("Path must be absolute"); + return handle; + } + auto* handler = new SavePageHandler(web_contents(), std::move(promise)); handler->Handle(full_file_path, save_type); diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index c181cb4f29b44..5f3657001ab8c 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -3431,6 +3431,23 @@ describe('BrowserWindow module', () => { } catch {} }); + it('should throw when passing relative paths', async () => { + const w = new BrowserWindow({ show: false }); + await w.loadFile(path.join(fixtures, 'pages', 'save_page', 'index.html')); + + await expect( + w.webContents.savePage('save_page.html', 'HTMLComplete') + ).to.eventually.be.rejectedWith('Path must be absolute'); + + await expect( + w.webContents.savePage('save_page.html', 'HTMLOnly') + ).to.eventually.be.rejectedWith('Path must be absolute'); + + await expect( + w.webContents.savePage('save_page.html', 'MHTML') + ).to.eventually.be.rejectedWith('Path must be absolute'); + }); + it('should save page to disk with HTMLOnly', async () => { const w = new BrowserWindow({ show: false }); await w.loadFile(path.join(fixtures, 'pages', 'save_page', 'index.html'));