Skip to content

Commit

Permalink
fix: correctly locate and skip over esi:include tags with a blank src…
Browse files Browse the repository at this point in the history
… attribute

tags like <esi:include src="" /> cause the parser to
skip over the tag and the following tag
  • Loading branch information
cdloh committed May 1, 2024
1 parent 40674a2 commit 283b0f6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/tagParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class tagParser {
}

return new RegExp(
`<(${tag})(?:\\s*(?:[a-z]+=\\".+?(?<!\\\\)\\"))?[^>]*?(?:\\s*)(/>|>)?`,
`<(${tag})(?:\\s*(?:[a-z]+=\\".*?(?<!\\\\)\\"))?[^>]*?(?:\\s*)(/>|>)?`,
);
}

Expand All @@ -58,7 +58,7 @@ export class tagParser {
return /(?:<(!--esi)|(-->))/;
}
return new RegExp(
`<[\\/]?(${tag})(?:\\s*(?:[a-z]+=\\".+?(?<!\\\\)\\"))?[^>]*?(?:\\s*)(\\s*/>|>)?`,
`<[\\/]?(${tag})(?:\\s*(?:[a-z]+=\\".*?(?<!\\\\)\\"))?[^>]*?(?:\\s*)(\\s*/>|>)?`,
);
}

Expand Down
49 changes: 49 additions & 0 deletions test/esi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1911,3 +1911,52 @@ test("TEST 50: Multiple ESI Args make it all the way through", async () => {
`esi_args1=1&esi_args2=2&esi_args3=3&esi_args4=4`,
);
});

test("TEST 51: Blank ESI source should be skipped over", async () => {
const url = `/esi/test-51`;
routeHandler.add(url, function (req, res) {
res.writeHead(200, esiHead);
res.say("START:");
res.say(`<esi:include src="${url}/fragment_1" />`);
res.say(`<esi:include src="" />`);
res.say(`<esi:include src="${url}/fragment_1" />`);
res.end(`:END`);
});
routeHandler.add(
`${url}/fragment_1`,
function (req, res) {
res.writeHead(200, esiHead);
res.end(`FRAGMENT`);
},
{ count: 2 },
);
const res = await makeRequest(url);
expect(res.ok).toBeTruthy();
expect(checkSurrogate(res)).toBeTruthy();
expect(await res.text()).toEqual(
`START:\nFRAGMENT\n<esi:include src="" />\nFRAGMENT\n:END`,
);
});

test("TEST 52: ESI drops responses with no body", async () => {
const url = `/esi/test-52`;
routeHandler.add(url, function (req, res) {
res.writeHead(200, esiHead);
res.say("START:");
res.say(`<esi:include src="${url}/fragment_1" />`);
res.end(`:END`);
});
routeHandler.add(
`${url}/fragment_1`,
function (req, res) {
res.writeHead(302, { Location: "http://localhost" });
res.end();
},
{ count: 1 },
);
debugger;
const res = await makeRequest(url);
expect(res.ok).toBeTruthy();
expect(checkSurrogate(res)).toBeTruthy();
expect(await res.text()).toEqual(`START:\n\n:END`);
});

0 comments on commit 283b0f6

Please sign in to comment.