fix(webhook): trim webhook URL and enforce http/https scheme in schema validation by Gabrieljr42 · Pull Request #2757 · evolution-foundation/evolution-api · GitHub
Skip to content

fix(webhook): trim webhook URL and enforce http/https scheme in schema validation - #2757

Open
Gabrieljr42 wants to merge 3 commits into
evolution-foundation:mainfrom
Gabrieljr42:fix/webhook-url-trim-and-scheme
Open

Gabrieljr42 wants to merge 3 commits into
evolution-foundation:mainfrom
Gabrieljr42:fix/webhook-url-trim-and-scheme

Conversation

@Gabrieljr42

@Gabrieljr42 Gabrieljr42 commented Oct 15, 2026 •

Copy link
Copy Markdown

Description

Webhook configuration now rejects non-HTTP(S) schemes, missing hosts and whitespace in both webhook settings and nested webhook settings supplied during instance creation. Both schemas use a shared URL format backed by the Node URL parser, so malformed hosts and ports are rejected before instance creation. The controller applies the same check before calling Prisma and returns HTTP 400 for invalid destinations.

Valid uppercase and mixed-case HTTP(S) schemes are accepted and stored in lowercase so the existing delivery scheme check can send them. Controller persistence trims surrounding whitespace on both create and update. Schema validation still rejects whitespace in incoming URL values, including final line terminators.

The regression test calls WebhookController.set with an injected Prisma stub and asserts the URL passed to both persistence paths. It also checks that invalid destinations never reach Prisma. It does not start a database, Redis or a message broker.

Type of Change

  • Bug fix
  • Regression tests and CI integration

Testing

  • npm run test:webhook, 7 tests passed
  • npm run lint:check
  • npm run build
  • Commit hooks passed ESLint and TypeScript checks

The cases cover normal HTTPS, HTTP on localhost with a port, IPv6, uppercase and mixed-case schemes, missing hosts, invalid ports, invalid IPv6, unsupported schemes, padded values, embedded whitespace and final LF/CRLF. Instance creation without webhook settings remains valid. Supplying webhook settings requires a URL.

The code quality workflow runs npm run test:webhook before the build.

Review follow-up

The update addresses the five Sourcery findings by validating nested instance webhook URLs, checking complete destinations before persistence, accepting uppercase schemes, rejecting trailing newlines at the schema boundary, and testing controller persistence rather than only the trim helper.

Compatibility

Malformed webhook destinations that were previously accepted now return a validation error. No database schema or migration changes are required.

…a validation

Persist a trimmed webhook URL and reject values that are not http or https or that contain whitespace.

Verified with npx tsx src/api/integrations/event/webhook/webhook.url.test.ts, npm run lint, and npm run build.

Signed-off-by: Gabrieljr42 <gabrieljunior42566@gmail.com>
@sourcery-ai

sourcery-ai Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 5 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/api/integrations/event/webhook/webhook.schema.ts" line_range="36" />
<code_context>
-        url: { type: 'string' },
+        url: {
+          type: 'string',
+          pattern: WEBHOOK_HTTP_URL_PATTERN,
+          description: 'The "url" must be an http or https URL without whitespace',
+        },
</code_context>
<issue_to_address>
**Instance webhooks are skipped**

When an instance is created with a nested webhook URL using a non-HTTP(S) scheme, `instanceSchema` does not validate nested `webhook.url`, so `EventManager.setInstance` forwards it to `WebhookController.set`, which persists it; `emit` then skips the non-HTTP(S) destination and the instance sends no webhook events.

Validate the nested URL in `instanceSchema` or apply `webhookSchema` during instance creation before calling `EventManager.setInstance`.
</issue_to_address>

### Comment 2
<location path="src/api/integrations/event/webhook/webhook.url.ts" line_range="2" />
<code_context>
+/** http(s) URL with no whitespace. Anchored so the schema rejects any other scheme or padding. */
+export const WEBHOOK_HTTP_URL_PATTERN = '^https?:\\/\\/\\S+$';
+
+/** Value stored for a webhook URL. Callers persist this, not the raw input. */
</code_context>
<issue_to_address>
**Malformed destinations pass validation**

When a caller submits a non-whitespace value with an HTTP(S) prefix that is not a valid URL, `WEBHOOK_HTTP_URL_PATTERN` checks only the scheme and whitespace, so malformed destinations pass validation and are persisted; Axios then fails to deliver the webhook event.

Validate that the entire value is a valid HTTP(S) URL, including a host, rather than relying on a scheme-and-whitespace pattern.

Also at `src/api/integrations/event/webhook/webhook.schema.ts:36`.
</issue_to_address>

### Comment 3
<location path="src/api/integrations/event/webhook/webhook.url.ts" line_range="2" />
<code_context>
+/** http(s) URL with no whitespace. Anchored so the schema rejects any other scheme or padding. */
+export const WEBHOOK_HTTP_URL_PATTERN = '^https?:\\/\\/\\S+$';
+
+/** Value stored for a webhook URL. Callers persist this, not the raw input. */
</code_context>
<issue_to_address>
**Valid uppercase schemes are rejected**

When a caller supplies a valid URL with an uppercase or mixed-case HTTP(S) scheme, `WEBHOOK_HTTP_URL_PATTERN` is case-sensitive, so validation rejects the destination and callers cannot save the webhook configuration.

Make the scheme check case-insensitive.

Also at `src/api/integrations/event/webhook/webhook.schema.ts:36`.
</issue_to_address>

### Comment 4
<location path="src/api/integrations/event/webhook/webhook.url.ts" line_range="2" />
<code_context>
+/** http(s) URL with no whitespace. Anchored so the schema rejects any other scheme or padding. */
+export const WEBHOOK_HTTP_URL_PATTERN = '^https?:\\/\\/\\S+$';
+
+/** Value stored for a webhook URL. Callers persist this, not the raw input. */
</code_context>
<issue_to_address>
**Trailing newlines pass validation**

When a submitted URL ends in a line terminator, javaScript’s `$` matches before a final line terminator, so `WEBHOOK_HTTP_URL_PATTERN` accepts the value and schema validation does not reject the trailing whitespace.

Change the pattern to require the actual end of the input after the URL.

Also at `src/api/integrations/event/webhook/webhook.schema.ts:36`.
</issue_to_address>

### Comment 5
<location path="src/api/integrations/event/webhook/webhook.url.test.ts" line_range="23-24" />
<code_context>
+assert.equal(httpsResult.valid, true, 'normal https:// URL must be accepted');
+console.log('normal https:// URL accepted');
+
+const persisted = persistedWebhookUrl('  https://example.com/hook  ');
+assert.equal(persisted, 'https://example.com/hook');
+console.log('persisted value is trimmed', persisted);
</code_context>
<issue_to_address>
**Controller stores padded URLs**

When a controller persistence call site stops using the helper, if `WebhookController.set` passes `data.webhook.url` directly to Prisma instead of calling `persistedWebhookUrl`, the test still passes: `webhook.url.test.ts` lines 23–24 invoke and assert only the helper, not the value persisted by the controller.

Exercise `WebhookController.set` and assert that the value passed to Prisma is trimmed.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 5 findings to address first, and webhook URL values are now trimmed before persistence, so a wrong normalization can remain in stored configuration after rollback, but it is bounded and can be corrected by updating the webhook URL. The stricter schema may reject previously accepted values; that is a reversible configuration/API behavior change.

Blocking findings: src/api/integrations/event/webhook/webhook.schema.ts:36, src/api/integrations/event/webhook/webhook.url.ts:2, src/api/integrations/event/webhook/webhook.url.ts:2, src/api/integrations/event/webhook/webhook.url.ts:2, src/api/integrations/event/webhook/webhook.url.test.ts:24


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/api/integrations/event/webhook/webhook.schema.ts Outdated
Comment thread src/api/integrations/event/webhook/webhook.url.ts Outdated
Comment thread src/api/integrations/event/webhook/webhook.url.ts Outdated
Comment thread src/api/integrations/event/webhook/webhook.url.ts Outdated
Comment thread src/api/integrations/event/webhook/webhook.url.test.ts Outdated

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

magnocarvalho added a commit to magnocarvalho/evolution-api that referenced this pull request Oct 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
magnocarvalho added a commit to magnocarvalho/evolution-api that referenced this pull request Oct 23, 2026
Upstream PRs evolution-foundation#2760, evolution-foundation#2634, evolution-foundation#2757; WhatsApp Flows, CTWA referral, voice notes and Meta delivery errors for the Meta/EvoHub channels.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant