CWE-186 – Overly Restrictive Regular Expression

Read Time:1 Minute, 14 Second

Description

A regular expression is overly restrictive, which prevents dangerous values from being detected.

This weakness is not about regular expression complexity. Rather, it is about a regular expression that does not match all values that are intended. Consider the use of a regexp to identify acceptable values or to spot unwanted terms. An overly restrictive regexp misses some potentially security-relevant values leading to either false positives *or* false negatives, depending on how the regexp is being used within the code. Consider the expression /[0-8]/ where the intention was /[0-9]/. This expression is not “complex” but the value “9” is not matched when maybe the programmer planned to check for it.

Modes of Introduction:

– Implementation

 

 

Related Weaknesses

CWE-185
CWE-184
CWE-183

 

Consequences

Access Control: Bypass Protection Mechanism

 

Potential Mitigations

Phase: Implementation

Description: 

Regular expressions can become error prone when defining a complex language even for those experienced in writing grammars. Determine if several smaller regular expressions simplify one large regular expression. Also, subject your regular expression to thorough testing techniques such as equivalence partitioning, boundary value analysis, and robustness. After testing and a reasonable confidence level is achieved, a regular expression may not be foolproof. If an exploit is allowed to slip through, then record the exploit and refactor your regular expression.

CVE References

  • CVE-2005-1604
    • MIE. “.php.ns” bypasses “.php$” regexp but is still parsed as PHP by Apache. (manipulates an equivalence property under Apache)

CWE-185 – Incorrect Regular Expression

Read Time:1 Minute, 49 Second

Description

The software specifies a regular expression in a way that causes data to be improperly matched or compared.

When the regular expression is used in protection mechanisms such as filtering or validation, this may allow an attacker to bypass the intended restrictions on the incoming data.

Modes of Introduction:

– Implementation

 

 

Related Weaknesses

CWE-697
CWE-187
CWE-182

 

Consequences

Other: Unexpected State, Varies by Context

When the regular expression is not correctly specified, data might have a different format or type than the rest of the program expects, producing resultant weaknesses or errors.

Access Control: Bypass Protection Mechanism

In PHP, regular expression checks can sometimes be bypassed with a null byte, leading to any number of weaknesses.

 

Potential Mitigations

Phase: Architecture and Design

Description: 

Regular expressions can become error prone when defining a complex language even for those experienced in writing grammars. Determine if several smaller regular expressions simplify one large regular expression. Also, subject the regular expression to thorough testing techniques such as equivalence partitioning, boundary value analysis, and robustness. After testing and a reasonable confidence level is achieved, a regular expression may not be foolproof. If an exploit is allowed to slip through, then record the exploit and refactor the regular expression.

CVE References

  • CVE-2002-2109
    • Regexp isn’t “anchored” to the beginning or end, which allows spoofed values that have trusted values as substrings.
  • CVE-2005-1949
    • Regexp for IP address isn’t anchored at the end, allowing appending of shell metacharacters.
  • CVE-2001-1072
    • Bypass access restrictions via multiple leading slash, which causes a regular expression to fail.
  • CVE-2002-1527
    • chain: Malformed input generates a regular expression error that leads to information exposure.
  • CVE-2005-1061
    • Certain strings are later used in a regexp, leading to a resultant crash.
  • CVE-2005-2169
    • MFV. Regular expression intended to protect against directory traversal reduces “…/…//” to “../”.
  • CVE-2005-0603
    • Malformed regexp syntax leads to information exposure in error message.
  • CVE-2005-1820
    • Code injection due to improper quoting of regular expression.

CWE-184 – Incomplete List of Disallowed Inputs

Read Time:2 Minute, 27 Second

Description

The product implements a protection mechanism that relies on a list of inputs (or properties of inputs) that are not allowed by policy or otherwise require other action to neutralize before additional processing takes place, but the list is incomplete, leading to resultant weaknesses.

Developers often try to protect their products against malicious input by performing tests against inputs that are known to be bad, such as special characters that can invoke new commands. However, such lists often only account for the most well-known bad inputs. Attackers may be able to find other malicious inputs that were not expected by the developer, allowing them to bypass the intended protection mechanism.

Modes of Introduction:

– Implementation

 

 

Related Weaknesses

CWE-693
CWE-1023
CWE-79
CWE-78
CWE-434
CWE-98

 

Consequences

Access Control: Bypass Protection Mechanism

 

Potential Mitigations

Phase: Implementation

Description: 

Do not rely exclusively on detecting disallowed inputs. There are too many variants to encode a character, especially when different environments are used, so there is a high likelihood of missing some variants. Only use detection of disallowed inputs as a mechanism for detecting suspicious activity. Ensure that you are using other protection mechanisms that only identify “good” input – such as lists of allowed inputs – and ensure that you are properly encoding your outputs.

CVE References

  • CVE-2008-2309
    • product uses a denylist to identify potentially dangerous content, allowing attacker to bypass a warning
  • CVE-2005-2782
    • PHP remote file inclusion in web application that filters “http” and “https” URLs, but not “ftp”.
  • CVE-2004-0542
    • Programming language does not filter certain shell metacharacters in Windows environment.
  • CVE-2004-0595
    • XSS filter doesn’t filter null characters before looking for dangerous tags, which are ignored by web browsers. MIE and validate-before-cleanse.
  • CVE-2005-3287
    • Web-based mail product doesn’t restrict dangerous extensions such as ASPX on a web server, even though others are prohibited.
Read Time:45 Second

Description

The product implements a protection mechanism that relies on a list of inputs (or properties of inputs) that are explicitly allowed by policy because the inputs are assumed to be safe, but the list is too permissive – that is, it allows an input that is unsafe, leading to resultant weaknesses.

Modes of Introduction:

– Implementation

 

 

Related Weaknesses

CWE-697
CWE-434

 

Consequences

Access Control: Bypass Protection Mechanism

 

Potential Mitigations

CVE References

  • CVE-2019-12799
    • chain: bypass of untrusted deserialization issue (CWE-502) by using an assumed-trusted class (CWE-183)
  • CVE-2019-10458
    • sandbox bypass using a method that is on an allowlist
  • CVE-2019-10458
    • CI/CD pipeline feature has unsafe elements in allowlist, allowing bypass of script restrictions
  • CVE-2017-1000095
    • Default allowlist includes unsafe methods, allowing bypass of sandbox