fix: auto-add --no-sandbox when running Chromium under root (#3064)

References #2745
This commit is contained in:
Andrey Lushnikov 2020-07-21 13:21:42 -07:00 committed by GitHub
parent 2120a2361b
commit af20d2704f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 4 deletions

View file

@ -200,7 +200,7 @@ export abstract class BrowserTypeBase implements BrowserType {
let browserServer: BrowserServer | undefined = undefined;
const { launchedProcess, gracefullyClose, kill } = await launchProcess({
executablePath: executable,
args: browserArguments,
args: this._amendArguments(browserArguments),
env: this._amendEnvironment(env, userDataDir, executable, browserArguments),
handleSIGINT,
handleSIGTERM,
@ -239,6 +239,7 @@ export abstract class BrowserTypeBase implements BrowserType {
abstract _connectToTransport(transport: ConnectionTransport, options: BrowserOptions): Promise<BrowserBase>;
abstract _startWebSocketServer(transport: ConnectionTransport, logger: Logger, port: number): WebSocketServer;
abstract _amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env;
abstract _amendArguments(browserArguments: string[]): string[];
abstract _attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void;
}

View file

@ -16,7 +16,8 @@
*/
import * as path from 'path';
import { assert, getFromENV, logPolitely, helper } from '../helper';
import * as os from 'os';
import { getFromENV, logPolitely, helper } from '../helper';
import { CRBrowser } from '../chromium/crBrowser';
import * as ws from 'ws';
import { Env } from './processLauncher';
@ -63,11 +64,24 @@ export class Chromium extends BrowserTypeBase {
}
_amendEnvironment(env: Env, userDataDir: string, executable: string, browserArguments: string[]): Env {
const runningAsRoot = process.geteuid && process.geteuid() === 0;
assert(!runningAsRoot || browserArguments.includes('--no-sandbox'), 'Cannot launch Chromium as root without --no-sandbox. See https://crbug.com/638180.');
return env;
}
_amendArguments(browserArguments: string[]): string[] {
// We currently only support Linux.
if (os.platform() !== 'linux')
return browserArguments;
// If there's already --no-sandbox passed in, do nothing.
if (browserArguments.indexOf('--no-sandbox') !== -1)
return browserArguments;
const runningAsRoot = process.geteuid && process.geteuid() === 0;
if (runningAsRoot) {
console.warn('WARNING: Playwright is being run under "root" user - disabling Chromium sandbox! Run under regular user to get rid of this warning.');
return ['--no-sandbox', ...browserArguments];
}
return browserArguments;
}
_attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void {
const message: ProtocolRequest = { method: 'Browser.close', id: kBrowserCloseMessageId, params: {} };
transport.send(message);

View file

@ -47,6 +47,10 @@ export class Firefox extends BrowserTypeBase {
} : env;
}
_amendArguments(browserArguments: string[]): string[] {
return browserArguments;
}
_attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void {
const message = { method: 'Browser.close', params: {}, id: kBrowserCloseMessageId };
transport.send(message);

View file

@ -42,6 +42,10 @@ export class WebKit extends BrowserTypeBase {
return { ...env, CURL_COOKIE_JAR_PATH: path.join(userDataDir, 'cookiejar.db') };
}
_amendArguments(browserArguments: string[]): string[] {
return browserArguments;
}
_attemptToGracefullyCloseBrowser(transport: ConnectionTransport): void {
transport.send({method: 'Playwright.close', params: {}, id: kBrowserCloseMessageId});
}