fix(server): avoid excessive backtracking in webhook templates (#19160)
Co-authored-by: Simone <[email protected]>
This commit is contained in:
1 parent
5e5c52b7a7
commit
5e5abc92f4
2 files changed
+23
-2
No files matched your search
@@ -52,6 +52,25 @@ describe("renderWebhookPrompt", () => {
|
||||
assert.deepEqual(rendered.missing, ["body.nope", "headers.x-missing", "unknown"]);
|
||||
});
|
||||
|
||||
it("trims placeholder whitespace before resolving and reporting missing fields", () => {
|
||||
const rendered = renderWebhookPrompt(
|
||||
"{{\tbody.action\n}}|{{ body.nope }}|{{\nbody.nope\t}}|{{ \t\n }}",
|
||||
githubPullRequest,
|
||||
);
|
||||
assert.equal(rendered.prompt, "opened|||");
|
||||
assert.deepEqual(rendered.missing, ["body.nope", ""]);
|
||||
});
|
||||
|
||||
it("preserves unclosed whitespace-heavy placeholders and resolves later valid ones", () => {
|
||||
const unfinished = `Notify {{${" ".repeat(1024)}body`;
|
||||
const rendered = renderWebhookPrompt(
|
||||
`${unfinished}\n{{body.pull_request.number}}`,
|
||||
githubPullRequest,
|
||||
);
|
||||
assert.equal(rendered.prompt, `${unfinished}\n45`);
|
||||
assert.deepEqual(rendered.missing, []);
|
||||
});
|
||||
|
||||
it("addresses form-encoded bodies and leaves other bodies to {{body}}", () => {
|
||||
const form = renderWebhookPrompt("{{body.text}} by {{body.user_name}}", {
|
||||
...githubPullRequest,
|
||||
|
||||
@@ -69,7 +69,8 @@ export function redactQuery(query: string): string {
|
||||
.join("&");
|
||||
}
|
||||
|
||||
const PLACEHOLDER = /\{\{\s*([^{}]*?)\s*\}\}/g;
|
||||
// Trim after matching so unclosed placeholders cannot backtrack over whitespace partitions.
|
||||
const PLACEHOLDER = /\{\{([^{}]*)\}\}/g;
|
||||
|
||||
function parseBody(request: WebhookRequest): unknown {
|
||||
const contentType = (request.headers["content-type"] ?? "").toLowerCase();
|
||||
@@ -144,7 +145,8 @@ export function renderWebhookPrompt(
|
||||
}
|
||||
};
|
||||
|
||||
const prompt = template.replace(PLACEHOLDER, (_match, expression: string) => {
|
||||
const prompt = template.replace(PLACEHOLDER, (_match, rawExpression: string) => {
|
||||
const expression = rawExpression.trim();
|
||||
const value = resolve(expression);
|
||||
if (value === undefined || value === null) {
|
||||
if (!missing.includes(expression)) missing.push(expression);
|
||||
|
||||
Reference in new issue
Block a user