Skip to content

Commit

Permalink
fix: Crash on some invalid URLs in schema CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
Stranger6667 committed Feb 9, 2020
1 parent a18648f commit 1418a20
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -12,6 +12,7 @@ Fixed
- Crash on invalid regular expressions in ``method``, ``endpoint`` and ``tag`` CLI options. `#403`_
- Crash on non latin-1 encodable value in ``auth`` CLI option. `#404`_
- Crash on invalid value in ``header`` CLI options. `#405`_
- Crash on some invalid URLs in ``schema`` CLI option. `#406`_

`0.24.1`_ - 2020-02-08
----------------------
Expand Down Expand Up @@ -694,6 +695,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

.. _#406: https://github.com/kiwicom/schemathesis/issues/406
.. _#405: https://github.com/kiwicom/schemathesis/issues/405
.. _#404: https://github.com/kiwicom/schemathesis/issues/404
.. _#403: https://github.com/kiwicom/schemathesis/issues/403
Expand Down
10 changes: 10 additions & 0 deletions src/schemathesis/cli/callbacks.py
Expand Up @@ -6,6 +6,7 @@

import click
import hypothesis
from requests import PreparedRequest, RequestException

from .. import utils

Expand All @@ -16,9 +17,18 @@ def validate_schema(ctx: click.core.Context, param: click.core.Parameter, raw_va
raise click.UsageError("Invalid SCHEMA, must be a valid URL or file path.")
if "base_url" not in ctx.params:
raise click.UsageError('Missing argument, "--base-url" is required for SCHEMA specified by file.')
else:
_validate_url(raw_value)
return raw_value


def _validate_url(value: str) -> None:
try:
PreparedRequest().prepare_url(value, {}) # type: ignore
except RequestException:
raise click.UsageError("Invalid SCHEMA, must be a valid URL or file path.")


def validate_base_url(ctx: click.core.Context, param: click.core.Parameter, raw_value: str) -> str:
if raw_value and not urlparse(raw_value).netloc:
raise click.UsageError("Invalid base URL")
Expand Down
1 change: 1 addition & 0 deletions test/cli/test_callbacks.py
Expand Up @@ -12,6 +12,7 @@

@given(value=st.text().filter(lambda x: "//" not in x))
@example("0" * 1000)
@example("//test")
def test_validate_schema(value):
with pytest.raises(click.UsageError):
callbacks.validate_schema(SimpleNamespace(params={}), None, value)
Expand Down

0 comments on commit 1418a20

Please sign in to comment.