Skip to content

Commit

Permalink
Merge pull request #72 from SciCatProject/hide-token-in-error-msg
Browse files Browse the repository at this point in the history
Strip tokens from exception messages
  • Loading branch information
jl-wynen authored Mar 15, 2023
2 parents 05ebd2b + 267ece9 commit 3ec7d97
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
9 changes: 6 additions & 3 deletions docs/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ Bugfixes

* Removed ``Dataset.instrument_id`` for derived datasets.

Documentation
~~~~~~~~~~~~~
v23.03.1 (2023-03-15)
---------------------

Security
~~~~~~~~

* Document how file transfers interact with source folders.
* Remove user token from error messages.

v23.01.1 (2023-01-20)
---------------------
Expand Down
18 changes: 16 additions & 2 deletions src/scitacean/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ def _send_to_scicat(
params = {"access_token": token}
headers = {"Authorization": "Bearer {}".format(token)}
else:
token = ""
params = {}
headers = {}

Expand All @@ -619,8 +620,14 @@ def _send_to_scicat(
verify=True,
)
except Exception as exc:
# Remove concrete request function call from backtrace.
raise type(exc)(exc.args) from None
# Remove concrete request function call from backtrace to hide the token.
# Also modify the error message to strip out the token.
# It shows up, e.g. in urllib3.exceptions.NewConnectionError.
# This turns the exception args into strings.
# But we have little use of more structured errors, so that should be fine.
raise type(exc)(
tuple(_strip_token(arg, token) for arg in exc.args)
) from None

def _call_endpoint(
self,
Expand Down Expand Up @@ -656,6 +663,13 @@ def _url_concat(a: str, b: str) -> str:
return a + b


def _strip_token(error: Any, token: str) -> str:
error = str(error)
error = re.sub(r"token=[\w\-./]+", "token=<HIDDEN>", error)
error = error.replace(token, "<HIDDEN>")
return error


def _make_orig_datablock(
fields: Dict[str, Any], strict_validation: bool
) -> model.OrigDatablock:
Expand Down
14 changes: 14 additions & 0 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,17 @@ def test_cannot_pickle_client_credentials_login(request, scicat_access, scicat_b
)
with pytest.raises(TypeError):
pickle.dumps(client)


def test_connection_error_does_not_contain_token():
client = Client.from_token(
url="https://not-actually-a_server",
token="the token/which_must-be.kept secret", # noqa: S106
)
try:
client.get_dataset("does not exist")
assert False, "There must be an exception" # noqa: B011
except Exception as exc:
assert "the token/which_must-be.kept secret" not in str(exc)
for arg in exc.args:
assert "the token/which_must-be.kept secret" not in str(arg)

0 comments on commit 3ec7d97

Please sign in to comment.