Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(reporters): add secrets option to redact sensitive values (#43334)

Dmitry Gozmanadf0514792f6
diff --git a/docs/src/test-reporters-js.md b/docs/src/test-reporters-js.md
index 14af3d9ab0..e811dcc39c 100644
--- a/docs/src/test-reporters-js.md
+++ b/docs/src/test-reporters-js.md
@@ -79,6 +79,25 @@ export default defineConfig({
});
```
+### Redacting secrets
+
+Pass a `secrets` list to any reporter, built-in or custom, to hide sensitive values in its output. Each entry has a `value` with the sensitive text, and an optional `name`. The reporter sees `<secret>NAME</secret>`, or `<secret/>` for an entry without a name, in place of the value in test and step titles, errors, console output, attachment names and attachment bodies.
+
+```js title="playwright.config.ts"
+import { defineConfig } from '@playwright/test';
+
+export default defineConfig({
+ reporter: [
+ ['list'],
+ ['html', { secrets: [{ name: 'PASSWORD', value: process.env.PASSWORD }] }],
+ ],
+});
+```
+
+In this example, the terminal output is not changed, while the HTML report never contains the password.
+
+Values are matched as plain text, so a short value could match unrelated text. Screenshots, videos, trace files, and attachments referenced by path are not changed.
+
## Built-in reporters
All built-in reporters show detailed information about failures, and mostly differ in verbosity for successful runs.
diff --git a/packages/isomorphic/index.ts b/packages/isomorphic/index.ts
index 6b073e7f9b..bf6df6dc41 100644
--- a/packages/isomorphic/index.ts
+++ b/packages/isomorphic/index.ts
@@ -31,6 +31,7 @@ export * from './multimap';
export * from './protocolFormatter';
export * from './protocolMetainfo';
export * from './rtti';
+export * from './secrets';
export * from './semaphore';
export * from './stringUtils';
export * from './formatUtils';
diff --git a/packages/isomorphic/secrets.ts b/packages/isomorphic/secrets.ts
new file mode 100644
index 0000000000..3b5a8a9fb5
--- /dev/null
+++ b/packages/isomorphic/secrets.ts
@@ -0,0 +1,70 @@
+/**
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export type Secrets = { name?: string, value: string }[];
+
+// Longer values go first, so that a secret that contains another secret is replaced as a whole.
+export function normalizeSecrets(secrets: Secrets | undefined): Secrets {
+ if (!secrets)
+ return [];
+ return secrets
+ .filter(secret => typeof secret?.value === 'string' && secret.value.length > 0)
+ .sort((a, b) => b.value.length - a.value.length);
+}
+
+export function secretsKey(secrets: Secrets | undefined): string {
+ return JSON.stringify(normalizeSecrets(secrets).map(secret => JSON.stringify([secret.name ?? null, secret.value])).sort());
+}
+
+export function secretPlaceholder(name: string | undefined): string {
+ return name ? `<secret>${name}</secret>` : `<secret/>`;
+}
+
+export function redactSecrets(text: string, secrets: Secrets): string {
+ for (const { name, value } of secrets)
+ text = text.replaceAll(value, secretPlaceholder(name));
+ return text;
+}
+
+export function redactSecretsInJSON(json: string, secrets: Secrets): string {
+ for (const { name, value } of secrets) {
+ const escapedValue = JSON.stringify(value).slice(1, -1);
+ const escapedPlaceholder = JSON.stringify(secretPlaceholder(name)).slice(1, -1);
+ json = json.replaceAll(escapedValue, escapedPlaceholder);
+ }
+ return json;
+}
+
+// The buffer is not decoded, because it may not be a valid utf-8 string.
+export function redactSecretsInBuffer(buffer: Buffer, secrets: Secrets): Buffer {
+ for (const { name, value } of secrets) {
+ const needle = Buffer.from(value, 'utf8');
+ let index = buffer.indexOf(needle);
+ if (index === -1)
+ continue;
+ const replacement = Buffer.from(secretPlaceholder(name), 'utf8');
+ const chunks: Buffer[] = [];
+ let start = 0;
+ while (index !== -1) {
+ chunks.push(buffer.subarray(start, index), replacement);
+ start = index + needle.length;
+ index = buffer.indexOf(needle, start);
+ }
+ chunks.push(buffer.subarray(start));
+ buffer = Buffer.concat(chunks);
+ }
+ return buffer;
+}
diff --git a/packages/playwright/src/cli/reportActions.ts b/packages/playwright/src/cli/reportActions.ts
index 599de78429..5ade1d699f 100644
--- a/packages/playwright/src/cli/reportActions.ts
+++ b/packages/playwright/src/cli/reportActions.ts
@@ -40,7 +40,7 @@ export async function mergeReports(reportDir: string | undefined, opts: { [key:
throw new Error(`"${dir}" is not a directory`);
let reporterDescriptions: ReporterDescription[] | undefined = resolveReporterOption(opts.reporter);
if (!reporterDescriptions && configFile)
- reporterDescriptions = config.config.reporter;
+ reporterDescriptions = config.reporterDescriptions;
if (!reporterDescriptions)
reporterDescriptions = [[commonConfig.defaultReporter]];
const rootDirOverride = configFile ? config.config.rootDir : undefined;
diff --git a/packages/playwright/src/common/config.ts b/packages/playwright/src/common/config.ts
index c27be2079f..75b07ee2c3 100644
--- a/packages/playwright/src/common/config.ts
+++ b/packages/playwright/src/common/config.ts
@@ -49,6 +49,7 @@ export class FullConfigInternal {
readonly singleTSConfigPath?: string;
readonly captureGitInfo: Config['captureGitInfo'];
readonly retryStrategy: 'immediate' | 'isolated';
+ readonly reporterDescriptions: ReporterDescription[]; // Includes secrets, unlike FullConfig.reporter
defineConfigWasUsed = false;
globalSetups: string[] = [];
@@ -77,6 +78,8 @@ export class FullConfigInternal {
// so that plugins such as gitCommitInfoPlugin can populate metadata once.
userConfig.metadata = userConfig.metadata || {};
+ this.reporterDescriptions = [...takeFirst(configCLIOverrides.reporter, resolveReporters(userConfig.reporter, configDir), [[defaultReporter]]), ...(configCLIOverrides.additionalReporters ?? [])];
+
const globalTags = Array.isArray(userConfig.tag) ? userConfig.tag : (userConfig.tag ? [userConfig.tag] : []);
for (const tag of globalTags) {
if (tag[0] !== '@')
@@ -101,7 +104,7 @@ export class FullConfigInternal {
projects: [],
filteredProjects: [],
quiet: takeFirst(configCLIOverrides.quiet, userConfig.quiet, false),
- reporter: [...takeFirst(configCLIOverrides.reporter, resolveReporters(userConfig.reporter, configDir), [[defaultReporter]]), ...(configCLIOverrides.additionalReporters ?? [])],
+ reporter: this.reporterDescriptions.map(description => stripReporterSecrets(description)),
reportSlowTests: takeFirst(userConfig.reportSlowTests, { max: 5, threshold: 300_000 /* 5 minutes */ }),
shard: takeFirst(configCLIOverrides.shard, userConfig.shard, null),
tags: globalTags,
@@ -220,6 +223,15 @@ function pathResolve(baseDir: string, relative: string | undefined): string | un
return path.resolve(baseDir, relative);
}
+export function stripReporterSecrets(description: ReporterDescription): ReporterDescription {
+ const [name, options] = description;
+ if (!options || typeof options !== 'object' || !('secrets' in options))
+ return description;
+ const rest = { ...options };
+ delete rest.secrets;
+ return [name, rest];
+}
+
function resolveReporters(reporters: Config['reporter'], rootDir: string): ReporterDescription[] | undefined {
return toReporters(reporters as any)?.map(([id, arg]) => {
if (builtInReporters.includes(id as any))
diff --git a/packages/playwright/src/reporters/redactingReporter.ts b/packages/playwright/src/reporters/redactingReporter.ts
new file mode 100644
index 0000000000..fee40c73b5
--- /dev/null
+++ b/packages/playwright/src/reporters/redactingReporter.ts
@@ -0,0 +1,154 @@
+/**
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { normalizeSecrets, redactSecrets, redactSecretsInBuffer } from '@isomorphic/secrets';
+
+import type { ReportConfigureParams, ReportEndParams, ReporterPreprocessParams, ReporterV2 } from './reporterV2';
+import type { FullConfig, FullResult, Suite, TestCase, TestError, TestResult, TestStep, WorkerInfo } from '../../types/testReporter';
+import type { Secrets } from '@isomorphic/secrets';
+
+export class SecretsRedactor {
+ private _secrets: Secrets;
+ private _views = new Map<object, any>();
+ private _buffers = new Map<Buffer, Buffer>();
+ private _handler: ProxyHandler<any>;
+
+ constructor(secrets: Secrets) {
+ this._secrets = normalizeSecrets(secrets);
+ this._handler = {
+ get: (target, property, receiver) => {
+ const value = Reflect.get(target, property, receiver);
+ // Methods run against the view, so everything they touch and return is redacted too.
+ if (typeof value === 'function')
+ return (...args: any[]) => this.redact(Reflect.apply(value, receiver, args));
+ return this.redact(value);
+ },
+ };
+ }
+
+ redact<T>(value: T): T {
+ if (typeof value === 'string')
+ return redactSecrets(value, this._secrets) as T;
+ if (typeof value !== 'object' || value === null)
+ return value;
+ if (Buffer.isBuffer(value)) {
+ let redacted = this._buffers.get(value);
+ if (!redacted) {
+ redacted = redactSecretsInBuffer(value, this._secrets);
+ this._buffers.set(value, redacted);
+ }
+ return redacted as T;
+ }
+ if (isOpaque(value))
+ return value;
+ let view = this._views.get(value);
+ if (!view) {
+ view = new Proxy(value, this._handler);
+ this._views.set(value, view);
+ }
+ return view;
+ }
+}
+
+// Built-in objects whose methods do not work through a proxy. None of them hold secrets.
+// Iterators are created by methods that run against the view, so they already
+// produce redacted values.
+function isOpaque(value: object): boolean {
+ if (value instanceof Date || value instanceof RegExp || value instanceof Promise ||
+ value instanceof Map || value instanceof Set || value instanceof WeakMap || value instanceof WeakSet ||
+ value instanceof ArrayBuffer || ArrayBuffer.isView(value))
+ return true;
+ const tag = Object.prototype.toString.call(value);
+ return tag.endsWith(' Iterator]') || tag === '[object Generator]';
+}
+
+export class RedactingReporter implements ReporterV2 {
+ private _reporter: ReporterV2;
+ private _redactor: SecretsRedactor;
+
+ constructor(reporter: ReporterV2, redactor: SecretsRedactor) {
+ this._reporter = reporter;
+ this._redactor = redactor;
+ }
+
+ version(): 'v2' {
+ return 'v2';
+ }
+
+ onConfigure(config: FullConfig) {
+ this._reporter.onConfigure?.(this._redactor.redact(config));
+ }
+
+ async preprocess(params: ReporterPreprocessParams) {
+ await this._reporter.preprocess?.(this._redactor.redact(params));
+ }
+
+ onBegin(suite: Suite) {
+ this._reporter.onBegin?.(this._redactor.redact(suite));
+ }
+
+ onTestBegin(test: TestCase, result: TestResult) {
+ this._reporter.onTestBegin?.(this._redactor.redact(test), this._redactor.redact(result));
+ }
+
+ onStdOut(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
+ this._reporter.onStdOut?.(this._redactor.redact(chunk), this._redactor.redact(test), this._redactor.redact(result));
+ }
+
+ onStdErr(chunk: string | Buffer, test?: TestCase, result?: TestResult) {
+ this._reporter.onStdErr?.(this._redactor.redact(chunk), this._redactor.redact(test), this._redactor.redact(result));
+ }
+
+ async onTestPaused(test: TestCase, result: TestResult) {
+ await this._reporter.onTestPaused?.(this._redactor.redact(test), this._redactor.redact(result));
+ }
+
+ onTestEnd(test: TestCase, result: TestResult) {
+ this._reporter.onTestEnd?.(this._redactor.redact(test), this._redactor.redact(result));
+ }
+
+ onReportConfigure(params: ReportConfigureParams) {
+ this._reporter.onReportConfigure?.(this._redactor.redact(params));
+ }
+
+ onReportEnd(params: ReportEndParams) {
+ this._reporter.onReportEnd?.(this._redactor.redact(params));
+ }
+
+ async onEnd(result: FullResult) {
+ return await this._reporter.onEnd?.(this._redactor.redact(result));
+ }
+
+ async onExit() {
+ await this._reporter.onExit?.();
+ }
+
+ onError(error: TestError, workerInfo?: WorkerInfo) {
+ this._reporter.onError?.(this._redactor.redact(error), this._redactor.redact(workerInfo));
+ }
+
+ onStepBegin(test: TestCase, result: TestResult, step: TestStep) {
+ this._reporter.onStepBegin?.(this._redactor.redact(test), this._redactor.redact(result), this._redactor.redact(step));
+ }
+
+ onStepEnd(test: TestCase, result: TestResult, step: TestStep) {
+ this._reporter.onStepEnd?.(this._redactor.redact(test), this._redactor.redact(result), this._redactor.redact(step));
+ }
+
+ printsToStdio(): boolean {
+ return this._reporter.printsToStdio ? this._reporter.printsToStdio() : true;
+ }
+}
diff --git a/packages/playwright/src/runner/reporters.ts b/packages/playwright/src/runner/reporters.ts
index 7b9bef3c18..c53939a42c 100644
--- a/packages/playwright/src/runner/reporters.ts
+++ b/packages/playwright/src/runner/reporters.ts
@@ -14,9 +14,11 @@
* limitations under the License.
*/
import fs from 'fs';
+import { secretsKey } from '@isomorphic/secrets';
import { calculateSha1 } from '@utils/crypto';
import { loadReporter } from './loadUtils';
+import { config as commonConfig } from '../common';
import { formatError } from '../reporters/base';
import { BlobReporter } from '../reporters/blob';
import CoverageReporter from '../reporters/coverage';
@@ -31,13 +33,15 @@ import LineReporter from '../reporters/line';
import ListReporter from '../reporters/list';
import ListModeReporter from '../reporters/listModeReporter';
import PerfettoReporter from '../reporters/perfetto';
+import { SecretsRedactor, RedactingReporter } from '../reporters/redactingReporter';
import { wrapReporterAsV2 } from '../reporters/reporterV2';
import type { ReporterDescription } from '../../types/test';
import type { TestError } from '../../types/testReporter';
-import type { config as commonConfig, FullConfigInternal } from '../common';
+import type { FullConfigInternal } from '../common';
import type { CommonReporterOptions, Screen } from '../reporters/base';
import type { ReporterV2 } from '../reporters/reporterV2';
+import type { Secrets } from '@isomorphic/secrets';
import type { TestRunOptions } from './tasks';
export async function createReporters(config: FullConfigInternal, mode: 'list' | 'test' | 'merge', descriptions?: ReporterDescription[], runOptions?: TestRunOptions): Promise<ReporterV2[]> {
@@ -56,10 +60,10 @@ export async function createReporters(config: FullConfigInternal, mode: 'list' |
'coverage': CoverageReporter,
};
const reporters: ReporterV2[] = [];
- descriptions ??= config.config.reporter;
+ descriptions ??= config.reporterDescriptions;
const reportOptions = reporterCommandOptions(config, mode, runOptions);
for (const r of descriptions) {
- const [name, arg] = r;
+ const [name, arg] = commonConfig.stripReporterSecrets(r);
const options = resolveReporterOptions(reportOptions, { ...reportOptions, ...arg });
if (name in defaultReporters) {
reporters.push(new defaultReporters[name as commonConfig.BuiltInReporter](options));
@@ -68,6 +72,7 @@ export async function createReporters(config: FullConfigInternal, mode: 'list' |
reporters.push(wrapReporterAsV2(new reporterConstructor(options)));
}
}
+ const allSecretsRedactor = wrapReportersWithSecrets(reporters, descriptions);
if (process.env.PW_TEST_REPORTER) {
const name = process.env.PW_TEST_REPORTER;
if (name in defaultReporters) {
@@ -82,14 +87,41 @@ export async function createReporters(config: FullConfigInternal, mode: 'list' |
if (reporters.length && !someReporterPrintsToStdio) {
// Add a line/dot/list-mode reporter for convenience.
// Important to put it first, just in case some other reporter stalls onEnd.
+ let reporter: ReporterV2 | undefined;
if (mode === 'list')
- reporters.unshift(new ListModeReporter());
+ reporter = new ListModeReporter();
else if (mode !== 'merge')
- reporters.unshift(!process.env.CI ? new LineReporter() : new DotReporter());
+ reporter = !process.env.CI ? new LineReporter() : new DotReporter();
+ if (reporter)
+ reporters.unshift(allSecretsRedactor ? new RedactingReporter(reporter, allSecretsRedactor) : reporter);
}
return reporters;
}
+function wrapReportersWithSecrets(reporters: ReporterV2[], descriptions: ReporterDescription[]): SecretsRedactor | undefined {
+ const redactors = new Map<string, SecretsRedactor>();
+ const redactorFor = (secrets: Secrets) => {
+ const key = secretsKey(secrets);
+ let redactor = redactors.get(key);
+ if (!redactor) {
+ redactor = new SecretsRedactor(secrets);
+ redactors.set(key, redactor);
+ }
+ return redactor;
+ };
+ let allSecrets: Secrets | undefined = reporters.length ? [] : undefined;
+ for (let i = 0; i < reporters.length; i++) {
+ const secrets: Secrets | undefined = descriptions[i][1]?.secrets;
+ if (secrets) {
+ reporters[i] = new RedactingReporter(reporters[i], redactorFor(secrets));
+ allSecrets = allSecrets ? [...allSecrets, ...secrets] : undefined;
+ } else {
+ allSecrets = undefined;
+ }
+ }
+ return allSecrets ? redactorFor(allSecrets) : undefined;
+}
+
interface ErrorCollectingReporter extends ReporterV2 {
errors(): TestError[];
}
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index 44c7700894..43fe8285a3 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -18,16 +18,17 @@
import type { APIRequestContext, Browser, BrowserContext, BrowserContextOptions, Page, LaunchOptions, ViewportSize, Geolocation, HTTPCredentials, Locator, APIResponse, PageScreenshotOptions } from 'playwright-core';
export * from 'playwright-core';
-export type BlobReporterOptions = { outputDir?: string, fileName?: string };
-export type DotReporterOptions = { omitTags?: boolean };
-export type FailuresReporterOptions = { omitTags?: boolean };
-export type LineReporterOptions = { omitTags?: boolean };
-export type ListReporterOptions = { printSteps?: boolean, printFailuresInline?: boolean, omitTags?: boolean };
-export type GitHubReporterOptions = { omitTags?: boolean };
-export type JUnitReporterOptions = { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean, includeRetries?: boolean, omitTags?: boolean, onlyFailures?: boolean };
-export type JsonReporterOptions = { outputFile?: string, onlyFailures?: boolean };
-export type PerfettoReporterOptions = { outputFile?: string };
-export type HtmlReporterOptions = {
+export type ReporterSecretsOptions = { secrets?: { name?: string, value: string }[] };
+export type BlobReporterOptions = ReporterSecretsOptions & { outputDir?: string, fileName?: string };
+export type DotReporterOptions = ReporterSecretsOptions & { omitTags?: boolean };
+export type FailuresReporterOptions = ReporterSecretsOptions & { omitTags?: boolean };
+export type LineReporterOptions = ReporterSecretsOptions & { omitTags?: boolean };
+export type ListReporterOptions = ReporterSecretsOptions & { printSteps?: boolean, printFailuresInline?: boolean, omitTags?: boolean };
+export type GitHubReporterOptions = ReporterSecretsOptions & { omitTags?: boolean };
+export type JUnitReporterOptions = ReporterSecretsOptions & { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean, includeRetries?: boolean, omitTags?: boolean, onlyFailures?: boolean };
+export type JsonReporterOptions = ReporterSecretsOptions & { outputFile?: string, onlyFailures?: boolean };
+export type PerfettoReporterOptions = ReporterSecretsOptions & { outputFile?: string };
+export type HtmlReporterOptions = ReporterSecretsOptions & {
onlyFailures?: boolean;
outputFolder?: string;
open?: 'always' | 'never' | 'on-failure';
@@ -52,7 +53,7 @@ export type ReporterDescription = Readonly<
['json'] | ['json', JsonReporterOptions] |
['perfetto'] | ['perfetto', PerfettoReporterOptions] |
['html'] | ['html', HtmlReporterOptions] |
- ['null'] |
+ ['null'] | ['null', ReporterSecretsOptions] |
[string] | [string, any]
>;
diff --git a/tests/playwright-test/reporter-secrets.spec.ts b/tests/playwright-test/reporter-secrets.spec.ts
new file mode 100644
index 0000000000..99c4c4602c
--- /dev/null
+++ b/tests/playwright-test/reporter-secrets.spec.ts
@@ -0,0 +1,178 @@
+/**
+ * Copyright (c) Microsoft Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import fs from 'fs';
+import path from 'path';
+import { test, expect, stripAnsi } from './playwright-test-fixtures';
+import { utils } from '../../packages/playwright-core/lib/coreBundle';
+
+const reporterJS = `
+class Reporter {
+ constructor(options) {
+ this.prefix = options.prefix;
+ this.secrets = options.secrets;
+ }
+ log(message) {
+ console.log('%%' + this.prefix + ' ' + message);
+ }
+ onBegin(config, suite) {
+ this.suite = suite;
+ this.log('config ' + JSON.stringify(config.reporter));
+ }
+ onStdOut(chunk) {
+ this.log('stdout ' + chunk.toString().trim());
+ }
+ onStepEnd(test, result, step) {
+ if (step.category === 'test.step')
+ this.log('step ' + step.title + (step.error ? ' error=' + step.error.message.split('\\n')[0] : ''));
+ }
+ onTestEnd(test, result) {
+ this.log('secrets option ' + JSON.stringify(this.secrets));
+ this.log('identity ' + (result === globalThis.__lastResult));
+ globalThis.__lastResult = result;
+ for (const attachment of result.attachments.filter(a => a.body))
+ this.log('attachment ' + attachment.name + ' ' + attachment.body.toString());
+ this.log('title ' + test.title);
+ }
+ onEnd() {
+ for (const test of this.suite.allTests()) {
+ for (const result of test.results) {
+ for (const error of result.errors)
+ this.log('error ' + error.message.split('\\n')[0]);
+ this.log('snippet ' + (error => error.snippet.includes('password123'))(result.errors[0]));
+ }
+ }
+ }
+}
+module.exports = Reporter;
+`;
+
+const testJS = `
+import { test, expect } from '@playwright/test';
+test('fails ' + process.env.PASSWORD, async ({}, testInfo) => {
+ const password = process.env.PASSWORD;
+ console.log('stdout ' + password);
+ await testInfo.attach('note ' + password, { body: 'body ' + password });
+ await test.step('step ' + password, async () => {
+ expect(password).toBe('wrong ' + 'password123');
+ });
+});
+`;
+
+test('should redact secrets for reporters with the option only', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'reporter.ts': reporterJS,
+ 'playwright.config.ts': `
+ module.exports = {
+ reporter: [
+ ['./reporter.ts', { prefix: 'redacted', secrets: [{ name: 'PASSWORD', value: process.env.PASSWORD }, { name: 'EMPTY', value: '' }] }],
+ ['./reporter.ts', { prefix: 'raw' }],
+ ],
+ };
+ `,
+ 'a.spec.ts': testJS,
+ }, {}, { PASSWORD: 'password123' });
+
+ expect(result.exitCode).toBe(1);
+ expect(result.outputLines).toEqual(expect.arrayContaining([
+ 'redacted stdout stdout <secret>PASSWORD</secret>',
+ 'redacted step step <secret>PASSWORD</secret> error=Error: expect(received).toBe(expected) // Object.is equality',
+ 'redacted secrets option undefined',
+ 'redacted attachment note <secret>PASSWORD</secret> body <secret>PASSWORD</secret>',
+ 'redacted title fails <secret>PASSWORD</secret>',
+ 'redacted error Error: expect(received).toBe(expected) // Object.is equality',
+ 'redacted snippet false',
+ 'raw stdout stdout password123',
+ 'raw step step password123 error=Error: expect(received).toBe(expected) // Object.is equality',
+ 'raw secrets option undefined',
+ 'raw attachment note password123 body password123',
+ 'raw title fails password123',
+ 'raw snippet true',
+ ]));
+ // Secrets are not exposed through the config.
+ const configLines = result.outputLines.filter(line => line.includes(' config '));
+ expect(configLines).toHaveLength(2);
+ for (const line of configLines) {
+ expect(line).not.toContain('"secrets"');
+ expect(line).not.toContain('password123');
+ expect(line).toContain('"prefix":"redacted"');
+ }
+ expect(result.outputLines.filter(line => line.startsWith('redacted ')).join('\n')).not.toContain('password123');
+});
+
+test('should redact secrets in built-in reporters', async ({ runInlineTest }, testInfo) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `
+ module.exports = {
+ reporter: [
+ ['list', { secrets: [{ name: 'PASSWORD', value: process.env.PASSWORD }] }],
+ ['json', { secrets: [{ name: 'PASSWORD', value: process.env.PASSWORD }], outputFile: 'secrets-report.json' }],
+ ['html', { secrets: [{ name: 'PASSWORD', value: process.env.PASSWORD }], open: 'never' }],
+ ],
+ };
+ `,
+ 'a.spec.ts': testJS,
+ }, {}, { PASSWORD: 'password123', PLAYWRIGHT_HTML_OPEN: 'never' });
+
+ expect(result.exitCode).toBe(1);
+ expect(stripAnsi(result.output)).not.toContain('password123');
+ expect(stripAnsi(result.output)).toContain('fails <secret>PASSWORD</secret>');
+ expect(stripAnsi(result.output)).toContain('Expected: "wrong <secret>PASSWORD</secret>"');
+
+ const json = fs.readFileSync(testInfo.outputPath('secrets-report.json'), 'utf8');
+ expect(json).not.toContain('password123');
+ expect(json).toContain('fails <secret>PASSWORD</secret>');
+ expect(JSON.parse(json).config.reporter).toEqual([
+ ['list', {}],
+ ['json', { outputFile: 'secrets-report.json' }],
+ ['html', { open: 'never' }],
+ ]);
+
+ // The HTML report keeps its data in a zip embedded into index.html.
+ const reportDir = testInfo.outputPath('playwright-report');
+ const indexHtml = fs.readFileSync(path.join(reportDir, 'index.html'), 'utf8');
+ const base64 = /<template id="playwrightReportBase64">data:application\/zip;base64,([^<]*)<\/template>/.exec(indexHtml)![1];
+ const zipPath = testInfo.outputPath('report.zip');
+ fs.writeFileSync(zipPath, Buffer.from(base64, 'base64'));
+ const zipFile = new utils.ZipFile(zipPath);
+ const entries = await zipFile.entries();
+ expect(entries.length).toBeGreaterThan(0);
+ let reportText = '';
+ for (const entry of entries)
+ reportText += (await zipFile.read(entry)).toString();
+ zipFile.close();
+ expect(reportText).not.toContain('password123');
+ expect(reportText).toContain('fails <secret>PASSWORD</secret>');
+});
+
+test('should redact secrets in the convenience reporter when all reporters have secrets', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'playwright.config.ts': `
+ module.exports = {
+ reporter: [
+ ['json', { secrets: [{ name: 'PASSWORD', value: process.env.PASSWORD }], outputFile: 'secrets-report.json' }],
+ ['null', { secrets: [{ value: 'stdout' }] }],
+ ],
+ };
+ `,
+ 'a.spec.ts': testJS,
+ }, {}, { PASSWORD: 'password123', PW_TEST_REPORTER: '' });
+
+ expect(result.exitCode).toBe(1);
+ expect(stripAnsi(result.output)).not.toContain('password123');
+ expect(stripAnsi(result.output)).toContain('fails <secret>PASSWORD</secret>');
+ expect(stripAnsi(result.output)).toContain('<secret/> <secret>PASSWORD</secret>');
+});
diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts
index 31e52a68d3..6ccb2a602e 100644
--- a/utils/generate_types/overrides-test.d.ts
+++ b/utils/generate_types/overrides-test.d.ts
@@ -17,16 +17,17 @@
import type { APIRequestContext, Browser, BrowserContext, BrowserContextOptions, Page, LaunchOptions, ViewportSize, Geolocation, HTTPCredentials, Locator, APIResponse, PageScreenshotOptions } from 'playwright-core';
export * from 'playwright-core';
-export type BlobReporterOptions = { outputDir?: string, fileName?: string };
-export type DotReporterOptions = { omitTags?: boolean };
-export type FailuresReporterOptions = { omitTags?: boolean };
-export type LineReporterOptions = { omitTags?: boolean };
-export type ListReporterOptions = { printSteps?: boolean, printFailuresInline?: boolean, omitTags?: boolean };
-export type GitHubReporterOptions = { omitTags?: boolean };
-export type JUnitReporterOptions = { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean, includeRetries?: boolean, omitTags?: boolean, onlyFailures?: boolean };
-export type JsonReporterOptions = { outputFile?: string, onlyFailures?: boolean };
-export type PerfettoReporterOptions = { outputFile?: string };
-export type HtmlReporterOptions = {
+export type ReporterSecretsOptions = { secrets?: { name?: string, value: string }[] };
+export type BlobReporterOptions = ReporterSecretsOptions & { outputDir?: string, fileName?: string };
+export type DotReporterOptions = ReporterSecretsOptions & { omitTags?: boolean };
+export type FailuresReporterOptions = ReporterSecretsOptions & { omitTags?: boolean };
+export type LineReporterOptions = ReporterSecretsOptions & { omitTags?: boolean };
+export type ListReporterOptions = ReporterSecretsOptions & { printSteps?: boolean, printFailuresInline?: boolean, omitTags?: boolean };
+export type GitHubReporterOptions = ReporterSecretsOptions & { omitTags?: boolean };
+export type JUnitReporterOptions = ReporterSecretsOptions & { outputFile?: string, stripANSIControlSequences?: boolean, includeProjectInTestName?: boolean, includeRetries?: boolean, omitTags?: boolean, onlyFailures?: boolean };
+export type JsonReporterOptions = ReporterSecretsOptions & { outputFile?: string, onlyFailures?: boolean };
+export type PerfettoReporterOptions = ReporterSecretsOptions & { outputFile?: string };
+export type HtmlReporterOptions = ReporterSecretsOptions & {
onlyFailures?: boolean;
outputFolder?: string;
open?: 'always' | 'never' | 'on-failure';
@@ -51,7 +52,7 @@ export type ReporterDescription = Readonly<
['json'] | ['json', JsonReporterOptions] |
['perfetto'] | ['perfetto', PerfettoReporterOptions] |
['html'] | ['html', HtmlReporterOptions] |
- ['null'] |
+ ['null'] | ['null', ReporterSecretsOptions] |
[string] | [string, any]
>;