Invental/Review lessons/Trust boundaries the server must hold

Trust boundaries the server must hold

Lazy regexes on untrusted input

Any parser that runs on inbound email, webhooks or uploads takes attacker-controlled input: a lazy regex over “open tag … close tag” can go quadratic when the close tag never comes.

01 · Before and afterThe pattern

TypeScript
// before: rescans the remaining input for every unclosed "<style"
text = html.replace(/<style[\s\S]*?<\/style>/gi, " ");
// after: one pass, bounded lookahead
for (let i = 0; i < html.length; ) {
  const lt = html.indexOf("<", i);
  if (lt === -1) { out += html.slice(i); break; }
  out += html.slice(i, lt);
  i = skipTag(html, lt, /* max tag length */ 2048);  // jumps past a real </style>, never backtracks
}

02 · Run it on your appThe check

Does any regex in your app run on raw HTML or text from outside users, and have you timed it on 1 MB of unclosed tags?

If the honest answer is no or not sure, this is worth a look before it reaches production.

Want this checked on your code?

Tell us what you are building and what worries you. We match a senior reviewer to your code and confirm scope before anything starts.

— Get in touch
hi@invental.co ↗
— Or
— What to include

A link or short description of the code or system, what you want checked, your stack, and when you need the answer. No repository access is needed until scope is agreed.

— Or leave a note
We reply within one business day.