String scalar in input has no pattern defined
Issue ID: graphql-data-input-string-scalar-pattern-needed
Average severity: Critical
Description
A string scalar used in an input position has no pattern constraint defined. While GraphQL ensures that the value is a string, it does not enforce any structural or semantic constraints to it.
For more details, see the GraphQL constraints specification.
Possible exploit scenario
Without a pattern or equivalent structural validation rule, the API accepts any sequence of characters within the allowed length.
String inputs frequently represent structured data, such as email addresses, usernames, or URLs. Without a pattern constraint, these fields may accept:
- Unexpected characters
- Control characters
- Encoded payloads
- Injection primitives
- Invalid structural formats
Attackers could submit malicious input containing special characters or crafted payloads. This increases the risk of:
- SQL or NoSQL injection
- Command injection
- Log injection
- Template injection
- Path traversal
- Downstream parser abuse
- Business logic bypass
Even if injection is ultimately mitigated, lack of structural constraints significantly increases the likelihood of exploitable mistakes and thus the attack surface.
Remediation
Define a strict pattern constraint that matches your requirements for string scalars, especially for structured string inputs. This ensures that only strings matching the set pattern get passed to your API. We recommend that you:
- Define patterns for all structured string types
- Prefer whitelist-style regular expressions (allowed characters) over blacklist rules
- Keep patterns aligned with business requirements
- Avoid overly permissive patterns such as
.*
A well-defined pattern significantly reduces injection risk, improves data integrity, and strengthens API contract clarity. It also enforces structural expectations at the schema boundary and reduces reliance on resolver-level validation.
We recommend that you carefully think what kind of regular expression best matches your needs. Do not simply blindly copy the pattern from the code example.
Remember to include the anchors ^ and $ in your regular expression, otherwise the overall length of the pattern could be considered infinite. If you include the anchors in the regular expression and the pattern only has fixed or constant quantifiers (like {10,64}, for example), you do not have to define the property maxLength separately for the object, as the length is fully constrained by the pattern. However, if the regular expression does not include the anchors or its quantifiers are not fixed (like in ^a.*b$), it can be considered to be just a part of a longer string and the property maxLength is required to constrain the length.
For more information on regular expressions, see the following:
- Language-agnostic information on regular expressions at Base Definitions page on regular expressions
- OWASP Validation Regex Repository
- RegExr, an online tool for building and testing regular expressions
The following are examples of regular expressions for some common elements:
| Element | Examples of regular expressions | Examples with escape |
|---|---|---|
| Alphanumeric string |
|
— |
| Base64‑encoding (for an image) |
|
^data:image\\/(?:gif|png|jpeg|bmp|webp)(?:;charset=utf-8)?;base64,(?:[A-Za-z0-9]|[+/])+={0,2}$
|
| Date and time |
|
|
| Duration |
|
^\\d+:\\d{2}:\\d{2}$
|
| Email address (common format) |
|
^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,5})$
|
| File |
|
|
| IP address |
|
|
| Numbers |
|
|
| Password constraints |
Password that has:
|
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*.!@$%^&(){}[]:;<>,.?/~_+-=|\\]).{10,64}$
|
| Phone number |
International phone number, country code optional: Use libraries instead or regular expressions to validate phone numbers whenever possible. |
^(?:(?:\\(?(?:00|\\+)([1-4]\\d\\d|[1-9]\\d?)\\)?)?[\\-\\.\\ \\\\\\/]?)?((?:\\(?\\d{1,}\\)?[\\-\\.\\ \\\\\\/]?){0,})(?:[\\-\\.\\ \\\\\\/]?(?:#|ext\\.?|extension|x)[\\-\\.\\ \\\\\\/]?(\\d+))?$
|
| URL/URI (with protocol optional) |
|
^(https?:\\/\\/)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)$
|
| UUID |
|
— |