Skip to content

Commit

Permalink
Adds InvalidRegex TaintConfiguration Error type (#743)
Browse files Browse the repository at this point in the history
Summary:
**Pre-submission checklist**
- [x] I've ran the linters locally and fixed lint errors related to the files I modified in this PR. You can install the linters by running `pip install -r requirements-dev.txt && pre-commit install`
- [x] `pre-commit run`

Adds new error type that can handle Re2 compilation failed exceptions. Previously, when a regex compilation failed, the exception wasn't caught and the program terminated abnormally.

Catch the exception, and throw a custom TaintConfiguration Error to expose the underlying reason why the compilation failed and exit in a more user-friendly fashion.

Pull Request resolved: #743

Test Plan:
- taint.config used:
```json
{
  "sources": [
    {
      "name": "CustomUserControlled",
      "comment": "use to annotate user input"
    }
  ],
  "implicit_sources": {
     "literal_strings": [
       {
         "regexp": "\\d{1Z,3}(\\.\\d{1,3}+",
         "kind": "CustomUserControlled",
         "description": "String that looks like an IP address."
       }
     ]
  },
  "sinks": [
    {
      "name": "CodeExecution",
      "comment": "use to annotate execution of python code"
    }
  ],

  "features": [],

  "rules": [
    {
      "name": "Possible RCE:",
      "code": 5001,
      "sources": [ "CustomUserControlled" ],
      "sinks": [ "CodeExecution" ],
      "message_format": "User specified data may reach a code execution sink"
    }
  ]
}
```

Before:
<img width="990" alt="before" src="https://github.com/facebook/pyre-check/assets/8947010/2d98a7e3-7650-439b-84e5-e300f758bb47">

After:
<img width="990" alt="after" src="https://github.com/facebook/pyre-check/assets/8947010/5aa82960-eae0-4681-b7e8-949079448042">

Fixes part of: MLH-Fellowship#82
Signed-off-by: Abishek V Ashok <abishekvashok@gmail.com>

- `make test`

Reviewed By: saputkin

Differential Revision: D47589737

Pulled By: arthaud

fbshipit-source-id: eb173e8f20f0aec080abc62ea5c3c2ab21d442e5
  • Loading branch information
abishekvashok authored and facebook-github-bot committed Jul 20, 2023
1 parent 8ba1c37 commit 7dee807
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 24 additions & 4 deletions source/interprocedural_analyses/taint/taintConfiguration.ml
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,10 @@ module Error = struct
| SinkDuplicate of string
| TransformDuplicate of string
| FeatureDuplicate of string
| InvalidRegex of {
regex: string;
reason: string;
}
[@@deriving equal, compare]

type t = {
Expand Down Expand Up @@ -824,6 +828,8 @@ module Error = struct
| SinkDuplicate name -> Format.fprintf formatter "Duplicate entry for sink: `%s`" name
| TransformDuplicate name -> Format.fprintf formatter "Duplicate entry for transform: `%s`" name
| FeatureDuplicate name -> Format.fprintf formatter "Duplicate entry for feature: `%s`" name
| InvalidRegex { regex; reason } ->
Format.fprintf formatter "Invalid regex `%s`: `%s`" regex reason


let code = function
Expand All @@ -847,6 +853,7 @@ module Error = struct
| FeatureDuplicate _ -> 18
| UnsupportedTransform _ -> 19
| TransformDuplicate _ -> 20
| InvalidRegex _ -> 21


let show_kind = Format.asprintf "%a" pp_kind
Expand Down Expand Up @@ -1325,6 +1332,17 @@ let from_json_list source_json_list =
~init:CombinedSourceRules.empty
~f:(parse_string_combine_rule ~path ~allowed_sources)
in
let parse_regex ~path ~location pattern =
try Result.Ok (Re2.create_exn pattern) with
| Re2.Exceptions.Regex_compile_failed error ->
Result.Error
[
Error.create_with_location
~path
~location
~kind:(Error.InvalidRegex { regex = pattern; reason = error });
]
in
let parse_implicit_sinks ~allowed_sinks (path, json) =
match JsonAst.Json.Util.member "implicit_sinks" json with
| { JsonAst.Node.value = `Null; _ } -> Result.Ok empty_implicit_sinks
Expand Down Expand Up @@ -1352,8 +1370,9 @@ let from_json_list source_json_list =
parse_sink_reference ~path ~allowed_sinks sink
|> Result.map_error ~f:(fun error -> [error])
>>= fun sink_kind ->
json_string_member ~path "regexp" json
>>| fun pattern -> { sink_kind; pattern = Re2.create_exn pattern })
json_string_member_with_location ~path "regexp" json
>>= fun (raw_pattern, location) ->
parse_regex ~path ~location raw_pattern >>| fun pattern -> { sink_kind; pattern })
literal_strings
|> Result.combine_errors
|> Result.map_error ~f:List.concat
Expand All @@ -1379,8 +1398,9 @@ let from_json_list source_json_list =
parse_source_reference ~path ~allowed_sources source
|> Result.map_error ~f:(fun error -> [error])
>>= fun source_kind ->
json_string_member ~path "regexp" json
>>| fun pattern -> { source_kind; pattern = Re2.create_exn pattern })
json_string_member_with_location ~path "regexp" json
>>= fun (raw_pattern, location) ->
parse_regex ~path ~location raw_pattern >>| fun pattern -> { source_kind; pattern })
literal_strings
|> Result.combine_errors
|> Result.map_error ~f:List.concat
Expand Down
4 changes: 4 additions & 0 deletions source/interprocedural_analyses/taint/taintConfiguration.mli
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ module Error : sig
| SinkDuplicate of string
| TransformDuplicate of string
| FeatureDuplicate of string
| InvalidRegex of {
regex: string;
reason: string;
}
[@@deriving equal, show]

type t = {
Expand Down

0 comments on commit 7dee807

Please sign in to comment.