Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(builder): "Copy Error for AI" no longer copies API keys held in step URLs (#15706)

Bartosz Majewski88b61e9d1317
diff --git a/packages/web/src/app/builder/data-display/explanation-prompt.test.ts b/packages/web/src/app/builder/data-display/explanation-prompt.test.ts
new file mode 100644
index 00000000000..9b4eb27803a
--- /dev/null
+++ b/packages/web/src/app/builder/data-display/explanation-prompt.test.ts
@@ -0,0 +1,226 @@
+import { FriendlyPieceError } from '@activepieces/core-utils';
+import { describe, it, expect } from 'vitest';
+
+import {
+ ErrorExplanationContext,
+ explanationPromptUtils,
+ StepPropertySnapshot,
+} from './explanation-prompt';
+
+const buildError = (
+ overrides: Partial<FriendlyPieceError> = {},
+): FriendlyPieceError => ({
+ __apErrorVersion: 1,
+ message: 'Request failed with status code 401',
+ ...overrides,
+});
+
+const buildContext = (
+ properties: StepPropertySnapshot[],
+): ErrorExplanationContext => ({
+ stepKind: 'action',
+ stepName: 'send_http_request',
+ stepDisplayName: 'Send HTTP request',
+ stepProperties: properties,
+});
+
+const build = (
+ error: FriendlyPieceError,
+ properties: StepPropertySnapshot[] = [],
+) => explanationPromptUtils.build({ error, context: buildContext(properties) });
+
+const buildWithUrl = (url: string) =>
+ build(buildError(), [
+ { name: 'url', displayName: 'URL', type: 'SHORT_TEXT', currentValue: url },
+ ]);
+
+const nest = (depth: number, leaf: unknown): unknown =>
+ depth === 0 ? leaf : { next: nest(depth - 1, leaf) };
+
+describe('explanationPromptUtils.build - url secrets', () => {
+ it('redacts a secret query param in a non-secret step property', () => {
+ const prompt = buildWithUrl('https://example.com/api?api_key=SECRET');
+
+ expect(prompt).not.toContain('SECRET');
+ expect(prompt).toContain('api_key=[REDACTED]');
+ });
+
+ it('redacts a secret query param in a step property default value', () => {
+ const prompt = build(buildError(), [
+ {
+ name: 'endpoint',
+ type: 'SHORT_TEXT',
+ defaultValue: 'https://example.com/api?token=SECRET',
+ },
+ ]);
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts secrets in urls nested inside object and array property values', () => {
+ const prompt = build(buildError(), [
+ {
+ name: 'headers',
+ type: 'OBJECT',
+ currentValue: {
+ callback: 'https://example.com/hook?access_token=SECRET',
+ extras: ['https://example.com/a?client_secret=SECRET'],
+ },
+ },
+ ]);
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts both halves of the url userinfo', () => {
+ const prompt = buildWithUrl(
+ 'https://SECRET_USER:[email protected]/api',
+ );
+
+ expect(prompt).not.toContain('SECRET_USER');
+ expect(prompt).not.toContain('SECRET_PASS');
+ expect(prompt).toContain('example.com');
+ });
+
+ it('redacts a token carried as the whole url userinfo', () => {
+ const prompt = buildWithUrl('https://[email protected]/v1/charges');
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts a secret carried in the url fragment', () => {
+ const prompt = buildWithUrl(
+ 'https://example.com/callback#access_token=SECRET',
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts a secret param in a url whose query holds spaces and quotes', () => {
+ const prompt = buildWithUrl(
+ "https://example.com/odata?$filter=name eq 'Acme'&api_key=SECRET",
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts a secret param in a url embedded in free text after a space', () => {
+ const prompt = build(
+ buildError({
+ message:
+ "request to https://example.com/odata?$filter=name eq 'Acme'&api_key=SECRET failed",
+ }),
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts secret params in relative and protocol-relative urls', () => {
+ const prompt = build(buildError(), [
+ { name: 'path', type: 'SHORT_TEXT', currentValue: '/api?token=SECRET' },
+ {
+ name: 'other',
+ type: 'SHORT_TEXT',
+ currentValue: '//example.com/api?api_key=SECRET',
+ },
+ ]);
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts a secret param whose name is percent-encoded', () => {
+ const prompt = buildWithUrl('https://example.com/api?api%5Fkey=SECRET');
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('leaves a param whose name holds a malformed escape untouched', () => {
+ const prompt = buildWithUrl('https://example.com/api?a%ZZb=KEPT');
+
+ expect(prompt).toContain('a%ZZb=KEPT');
+ });
+
+ it('redacts a secret in a payload nested deeper than the object walk', () => {
+ const prompt = build(
+ buildError({
+ responseBody: nest(12, {
+ docs: 'https://example.com/d?api_key=SECRET',
+ }),
+ }),
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts a url embedded in the error message', () => {
+ const prompt = build(
+ buildError({
+ message:
+ 'request to https://example.com/api?api_key=SECRET failed, reason: ECONNRESET',
+ }),
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts a url embedded in the api message', () => {
+ const prompt = build(
+ buildError({ apiMessage: 'callback https://example.com?token=SECRET' }),
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('redacts secrets in urls nested inside the response body', () => {
+ const prompt = build(
+ buildError({
+ responseBody: { docs: 'https://example.com/docs?api_key=SECRET' },
+ }),
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('keeps redacting the request url', () => {
+ const prompt = build(
+ buildError({ requestUrl: 'https://example.com/api?api_key=SECRET' }),
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ });
+
+ it('keeps text around a redacted url intact', () => {
+ const prompt = build(
+ buildError({
+ message: 'see [docs](https://example.com/d?api_key=SECRET) for details',
+ }),
+ );
+
+ expect(prompt).not.toContain('SECRET');
+ expect(prompt).toContain('api_key=[REDACTED]) for details');
+ });
+
+ it('leaves strings without url secrets byte-identical', () => {
+ const untouched = [
+ 'https://alpha.example.com',
+ 'https://beta.example.com/api?page=2',
+ 'https://gamma.example.com/orders/{{step_1.id}}',
+ 'https://delta.example.com/docs#section-1',
+ 'https://epsilon.example.com/search?q=hello world',
+ 'not a url at all',
+ 'mailto:[email protected]',
+ ];
+ const prompt = build(
+ buildError(),
+ untouched.map((value, index) => ({
+ name: `prop_${index}`,
+ type: 'SHORT_TEXT',
+ currentValue: value,
+ })),
+ );
+
+ for (const value of untouched) {
+ expect(prompt).toContain(`current=${value}\n`);
+ }
+ });
+});
diff --git a/packages/web/src/app/builder/data-display/explanation-prompt.ts b/packages/web/src/app/builder/data-display/explanation-prompt.ts
index 21bdf4758f4..9d7a8789bd2 100644
--- a/packages/web/src/app/builder/data-display/explanation-prompt.ts
+++ b/packages/web/src/app/builder/data-display/explanation-prompt.ts
@@ -1,4 +1,8 @@
-import { FriendlyPieceError, isNil } from '@activepieces/core-utils';
+import {
+ FriendlyPieceError,
+ isNil,
+ tryCatchSync,
+} from '@activepieces/core-utils';
const MAX_BODY_PAYLOAD_CHARS = 4000;
const MAX_PROPERTY_VALUE_CHARS = 400;
@@ -7,6 +11,11 @@ const REDACTED_PLACEHOLDER = '[REDACTED]';
const SECRET_KEY_REGEX =
/^(authorization|cookie|set-cookie|password|new[_-]?password|current[_-]?password|token|access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|x[_-]?api[_-]?key|secret|client[_-]?secret|private[_-]?key|bearer|x[_-]?auth)$/i;
+const URL_SECRET_PARAM_REGEX =
+ /([?&#])([A-Za-z0-9_.%-]+)=([^&#\s"'<>\\)\]}]*)/g;
+
+const URL_USERINFO_REGEX = /([a-z][a-z0-9+.-]*:\/\/)([^/?#\s"'<>@\\]+)@/gi;
+
const SECRET_PROPERTY_TYPES = new Set([
'SECRET_TEXT',
'OAUTH2',
@@ -38,20 +47,27 @@ const redactSecrets = (value: unknown, depth = 0): unknown => {
return value;
};
-const redactUrlSecrets = (url: string): string => {
- try {
- const parsed = new URL(url);
- for (const key of Array.from(parsed.searchParams.keys())) {
- if (SECRET_KEY_REGEX.test(key)) {
- parsed.searchParams.set(key, REDACTED_PLACEHOLDER);
- }
- }
- return parsed.toString();
- } catch {
- return url;
- }
+const decodeParamKey = (key: string): string => {
+ const { data } = tryCatchSync(() => decodeURIComponent(key));
+ return isNil(data) ? key : data;
};
+const redactUrlSecrets = (value: string): string =>
+ value
+ .replace(
+ URL_SECRET_PARAM_REGEX,
+ (
+ match: string,
+ delimiter: string,
+ key: string,
+ paramValue: string,
+ ): string =>
+ SECRET_KEY_REGEX.test(decodeParamKey(key)) && paramValue.length > 0
+ ? `${delimiter}${key}=${REDACTED_PLACEHOLDER}`
+ : match,
+ )
+ .replace(URL_USERINFO_REGEX, `$1${REDACTED_PLACEHOLDER}@`);
+
const truncateForPrompt = (
value: unknown,
maxChars = MAX_BODY_PAYLOAD_CHARS,
@@ -59,8 +75,9 @@ const truncateForPrompt = (
if (isNil(value)) {
return '';
}
- const serialized =
- typeof value === 'string' ? value : JSON.stringify(value, null, 2);
+ const serialized = redactUrlSecrets(
+ typeof value === 'string' ? value : JSON.stringify(value, null, 2),
+ );
if (serialized.length <= maxChars) {
return serialized;
}