Skip to content

Commit

Permalink
fix(browser): omit file path validation in uploadFile() in browser en…
Browse files Browse the repository at this point in the history
…vironments (#13258)
  • Loading branch information
OrKoN authored Nov 4, 2024
1 parent 4e5c0ad commit a9e6cd1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
16 changes: 9 additions & 7 deletions packages/puppeteer-core/src/bidi/ElementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ export class BidiElementHandle<
): Promise<void> {
// Locate all files and confirm that they exist.
const path = environment.value.path;
files = files.map(file => {
if (path.win32.isAbsolute(file) || path.posix.isAbsolute(file)) {
return file;
} else {
return path.resolve(file);
}
});
if (path) {
files = files.map(file => {
if (path.win32.isAbsolute(file) || path.posix.isAbsolute(file)) {
return file;
} else {
return path.resolve(file);
}
});
}
await this.frame.setFiles(this, files);
}

Expand Down
23 changes: 14 additions & 9 deletions packages/puppeteer-core/src/cdp/ElementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,30 @@ export class CdpElementHandle<
@bindIsolatedHandle
override async uploadFile(
this: CdpElementHandle<HTMLInputElement>,
...filePaths: string[]
...files: string[]
): Promise<void> {
const isMultiple = await this.evaluate(element => {
return element.multiple;
});
assert(
filePaths.length <= 1 || isMultiple,
files.length <= 1 || isMultiple,
'Multiple file uploads only work with <input type=file multiple>',
);

// Locate all files and confirm that they exist.
const path = environment.value.path;
const files = filePaths.map(filePath => {
if (path.win32.isAbsolute(filePath) || path.posix.isAbsolute(filePath)) {
return filePath;
} else {
return path.resolve(filePath);
}
});
if (path) {
files = files.map(filePath => {
if (
path.win32.isAbsolute(filePath) ||
path.posix.isAbsolute(filePath)
) {
return filePath;
} else {
return path.resolve(filePath);
}
});
}

/**
* The zero-length array is a special case, it seems that
Expand Down
5 changes: 1 addition & 4 deletions packages/puppeteer-core/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const isNode = !!(typeof process !== 'undefined' && process.version);

export interface EnvironmentDependencies {
fs: typeof FS;
path: typeof Path;
path?: typeof Path;
ScreenRecorder: typeof ScreenRecorder;
}

Expand All @@ -31,9 +31,6 @@ export const environment: {
get fs(): typeof FS {
throw new Error('fs is not available in this environment');
},
get path(): typeof Path {
throw new Error('path is not available in this environment');
},
get ScreenRecorder(): typeof ScreenRecorder {
throw new Error('ScreenRecorder is not available in this environment');
},
Expand Down

0 comments on commit a9e6cd1

Please sign in to comment.