Repository navigation
fix(api): reject webhook body templates that don't compile, add /api/v2/webhooks/validate - #3365
Conversation
🦋 Changeset detectedLatest commit: 9bc49f9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Review
3 finding(s): 🔴 0 critical · 🟠 0 major · 🔵 3 minor 2 posted as inline comment(s) on the changed lines. 1 listed below. Findings outside the changed lines1 minor
Severity is the reviewer's own estimate and is used for ordering, not filtering. No finding blocks merge automatically; a maintainer decides. |
[Critical impact] Adds validation to webhook body templates and new API endpoint. Fix the rejection of working dynamic partials before merging.
|
Deep Review✅ No critical issues found. The validation logic is well-constructed: the custom AST walk in 🟡 P2 -- recommended
🔵 P3 nitpicks (1)
Reviewers (2): ce-security-reviewer (returned clean), orchestrator correctness/adversarial/previous-comments analysis. Testing gaps:
No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond |
| const partialRefs: hbs.AST.PartialStatement[] = []; | ||
|
|
||
| const checkHelperCall = (node: HelperCall) => { | ||
| if (found != null || node.params.length === 0) return; |
There was a problem hiding this comment.
🔵 minor — Built-in block helpers called with no argument pass validation but throw on every delivery
checkHelperCall returns early when node.params.length === 0, so {{#if}}x{{/if}}, {{#unless}}…, {{#with}}… and {{#each}}… are accepted. Stock Handlebars throws #if requires exactly one argument / Must pass iterator to #each when these render, and delivery uses stock Handlebars (createHandlebarsWithHelpers in tasks/checkAlerts/transports/generic.ts). That breaks the 'throws on every delivery' guarantee in the doc comment. Fix: reject if/unless/with/each BlockStatements where params.length !== 1.
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
| ); | ||
| } | ||
| super.DecoratorBlock(node); | ||
| } |
There was a problem hiding this comment.
🔵 minor — Inline partials are treated as global, so a partial used outside the block that defines it passes
inlinePartials is one flat set for the whole template. But {{#*inline}} only registers its partial inside the program where it appears. So {{#if title}}{{#*inline "p"}}x{{/inline}}{{/if}}{{> p}} passes validation and then fails on every render with 'The partial p could not be found'. Fix: track inline definitions per Program scope, using a stack pushed and popped in Program, and resolve each PartialStatement against the scopes that enclose it.
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
| service, | ||
| body, | ||
| }: { | ||
| service: WebhookService; |
There was a problem hiding this comment.
🔵 minor — Unknown-helper/partial check lives only in the API, so the webhook form's inline error never shows it
packages/app/src/components/TeamSettings/WebhookForm.tsx:148 shows inline errors by calling getWebhookTemplateError from common-utils core/handlebarsEnv.ts, and that only catches compile errors. A body like {{#if (equals state "ALERT")}} shows no inline error. Save then fails with only a generic toast built from the 400 message. Fix: move findUndeliverableConstruct and the helper list into common-utils next to getWebhookTemplateError (or have that function call it) so the form and the API use one check. Have createHandlebarsWithHelpers register from that shared list too; that also removes the hand-synced WEBHOOK_BODY_HELPERS copy and the import-cycle workaround.
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
| // `@`-prefixed partials, like `@partial-block`, come from Handlebars. | ||
| return !name.startsWith('@') && !inlinePartials.has(name); |
There was a problem hiding this comment.
Unknown partials pass validation
The new startsWith('@') exemption accepts unknown partials such as {{> @missing}}. Handlebars does not supply arbitrary @ partials, and delivery registers none. This body now passes create and /validate but fails when an alert renders it. Limit the exemption to @partial-block where Handlebars supplies it.
Knowledge Base Used: Alerting and notifications
jordan-simonovski
left a comment
There was a problem hiding this comment.
LGTM! Nice one
The base branch was changed.
Validate generic and incident.io bodies with getWebhookTemplateError on create, update, and test across the internal API, External API v2, and MCP. Add POST /api/v2/webhooks/validate for plan-time checks.
…lidate normalized
…n module (greptile P1, P2)
c466891 to
9bc49f9
Compare
🔴 Tier 4 — CriticalTouches authentication, tenancy data models, the public API or shipped database config — or substantially changes the query rendering engine, background tasks, the OTel pipeline, image build, or release CI. Why this tier:
Review process: Deep review from a domain expert. Synchronous walkthrough may be required. Stats
|
Duplicate code analysis1 new clone introduced by this PR.
If one of these duplicates a helper that already exists, import it instead jscpd compared this branch against |
| checkHelperCall(node); | ||
| super.SubExpression(node); | ||
| } | ||
| PartialStatement(node: hbs.AST.PartialStatement) { |
There was a problem hiding this comment.
🔵 minor — Inline partials are collected template-wide, but Handlebars scopes them to the block that defines them
inlinePartials is one flat set, so {{#if x}}{{#*inline "p"}}y{{/inline}}{{/if}}{{> p}} passes validation. At delivery it throws The partial p could not be found, because an inline partial is only visible inside the program that declares it and that program's children. This is the kind of always-failing body the PR says it rejects. Track inline definitions per Program scope: push a scope on entering a Program and pop it on exit, then resolve each PartialStatement against the scopes enclosing it. Add a test with the definition nested inside a block.
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
| }); | ||
|
|
||
| const nameOf = (node: hbs.AST.Expression): string => | ||
| 'original' in node ? String(node.original) : node.type; |
There was a problem hiding this comment.
🔵 minor — The unknown-helper/partial/decorator check is API-only, so the webhook form still lets these bodies through to a 400 on save
getWebhookTemplateError lives in @hyperdx/common-utils/dist/core/handlebarsEnv and WebhookForm.tsx:148 runs it inline. findUndeliverableConstruct is only in packages/api, so the form shows no error for {{#if (equals state "ALERT")}} and the user only finds out when save returns a 400. Move findUndeliverableConstruct and WEBHOOK_BODY_HELPERS into handlebarsEnv.ts next to getWebhookTemplateError, or fold them into it. That gives the API and the form one check. It also removes the import-cycle reason given for keeping the helper list here.
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
| const unknownPartial = partialRefs.find(ref => { | ||
| const name = nameOf(ref.name); | ||
| // `@`-prefixed partials, like `@partial-block`, come from Handlebars. | ||
| return !name.startsWith('@') && !inlinePartials.has(name); |
There was a problem hiding this comment.
Working dynamic partials are rejected
The partial check reports Unknown partial "SubExpression" for every dynamic name, even when it resolves to a defined inline partial. For example, {{#*inline "p"}}{"text":"hello"}{{/inline}}{{> (lookup "p" 0)}} renders a valid body with the helpers delivery provides, but create, update, test, and /validate now reject it.
Do not treat a dynamic name as a missing literal partial.
| DecoratorBlock(node: hbs.AST.DecoratorBlock) { | ||
| const [partial] = node.params; | ||
| if (nameOf(node.path) === 'inline' && partial?.type === 'StringLiteral') { | ||
| inlinePartials.add(nameOf(partial)); |
There was a problem hiding this comment.
Skipped definitions pass validation
The inline partial check collects definitions from every branch into one set. A body such as {{#if false}}{{#*inline "p"}}{"text":"hello"}{{/inline}}{{/if}}{{> p}} therefore passes validation, although the branch that defines p never runs. Delivery has no registered p, so rendering fails before any webhook request is sent.
Check whether each definition is available where the partial is used.
E2E Test Results✅ All tests passed • 417 passed • 1 skipped • 1285s
Tests ran across 4 shards in parallel. |
Builds on #3350 (base is that branch; retarget to
mainonce it merges).Why
#3350 shows Handlebars compile errors in the webhook form, but saving still goes through, and the External API v2 and MCP never checked the body. A body that doesn't compile fails on every alert delivery. This rejects it at write time. It also adds the
/validateendpoint requested on #3350 so Terraform can check webhook configs at plan time.What changed
Invalid webhook body template (line 1, column 29): Parse error on line 1: …. Slack and empty bodies are skipped.POST /webhooks,PUT /webhooks/:id,POST /webhooks/test(rejects before sending).clickstack_save_webhookget the check throughcreateWebhook/updateWebhook. MCP returns it as a user error.POST /api/v2/webhooks/validate, modelled onPOST /api/v2/dashboards/validate. It parses withexternalWebhookCreateSchema, runs the same URL and body checks as create, never saves, and always returns 200 with{ valid, errors: [{ path, message }], normalized }. It doesn't check name uniqueness.openapi.jsonis regenerated.bodydescription anddocs/alert-webhook-template-variables.mdnow say these templates are rejected.Tests
make dev-int FILE=webhooks: 154 passed. Coverage:/testrejects without sending{{#if (eq state "ALERT")}}…still saves/validate: valid, invalid body, URL and body errors together, schema errors, 401 without a key