Skip to content

Incorrect Regular Expression

Description

An incorrectly-bounded regex was passed to `validates_format_of` or `validate... format =>...`. Ruby regex behavior is multiline by default and lines should be terminated by `\A` for beginning of line and `\Z` for end of line, respectively.

Examples

Insecure Code

ruby
validates :username, format: { with: /(?=.*[a-zA-Z])[a-zA-Z0-9]+/ }

Secure Code

ruby
validates :username, format: { with: /\A(?=.*[a-zA-Z])[a-zA-Z0-9]+\z/ }

Remediation

Anchor the regex from start to end of the string using `\A` and `\z`.

Rule Details

FieldValue
IDCODE-0548
CategoryInjection
SeverityMEDIUM
CWECWE-185
ConfidenceHIGH
ImpactMEDIUM
LikelihoodMEDIUM
ExploitabilityMODERATE
Tagsregex, validation
OWASPA5:2017-Broken Access Control, A01:2021-Broken Access Control

References