From ddb0182a45e509b81693f5b0a30b435606a6b2bf 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/save_page_handler.cc | 6 ++++++ spec-main/api-browser-window-spec.ts | 17 +++++++++++++++++ 3 files changed, 24 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/save_page_handler.cc b/shell/browser/api/save_page_handler.cc index 5d45341192773..69a1db7bcf927 100644 --- a/shell/browser/api/save_page_handler.cc +++ b/shell/browser/api/save_page_handler.cc @@ -30,6 +30,12 @@ void SavePageHandler::OnDownloadCreated(content::DownloadManager* manager, bool SavePageHandler::Handle(const base::FilePath& full_path, const content::SavePageType& save_type) { + if (!full_path.IsAbsolute()) { + promise_.RejectWithErrorMessage("Path must be absolute"); + delete this; + return false; + } + auto* download_manager = web_contents_->GetBrowserContext()->GetDownloadManager(); download_manager->AddObserver(this); diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index b1613fea6dce2..0e24a6aa07cb5 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -3432,6 +3432,23 @@ describe('BrowserWindow module', () => { }); afterEach(closeAllWindows); + 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', async () => { const w = new BrowserWindow({ show: false }); await w.loadFile(path.join(fixtures, 'pages', 'save_page', 'index.html'));