turva-llms-txt-validator 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.mjs +232 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turva-llms-txt-validator",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Validate a site's llms.txt structure: eight checks as pass/warn/fail plus two llms.txt v2 discovery checks as information, JSON output. Same checks as the hosted validator at turva.dev/llms-txt-validator.",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
package/src/index.mjs CHANGED
@@ -177,6 +177,224 @@ export function validateLlmsTxt(f) {
177
177
  return checks;
178
178
  }
179
179
 
180
+ // A "<" starts a tag only when a letter, "!", "/" or "?" follows it. Anything else is
181
+ // text, and the NEXT "<" can still start a tag. Measured, not assumed: without this rule
182
+ // "<<style><link rel=describedby ...>" read the link as published while a real parser
183
+ // treats the first "<" as text, opens style, and publishes nothing.
184
+ function startsTag(c) {
185
+ return c !== undefined && (c === "!" || c === "/" || c === "?" || (c >= "a" && c <= "z"));
186
+ }
187
+
188
+ // The end of the tag that starts at lt: the first ">" that is NOT inside a quoted
189
+ // attribute value. A quote opens a value only right after "=", which is what the
190
+ // tokenizer does. indexOf(">") is wrong here: <link data-x="a>b" rel="describedby"> is
191
+ // one tag for a parser and two for indexOf, and the relation was lost.
192
+ function tagEnd(text, lt) {
193
+ let quote = "", afterEq = false;
194
+ for (let j = lt + 1; j < text.length; j++) {
195
+ const c = text[j];
196
+ if (quote) { if (c === quote) quote = ""; continue; }
197
+ if (c === ">") return j;
198
+ if (c === "=") { afterEq = true; continue; }
199
+ if (c === " " || c === "\t" || c === "\n" || c === "\r" || c === "\f") continue;
200
+ if (afterEq && (c === '"' || c === "'")) { quote = c; afterEq = false; continue; }
201
+ afterEq = false;
202
+ }
203
+ return -1;
204
+ }
205
+
206
+ // The head is where a real parser says it ends, not where the text "</head>" happens to
207
+ // appear. Erik's decision 2026-08-24: the two v2 checks describe what the page's HEAD
208
+ // points at, so a link element the parser moves into the body is not one of them. That is
209
+ // the strict reading, and it is what the HTML parsing spec's "in head" insertion mode
210
+ // does: whitespace, comments, doctype and the head-only elements keep the head open;
211
+ // the first text node, the first body-level element and </head>, </body>, </html> or
212
+ // </br> close it; any other end tag in the head is a parse error and is ignored.
213
+ var HEAD_ELEMENTS = ["base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title", "noscript"];
214
+ // Once </head> has been seen the parser is in "after head", and that list is the one above
215
+ // WITHOUT noscript: a noscript there opens the body instead of staying in the head.
216
+ var AFTER_HEAD_ELEMENTS = ["base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title"];
217
+ // Their content is not markup: script, style, title, noscript (a parser with scripting on
218
+ // reads it as raw text) and noframes are raw text or RCDATA, and template content is inert.
219
+ var HEAD_SKIPPED_CONTENT = ["script", "style", "title", "noscript", "noframes", "template"];
220
+
221
+ // The end of a raw text or RCDATA element: the first </name that is followed by optional
222
+ // whitespace, an optional "/" (a parser closes on </script/> too) and a ">". Returns the
223
+ // index after it, or -1 when the element never closes, which means the rest of the
224
+ // document is inside it.
225
+ function skipSpace(lower, j) {
226
+ while (j < lower.length && (lower[j] === " " || lower[j] === "\t" || lower[j] === "\n" || lower[j] === "\r" || lower[j] === "\f")) j++;
227
+ return j;
228
+ }
229
+
230
+ // True when the character ends a tag name: whitespace, "/", ">" or the end of the input.
231
+ function isTagBoundary(c) {
232
+ return c === undefined || c === ">" || c === "/" || c === " " || c === "\t" || c === "\n" || c === "\r" || c === "\f";
233
+ }
234
+
235
+ // The index just after "</name ... >", or -1 when that is not an end tag there.
236
+ function endTagAt(lower, name, at) {
237
+ if (!lower.startsWith("</" + name, at)) return -1;
238
+ let j = skipSpace(lower, at + name.length + 2);
239
+ if (lower[j] === "/") j = skipSpace(lower, j + 1);
240
+ return lower[j] === ">" ? j + 1 : -1;
241
+ }
242
+
243
+ function rawTextEnd(lower, name, from) {
244
+ for (let at = lower.indexOf("</" + name, from); at !== -1; at = lower.indexOf("</" + name, at + name.length + 2)) {
245
+ const end = endTagAt(lower, name, at);
246
+ if (end !== -1) return end;
247
+ }
248
+ return -1;
249
+ }
250
+
251
+ // script is not plain raw text: <!-- puts the tokenizer in the escaped state, a nested
252
+ // <script there puts it in the double escaped state, and in THAT state </script only ends
253
+ // the escape, not the element. Without this the legacy shape
254
+ // <script><!-- ... <script>...</script> ... --></script> ended early and the rest of the
255
+ // script was read as markup, which is the wrong direction. 4 of 240 000 fuzz inputs.
256
+ function scriptEnd(lower, from) {
257
+ let i = from, escaped = false, doubleEscaped = false;
258
+ while (i < lower.length) {
259
+ if (lower.startsWith("<!--", i)) { escaped = true; i += 4; continue; }
260
+ if (escaped && lower.startsWith("-->", i)) { escaped = false; doubleEscaped = false; i += 3; continue; }
261
+ if (escaped && !doubleEscaped && lower.startsWith("<script", i) && isTagBoundary(lower[i + 7])) { doubleEscaped = true; i += 7; continue; }
262
+ if (lower.startsWith("</script", i)) {
263
+ if (doubleEscaped) { doubleEscaped = false; i += 8; continue; }
264
+ const end = endTagAt(lower, "script", i);
265
+ if (end !== -1) return end;
266
+ i += 8;
267
+ continue;
268
+ }
269
+ const next = lower.indexOf("<", i + 1);
270
+ i = next === -1 ? lower.length : next;
271
+ }
272
+ return -1;
273
+ }
274
+
275
+ // The end of a nested template: templates count, so an inner </template> does not close
276
+ // an outer one. Returns the index after the closing tag, or -1 when it never closes.
277
+ function templateEnd(lower, from) {
278
+ let depth = 1;
279
+ // Both searches resume from their own previous hit. Restarting either one from a shared
280
+ // cursor is quadratic: "</templateX" repeated made every round scan to the end of the
281
+ // input again, and 1 MB of it measured 15 686 ms.
282
+ let open = lower.indexOf("<template", from);
283
+ let close = lower.indexOf("</template", from);
284
+ for (;;) {
285
+ if (close === -1) return -1;
286
+ if (open !== -1 && open < close) {
287
+ if (isTagBoundary(lower[open + 9])) depth++;
288
+ open = lower.indexOf("<template", open + 9);
289
+ continue;
290
+ }
291
+ let j = close + 10;
292
+ j = skipSpace(lower, j);
293
+ if (lower[j] === "/") j = skipSpace(lower, j + 1);
294
+ if (lower[j] !== ">") {
295
+ // Not an end tag: for the tokenizer the rest of the name runs to the next ">", and a
296
+ // "<" inside it is part of the name rather than a new tag.
297
+ const bogus = lower.indexOf(">", close + 10);
298
+ if (bogus === -1) return -1;
299
+ close = lower.indexOf("</template", bogus + 1);
300
+ if (open !== -1 && open < bogus) open = lower.indexOf("<template", bogus + 1);
301
+ continue;
302
+ }
303
+ depth--;
304
+ if (depth === 0) return j + 1;
305
+ close = lower.indexOf("</template", j + 1);
306
+ }
307
+ }
308
+
309
+ // One left to right scan by index that returns the head, with comments and the content of
310
+ // the raw text and template elements already removed. Why a scan and not a regex: a
311
+ // character class that reads a tag's attributes is quadratic on input the TARGET site
312
+ // controls (CodeQL js/polynomial-redos, alerts #4 and #5, 2026-08-24), and bounding the
313
+ // class trades that speed bug for a worse correctness bug, because an open tag longer than
314
+ // the bound stops being recognised and the element's own text is then read as markup. A
315
+ // scan has no bound and no backtracking, and it visits every character once.
316
+ //
317
+ // The shapes, all measured against a real HTML parser (parse5) rather than reasoned about.
318
+ // An unterminated <!-- comments out the rest of the document. <!--> and <!---> are EMPTY
319
+ // comments, not unterminated ones, and --!> ends a comment as well. A "<" that no letter,
320
+ // "!", "/" or "?" follows is text. A ">" inside a quoted attribute value does not end a
321
+ // tag. </script/> closes a raw text element as well as </script> does.
322
+ function headOfDocument(html) {
323
+ const text = String(html || "");
324
+ const lower = text.toLowerCase();
325
+ const out = [];
326
+ let i = 0, afterHead = false;
327
+ for (;;) {
328
+ const lt = lower.indexOf("<", i);
329
+ const gap = lt === -1 ? text.slice(i) : text.slice(i, lt);
330
+ if (gap.trim() !== "") break;
331
+ out.push(gap);
332
+ if (lt === -1) break;
333
+ if (lower.startsWith("<!--", lt)) {
334
+ if (lower.startsWith("<!-->", lt)) { i = lt + 5; continue; }
335
+ if (lower.startsWith("<!--->", lt)) { i = lt + 6; continue; }
336
+ const dashes = lower.indexOf("-->", lt + 4);
337
+ const bang = lower.indexOf("--!>", lt + 4);
338
+ if (dashes === -1 && bang === -1) break;
339
+ i = (bang === -1 || (dashes !== -1 && dashes <= bang)) ? dashes + 3 : bang + 4;
340
+ continue;
341
+ }
342
+ if (!startsTag(lower[lt + 1])) break;
343
+ const gt = tagEnd(text, lt);
344
+ if (gt === -1) break;
345
+ if (lower[lt + 1] === "!" || lower[lt + 1] === "?") { i = gt + 1; continue; }
346
+ const head14 = lower.slice(lt, Math.min(lt + 14, gt + 1));
347
+ const endTag = (/^<\/([a-z]+)/.exec(head14) || [])[1];
348
+ if (endTag) {
349
+ // </body>, </html> and </br> start the body. </head> does NOT end the search: after
350
+ // it a parser still puts base, link, meta, script, style, title and template into the
351
+ // HEAD element until real body content starts, and every other end tag in the head is
352
+ // a parse error that is ignored. Measured against parse5: without this, 2 660 of
353
+ // 200 000 fuzz inputs lost a relation the head really carries.
354
+ if (endTag === "body" || endTag === "html" || endTag === "br") break;
355
+ if (endTag === "head") afterHead = true;
356
+ i = gt + 1;
357
+ continue;
358
+ }
359
+ const name = (/^<([a-z]+)(?=[\s/>]|$)/.exec(head14) || [])[1];
360
+ if (!name) break;
361
+ if (name === "html" || name === "head") { i = gt + 1; continue; }
362
+ if (!(afterHead ? AFTER_HEAD_ELEMENTS : HEAD_ELEMENTS).includes(name)) break; // <body> and the first body level element
363
+ if (HEAD_SKIPPED_CONTENT.includes(name)) {
364
+ const end = name === "template" ? templateEnd(lower, gt + 1)
365
+ : name === "script" ? scriptEnd(lower, gt + 1)
366
+ : rawTextEnd(lower, name, gt + 1);
367
+ if (end === -1) break;
368
+ i = end;
369
+ continue;
370
+ }
371
+ out.push(text.slice(lt, gt + 1));
372
+ i = gt + 1;
373
+ }
374
+ return out.join("");
375
+ }
376
+
377
+ // Tags are found by index and not by /<link\b[^>]*>/g, on purpose. That regex is
378
+ // quadratic on input the TARGET site controls: every "<link" with no ">" after it makes
379
+ // the character class scan to the end of the document, and "<link" repeated 16 000 times
380
+ // measured 196 ms where this loop measures under 1 ms. CodeQL reports the same shape as
381
+ // js/polynomial-redos (alerts #4 and #5 on the package repo, 2026-08-24). This scan
382
+ // visits every character once: from each "<" it reads to the next ">" and then continues
383
+ // after it, which is what the regex meant to say.
384
+ function* htmlTags(text) {
385
+ const lower = text.toLowerCase();
386
+ let i = 0;
387
+ for (;;) {
388
+ const open = lower.indexOf("<", i);
389
+ if (open === -1) return;
390
+ if (!startsTag(lower[open + 1])) { i = open + 1; continue; }
391
+ const close = tagEnd(text, open);
392
+ if (close === -1) return;
393
+ yield text.slice(open, close + 1);
394
+ i = close + 1;
395
+ }
396
+ }
397
+
180
398
  // v2 of the llms.txt proposal (August 2026) left the file format alone and added one
