Skip to content

Swift: Query for regular expression injection #13660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 18, 2023
Merged

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Jul 4, 2023

Adds a query for regular expression injection, that is, constructing a regular expression from unsanitized user input. This is fairly straightforward as the vulnerability is a simple injection issue and we already have a regular expressions library that identifies points where regular expressions are evaluated. It turns out we also want to identify points where regular expressions are constructed in advance of evaluation, so as to get clean and reliable alerts, so we do that as well.

There are a handful of test results that are false positives or not detected. We have follow-up issues to address all of these, and in the case of the false positives I do not believe they will be noisy in real world projects before we address them.

TODO:

  • team review
  • doc review
  • DCA run

@geoffw0 geoffw0 added the Swift label Jul 4, 2023
@geoffw0 geoffw0 requested a review from a team as a code owner July 4, 2023 12:23
@github-actions
Copy link
Contributor

github-actions bot commented Jul 4, 2023

QHelp previews:

swift/ql/src/queries/Security/CWE-730/RegexInjection.qhelp

Regular expression injection

Constructing a regular expression with unsanitized user input is dangerous, since a malicious user may be able to modify the meaning of the expression. They may be able to cause unexpected program behaviour, or perform a denial-of-service attack. For example, they may provide a regular expression fragment that takes exponential time to evaluate in the worst case.

Recommendation

Before embedding user input into a regular expression, use a sanitization function such as NSRegularExpression::escapedPattern(for:) to escape meta-characters that have special meaning.

Example

The following examples construct regular expressions from user input without sanitizing it first:

func processRemoteInput(remoteInput: String) {
  ...

  // BAD: Unsanitized user input is used to construct a regular expression
  let regex1 = try Regex(remoteInput)

  // BAD: Unsanitized user input is used to construct a regular expression
  let regexStr = "abc|\(remoteInput)"
  let regex2 = try NSRegularExpression(pattern: regexStr)

  ...
}

If user input is used to construct a regular expression it should be sanitized first. This ensures that the user cannot insert characters that have special meanings in regular expressions.

func processRemoteInput(remoteInput: String) {
  ...

  // GOOD: Regular expression is not derived from user input
  let regex1 = try Regex(myRegex)

  // GOOD: User input is sanitized before being used to construct a regular expression
  let escapedInput = NSRegularExpression.escapedPattern(for: remoteInput)
  let regexStr = "abc|\(escapedInput)"
  let regex2 = try NSRegularExpression(pattern: regexStr)

  ...
}

References

@geoffw0
Copy link
Contributor Author

geoffw0 commented Jul 10, 2023

DCA run LGTM.

@geoffw0 geoffw0 added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label Jul 13, 2023
mattpollard
mattpollard previously approved these changes Jul 17, 2023
Co-authored-by: Matt Pollard <mattpollard@users.noreply.github.com>
@geoffw0
Copy link
Contributor Author

geoffw0 commented Jul 17, 2023

Thanks for the review @mattpollard. I've accepted both of your suggestions.

@rdmarsh2 I did your recommended change as well, are you happy to approve this now?

@geoffw0 geoffw0 merged commit 1deacf4 into github:main Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. Swift
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants