Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webContents.print parameter validation error #38721

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/browser/api/web-contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,9 @@ WebContents.prototype.printToPDF = async function (options) {

// TODO(codebytere): deduplicate argument sanitization by moving rest of
// print param logic into new file shared between printToPDF and print
WebContents.prototype.print = function (printOptions: ElectronInternal.WebContentsPrintOptions, callback) {
const options = printOptions ?? {};
if (options.pageSize) {
const pageSize = options.pageSize;
WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions, callback) {
if (typeof options === 'object') {
const pageSize = options.pageSize ?? 'A4';
if (typeof pageSize === 'object') {
if (!pageSize.height || !pageSize.width) {
throw new Error('height and width properties are required for pageSize');
Expand All @@ -357,10 +356,21 @@ WebContents.prototype.print = function (printOptions: ElectronInternal.WebConten
name: 'CUSTOM',
custom_display_name: 'Custom',
height_microns: height,
width_microns: width
width_microns: width,
imageable_area_left_microns: 0,
imageable_area_bottom_microns: 0,
imageable_area_right_microns: width,
imageable_area_top_microns: height
};
} else if (typeof pageSize === 'string' && PDFPageSizes[pageSize]) {
const mediaSize = PDFPageSizes[pageSize];
options.mediaSize = {
...mediaSize,
imageable_area_left_microns: 0,
imageable_area_bottom_microns: 0,
imageable_area_right_microns: mediaSize.width_microns,
imageable_area_top_microns: mediaSize.height_microns
};
} else if (PDFPageSizes[pageSize]) {
options.mediaSize = PDFPageSizes[pageSize];
} else {
throw new Error(`Unsupported pageSize: ${pageSize}`);
}
Expand Down
8 changes: 8 additions & 0 deletions spec/api-web-contents-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ describe('webContents module', () => {
}).to.throw('webContents.print(): Invalid print settings specified.');
});

it('throws when an invalid pageSize is passed', () => {
const badSize = 5;
expect(() => {
// @ts-ignore this line is intentionally incorrect
w.webContents.print({ pageSize: badSize });
}).to.throw(`Unsupported pageSize: ${badSize}`);
});

it('throws when an invalid callback is passed', () => {
expect(() => {
// @ts-ignore this line is intentionally incorrect
Expand Down
4 changes: 4 additions & 0 deletions typings/internal-electron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ declare namespace ElectronInternal {
custom_display_name: string,
height_microns: number,
width_microns: number,
imageable_area_left_microns?: number,
imageable_area_bottom_microns?: number,
imageable_area_right_microns?: number,
imageable_area_top_microns?: number,
is_default?: 'true',
}

Expand Down