181
399
  // thing: a page should say where its markdown version and its llms.txt are, using
182
400
  // rel="alternate" type="text/markdown" and rel="describedby", as HTML link elements or
@@ -185,14 +403,17 @@ export function validateLlmsTxt(f) {
185
403
  // summary line is unchanged, and --strict keeps exiting on warnings only.
186
404
  export function findLinkRelations(html, linkHeader) {
187
405
  const found = { describedby: null, markdown: null };
188
- // Comments are stripped first: a commented-out link element is not published,
189
- // and counting one would report a relation the site does not serve. With no
190
- // </head> the first 64 KB are scanned, body included, so a malformed document
191
- // does not silently report nothing found.
192
- const text = String(html || "").replace(/<!--[\s\S]*?-->/g, "");
193
- const cut = text.toLowerCase().indexOf("</head>");
194
- const head = cut === -1 ? text.slice(0, 65536) : text.slice(0, cut);
195
- for (const tag of head.match(/<link\b[^>]*>/gi) || []) {
406
+ // Only the head, and only what a parser would put there: a commented-out link element,
407
+ // one inside a script or a template, and one the parser moves into the body are all not
408
+ // what these two checks are about, and counting them would report a relation the site
409
+ // does not serve. See headOfDocument above for the rules shape by shape. The 64 KB bound
410
+ // is a cap on work, not a rule: a head longer than that is not a head.
411
+ const head = headOfDocument(html).slice(0, 65536);
412
+ for (const tag of htmlTags(head)) {
413
+ // The name has to END at "link": a real parser reads "<link<link" as ONE tag whose
414
+ // NAME is "link<link", not as a link element, so \b would count a relation the site
415
+ // does not publish. Measured against parse5, 2026-08-24.
416
+ if (!/^<link(?=[\s/>])/i.test(tag)) continue;
196
417
  const rel = ((tag.match(/\brel\s*=\s*["']?([^"'>]+)/i) || [])[1] || "").toLowerCase().trim().split(/\s+/);
197
418
  const type = ((tag.match(/\btype\s*=\s*["']?([^"'>\s]+)/i) || [])[1] || "").toLowerCase();
198
419
  const href = ((tag.match(/\bhref\s*=\s*"([^"]*)"|\bhref\s*=\s*'([^']*)'|\bhref\s*=\s*([^\s"'>]+)/i) || []).slice(1).find((x) => x !== undefined) || "").trim();
@@ -200,7 +421,9 @@ export function findLinkRelations(html, linkHeader) {
200
421
  if (!found.markdown && rel.includes("alternate") && type.startsWith("text/markdown")) found.markdown = href || "(link element without href)";
201
422
  }
202
423
  for (const part of String(linkHeader || "").split(/,(?=\s*<)/)) {
203
- const href = ((part.match(/<([^>]*)>/) || [])[1] || "").trim();
424
+ const lt = part.indexOf("<");
425
+ const gt = lt === -1 ? -1 : part.indexOf(">", lt + 1);
426
+ const href = (gt === -1 ? "" : part.slice(lt + 1, gt)).trim();
204
427
  const rel = ((part.match(/rel\s*=\s*"?([^";,]+)"?/i) || [])[1] || "").toLowerCase().trim().split(/\s+/);
205
428
  const type = ((part.match(/type\s*=\s*"?([^";,]+)"?/i) || [])[1] || "").toLowerCase().trim();
206
429
  if (!found.describedby && rel.includes("describedby")) found.describedby = href || "(Link header without a target)";