fix: savePage throw on relative paths (#32728)
This commit is contained in:
		
					parent
					
						
							
								d46431b564
							
						
					
				
			
			
				commit
				
					
						81fcd732c2
					
				
			
		
					 3 changed files with 23 additions and 1 deletions
				
			
		| 
						 | 
					@ -1856,7 +1856,7 @@ the cursor when dragging.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### `contents.savePage(fullPath, saveType)`
 | 
					#### `contents.savePage(fullPath, saveType)`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* `fullPath` string - The full file path.
 | 
					* `fullPath` string - The absolute file path.
 | 
				
			||||||
* `saveType` string - Specify the save type.
 | 
					* `saveType` string - Specify the save type.
 | 
				
			||||||
  * `HTMLOnly` - Save only the HTML of the page.
 | 
					  * `HTMLOnly` - Save only the HTML of the page.
 | 
				
			||||||
  * `HTMLComplete` - Save complete-html page.
 | 
					  * `HTMLComplete` - Save complete-html page.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2353,6 +2353,11 @@ v8::Local<v8::Promise> WebContents::SavePage(
 | 
				
			||||||
  gin_helper::Promise<void> promise(isolate);
 | 
					  gin_helper::Promise<void> promise(isolate);
 | 
				
			||||||
  v8::Local<v8::Promise> handle = promise.GetHandle();
 | 
					  v8::Local<v8::Promise> 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));
 | 
					  auto* handler = new SavePageHandler(web_contents(), std::move(promise));
 | 
				
			||||||
  handler->Handle(full_file_path, save_type);
 | 
					  handler->Handle(full_file_path, save_type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3431,6 +3431,23 @@ describe('BrowserWindow module', () => {
 | 
				
			||||||
      } catch {}
 | 
					      } 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 () => {
 | 
					    it('should save page to disk with HTMLOnly', async () => {
 | 
				
			||||||
      const w = new BrowserWindow({ show: false });
 | 
					      const w = new BrowserWindow({ show: false });
 | 
				
			||||||
      await w.loadFile(path.join(fixtures, 'pages', 'save_page', 'index.html'));
 | 
					      await w.loadFile(path.join(fixtures, 'pages', 'save_page', 'index.html'));
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue