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

Reviewer's Guide

The PR centralizes webhook URL handling with a shared trim helper and schema regex, applies trimming across both persistence paths, and adds a standalone regression test covering whitespace rejection, HTTP(S) enforcement, valid HTTPS input, and stored-value normalization.

Sequence diagram for webhook URL validation and persistence

sequenceDiagram
    participant Client
    participant webhookSchema
    participant WebhookController
    participant persistedWebhookUrl
    participant Storage

    Client->>webhookSchema: validate(webhookData)
    alt valid http or https URL without whitespace
        webhookSchema-->>WebhookController: validation succeeds
        WebhookController->>persistedWebhookUrl: persistedWebhookUrl(url)
        persistedWebhookUrl-->>WebhookController: trimmed URL
        WebhookController->>Storage: persist trimmed URL
    else invalid scheme or whitespace
        webhookSchema-->>Client: validation fails
    end
Loading

File-Level Changes

Change Details Files
Centralize webhook URL normalization before persistence.
  • Added a shared helper that trims string URLs while preserving undefined values.
  • Applied normalization to both webhook persistence paths in the controller.
src/api/integrations/event/webhook/webhook.controller.ts
src/api/integrations/event/webhook/webhook.url.ts
Restrict webhook URL schema values to unpadded HTTP(S) URLs.
  • Added an anchored pattern requiring http:// or https:// and rejecting whitespace.
  • Referenced the shared pattern from the webhook schema and documented the constraint.
src/api/integrations/event/webhook/webhook.schema.ts
src/api/integrations/event/webhook/webhook.url.ts
Add focused regression coverage for validation and persistence behavior.
  • Verified padded URLs and non-HTTP(S) schemes are rejected.
  • Verified a normal HTTPS URL is accepted and persisted values are trimmed.
src/api/integrations/event/webhook/webhook.url.test.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@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.

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