fix: add --no-sandbox flag to browser launch options (#6686)

This commit is contained in:
Alfredo Medrano 2025-08-11 21:25:58 -06:00 • committed by GitHub
parent e02ac08089
commit b6dd6e5210
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 24 deletions

View file

@ -50,15 +50,20 @@ export class UrlContentFetcher {
return
}
const stats = await this.ensureChromiumExists()
const args = [
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
"--disable-dev-shm-usage",
"--disable-accelerated-2d-canvas",
"--no-first-run",
"--disable-gpu",
"--disable-features=VizDisplayCompositor",
]
if (process.platform === "linux") {
// Fixes network errors on Linux hosts (see https://github.com/puppeteer/puppeteer/issues/8246)
args.push("--no-sandbox")
}
this.browser = await stats.puppeteer.launch({
args: [
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
"--disable-dev-shm-usage",
"--disable-accelerated-2d-canvas",
"--no-first-run",
"--disable-gpu",
"--disable-features=VizDisplayCompositor",
],
args,
executablePath: stats.executablePath,
})
// (latest version of puppeteer does not add headless to user agent)

View file

@ -125,25 +125,75 @@ describe("UrlContentFetcher", () => {
})
describe("launchBrowser", () => {
it("should launch browser with correct arguments", async () => {
await urlContentFetcher.launchBrowser()
expect(vi.mocked(PCR)).toHaveBeenCalledWith({
downloadPath: path.join("/test/storage", "puppeteer"),
it("should launch browser with correct arguments on non-Linux platforms", async () => {
// Ensure we're not on Linux for this test
const originalPlatform = process.platform
Object.defineProperty(process, 'platform', {
value: 'darwin' // macOS
})
const stats = await vi.mocked(PCR).mock.results[0].value
expect(stats.puppeteer.launch).toHaveBeenCalledWith({
args: [
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
"--disable-dev-shm-usage",
"--disable-accelerated-2d-canvas",
"--no-first-run",
"--disable-gpu",
"--disable-features=VizDisplayCompositor",
],
executablePath: "/path/to/chromium",
try {
await urlContentFetcher.launchBrowser()
expect(vi.mocked(PCR)).toHaveBeenCalledWith({
downloadPath: path.join("/test/storage", "puppeteer"),
})
const stats = await vi.mocked(PCR).mock.results[0].value
expect(stats.puppeteer.launch).toHaveBeenCalledWith({
args: [
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
"--disable-dev-shm-usage",
"--disable-accelerated-2d-canvas",
"--no-first-run",
"--disable-gpu",
"--disable-features=VizDisplayCompositor",
],
executablePath: "/path/to/chromium",
})
} finally {
// Restore original platform
Object.defineProperty(process, 'platform', {
value: originalPlatform
})
}
})
it("should launch browser with Linux-specific arguments", async () => {
// Mock process.platform to be linux
const originalPlatform = process.platform
Object.defineProperty(process, 'platform', {
value: 'linux'
})
try {
// Create a new instance to ensure fresh state
const linuxFetcher = new UrlContentFetcher(mockContext)
await linuxFetcher.launchBrowser()
expect(vi.mocked(PCR)).toHaveBeenCalledWith({
downloadPath: path.join("/test/storage", "puppeteer"),
})
const stats = await vi.mocked(PCR).mock.results[vi.mocked(PCR).mock.results.length - 1].value
expect(stats.puppeteer.launch).toHaveBeenCalledWith({
args: [
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
"--disable-dev-shm-usage",
"--disable-accelerated-2d-canvas",
"--no-first-run",
"--disable-gpu",
"--disable-features=VizDisplayCompositor",
"--no-sandbox", // Linux-specific argument
],
executablePath: "/path/to/chromium",
})
} finally {
// Restore original platform
Object.defineProperty(process, 'platform', {
value: originalPlatform
})
}
})
it("should set viewport and headers after launching", async () => {