← All articlesWeb security

How to limit string length before regex and verify rejection order

Validation with a regular expression should begin with an explicit string length limit. But a condition in the code is not enough: you need to confirm that the server actually checks length first and never calls regex for an overlength value. We will use an entirely fictional form with a label field: define the length unit, server-side limit and allowed format, then verify the event order using our own synthetic strings. The result is a testable contract for admission to regex validation, not a promise that the expression itself is safe.

Define the field contract first

In our fictional form, label is the name of an imaginary tag. Under the invented contract, it is a string of 1 to 20 Unicode code points containing only Latin letters, Cyrillic letters and spaces. We denote the upper limit by L: here, L = 20. These are conditions for this teaching example, not a universal recommendation for names.

Document the length unit alongside the limit. Do not rely on the name of a built-in length property: separately verify, using your own strings containing non-ASCII characters, that the chosen server-side counter actually counts code points. Prepare values with known contents and expected lengths, including a character outside the Basic Multilingual Plane. Checking the counter and checking the allowed format are separate tasks.

Place the limit before the regular expression

The server-side sequence in this example is sequential: check that the value is a string, determine its length, reject values outside the defined interval, and only then call regex. The regular expression must validate the entire value; if the chosen API provides full matching, use it. A partial match is insufficient to satisfy the contract.

Count length and validate format using the same string representation that will subsequently be used. In our example, both checks receive the same decoded label value. It is not decoded again later in the processing chain. Otherwise, the validated version and the version actually used will no longer conform to the same contract.

Make the server-side order observable

Browser validation may mirror the form rules, but the server must enforce them independently. The trust boundary here lies between receiving a value and admitting that value to regex validation. The following are original proposed defensive tests for your own test server, not an official verification procedure.

Add a stage log and a counter for calls to the regex wrapper in the test validator. Separately record when the value is passed to the next handler. Reset observations before each case. These events reveal the actual sequence; response time and a successful HTTP status alone do not confirm it. Internal observations should be available to the server-side test, not returned to the client.

Test values around the boundary

Prepare three synthetic strings of your own in the allowed format, with lengths of 19, 20 and 21 code points. For example, build them by repeating an allowed letter. Send each directly to your own test server, bypassing browser validation. The values of length 19 and 20 serve as positive controls: they should pass both stages.

Define acceptance criteria before running the tests. For valid values, expect the events ‘length allowed → regex called → accepted’, one regex call and one handoff to the next handler. For the string of length 21, expect rejection on length, zero regex calls and zero handoffs. If the interface displays a length error but the regex counter increases, the required order has not been achieved.

Distinguish length rejection from format rejection

Now prepare a string of an allowed length containing a character prohibited by the fictional contract, such as ‘Метка!’. It should pass the length limit, invoke regex and be rejected for its format. The next handler must not receive this value. This verifies that format actually contributes to the decision.

Then create a string of 21 code points containing the same prohibited character. The expected result is rejection on length without a single regex call or any handoff. Together, these cases distinguish the two rejection reasons and confirm that the length limit takes priority. Check server-side events and whether data was passed onward, not just the error text or response code.

Fix violations where they occur and keep conclusions narrow

If an overlength string reaches regex, move the length check before the call and stop processing on rejection. If the next handler receives a rejected value, fix the branching logic. If the counter uses a different unit, bring the implementation into line with the contract. Then repeat the proposed checks. Return a clear rejection reason to the client without a call stack or internal trace.

State a successful result narrowly: in the tested cases, input exceeding the limit did not reach regex. A length limit does not prove that the expression is safe, that excessive backtracking is absent or that processing times are identical. The expression itself needs separate attention: avoid excessive backtracking and, where supported, use a non-backtracking engine or a matching timeout.

What to establish in the team's workflow

The first priority is to agree on the field contract and length unit. The second is to ensure server-side rejection before regex and stop processing rejected data. The third is to retain the proposed boundary and rejection-reason checks as verifiable acceptance criteria. The product owner needs to see the admission conditions and results; the developer needs observations from inside the validator.

This article is limited to admitting a single string to regular expression validation; load testing and other input-processing mechanisms are outside its scope. If you need help checking the implementation, you can start with a free website or web-product audit: define the field contract and check whether the server-side sequence follows it.

Sources and documentation

  1. Input Validation Cheat Sheet
  2. REST Security Cheat Sheet
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free