Skip to content

Commit

Permalink
fix: savePage throw on relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Feb 3, 2022
1 parent e0f2511 commit ddb0182
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/api/web-contents.md
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions shell/browser/api/save_page_handler.cc
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -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'));
Expand Down

0 comments on commit ddb0182

Please sign in to comment.