Skip to content

Commit

Permalink
fix: Crash on invalid regular expressions in method, endpoint and…
Browse files Browse the repository at this point in the history
… `tag` CLI options
  • Loading branch information
Stranger6667 committed Feb 9, 2020
1 parent 6e2e0a2 commit 637b0d0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Expand Up @@ -6,6 +6,11 @@ Changelog
`Unreleased`_
-------------

Fixed
~~~~~

- Crash on invalid regular expressions in ``method``, ``endpoint`` and ``tag`` CLI options. `#403`_

`0.24.1`_ - 2020-02-08
----------------------

Expand Down Expand Up @@ -687,6 +692,7 @@ Fixed
.. _0.3.0: https://github.com/kiwicom/schemathesis/compare/v0.2.0...v0.3.0
.. _0.2.0: https://github.com/kiwicom/schemathesis/compare/v0.1.0...v0.2.0

.. _#403: https://github.com/kiwicom/schemathesis/issues/403
.. _#400: https://github.com/kiwicom/schemathesis/issues/400
.. _#394: https://github.com/kiwicom/schemathesis/issues/394
.. _#391: https://github.com/kiwicom/schemathesis/issues/391
Expand Down
21 changes: 19 additions & 2 deletions src/schemathesis/cli/__init__.py
Expand Up @@ -77,9 +77,26 @@ def schemathesis(pre_run: Optional[str] = None) -> None:
type=str,
multiple=True,
help=r"Filter schemathesis test by endpoint pattern. Example: users/\d+",
callback=callbacks.validate_regex,
)
@click.option(
"--method",
"-M",
"methods",
type=str,
multiple=True,
help="Filter schemathesis test by HTTP method.",
callback=callbacks.validate_regex,
)
@click.option(
"--tag",
"-T",
"tags",
type=str,
multiple=True,
help="Filter schemathesis test by schema tag pattern.",
callback=callbacks.validate_regex,
)
@click.option("--method", "-M", "methods", type=str, multiple=True, help="Filter schemathesis test by HTTP method.")
@click.option("--tag", "-T", "tags", type=str, multiple=True, help="Filter schemathesis test by schema tag pattern.")
@click.option(
"--workers",
"-w",
Expand Down
11 changes: 11 additions & 0 deletions src/schemathesis/cli/callbacks.py
Expand Up @@ -71,6 +71,17 @@ def validate_headers(
return headers


def validate_regex(ctx: click.core.Context, param: click.core.Parameter, raw_value: Tuple[str, ...]) -> Tuple[str, ...]:
if not raw_value:
return raw_value
for value in raw_value:
try:
re.compile(value)
except re.error as exc:
raise click.BadParameter(f"Invalid regex: {exc.args[0]}")
return raw_value


def convert_verbosity(
ctx: click.core.Context, param: click.core.Parameter, value: Optional[str]
) -> Optional[hypothesis.Verbosity]:
Expand Down
6 changes: 6 additions & 0 deletions test/cli/test_callbacks.py
Expand Up @@ -46,3 +46,9 @@ def test_reraise_format_error():
with pytest.raises(click.BadParameter, match="Should be in KEY:VALUE format. Got: bla"):
with callbacks.reraise_format_error("bla"):
raise ValueError


@pytest.mark.parametrize("value", ("+", "\\", "[",))
def test_validate_regex(value):
with pytest.raises(click.BadParameter, match="Invalid regex: "):
callbacks.validate_regex(None, None, (value,))

0 comments on commit 637b0d0

Please sign in to comment.