Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cyclonedx_py/_internal/utils/secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

from re import compile as re_compile

_URL_AUTH_MATCHER = re_compile(r'(?<=://)[^/@:]+:[^/@]+@')
# In HTTP URLs any userinfo is a secret, it could be a single token without a colon.
# Elsewhere, a single name is not - like in `ssh://git@...`.
_URL_AUTH_MATCHER = re_compile(r'(?i:(?<=http://)|(?<=https://))[^/?#\s]+@'
r'|(?<=://)[^/@:]+:[^/@]+@')
_URL_AUTH_REPLACE = '' # drop auth - in accordance with PEP 610


Expand Down
59 changes: 59 additions & 0 deletions tests/unit/test_utils_secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This file is part of CycloneDX Python
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.

from unittest import TestCase

from ddt import data, ddt, unpack

from cyclonedx_py._internal.utils.secret import redact_auth_from_url


@ddt
class TestRedactAuthFromUrl(TestCase):

@data(
# user and password
('https://user:[email protected]/simple', 'https://example.com/simple'),
('https://user:p@[email protected]/simple', 'https://example.com/simple'),
('ssh://git:[email protected]/foo.git', 'ssh://example.com/foo.git'),
# single token
('https://[email protected]/simple', 'https://example.com/simple'),
('HTTP://[email protected]:8080/simple', 'HTTP://example.com:8080/simple'),
('https://0123456789abcdef:@example.com/simple', 'https://example.com/simple'),
('https://:[email protected]/simple', 'https://example.com/simple'),
('git+https://[email protected]/[email protected]', 'git+https://example.com/[email protected]'),
('foo @ git+https://[email protected]/[email protected]',
'foo @ git+https://example.com/[email protected]'),
('https://[email protected]?a=b@c', 'https://example.com?a=b@c'),
)
@unpack
def test_redacts(self, url: str, expected: str) -> None:
self.assertEqual(expected, redact_auth_from_url(url))

@data(
'https://example.com/simple',
'https://example.com:8080/[email protected]',
'https://[email protected]',
'https://example.com#[email protected]',
'git+https://example.com/[email protected]',
'git+ssh://[email protected]/foo.git',
'[email protected]:foo.git',
'foo @ https://example.com/foo.tar.gz',
'https://example.com [email protected]',
)
def test_keeps(self, url: str) -> None:
self.assertEqual(url, redact_auth_from_url(url))
Loading