-
-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import re | ||
import sys | ||
from typing import Sequence | ||
|
||
# secret token is defined in https://datatracker.ietf.org/doc/html/rfc8959 as: | ||
# | ||
# secret-token-URI = secret-token-scheme ":" token | ||
# secret-token-scheme = "secret-token" | ||
# token = 1*pchar | ||
# | ||
# pchar is defined in https://www.rfc-editor.org/rfc/rfc3986#section-3.3 as: | ||
# | ||
# pchar = unreserved / pct-encoded / sub-delims / ":" / "@" | ||
SECRET_TOKEN_RE = re.compile( | ||
'secret-token:(' | ||
r"[A-Za-z0-9\-._~!$&'()*+,;=:@]" # unreserved / sub-delims / ":" / "@" | ||
'|%[0-9A-Fa-f]{2}' # pct-encoded | ||
')+', | ||
) | ||
|
||
|
||
def main(argv: Sequence[str] | None = None) -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('filenames', nargs='*', help='Filenames to check') | ||
args = parser.parse_args(argv) | ||
|
||
found = False | ||
for filename in args.filenames: | ||
with open(filename) as f: | ||
if SECRET_TOKEN_RE.match(f.read()): | ||
found = True | ||
print(f'secret-token found: {filename}', file=sys.stderr) | ||
|
||
return int(found) | ||
|
||
|
||
if __name__ == '__main__': | ||
raise SystemExit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from pre_commit_hooks.detect_secret_token import main | ||
|
||
|
||
@pytest.mark.parametrize( | ||
('input', 'expected'), | ||
( | ||
pytest.param( | ||
'There is no secret here', | ||
0, | ||
id='no secret-token', | ||
), | ||
pytest.param( | ||
'There is no secret here ☃', | ||
0, | ||
id='no secret-token unicode', | ||
), | ||
pytest.param( | ||
'Read about using "secret-token:" in RFC 8959', | ||
0, | ||
id='has secret-token prefix only', | ||
), | ||
pytest.param( | ||
'secret-token:E92FB7EB-D882-47A4-A265-A0B6135DC842%20foo', | ||
1, | ||
id='has secret-token', | ||
), | ||
), | ||
) | ||
def test_main(input, expected, tmpdir): | ||
path = tmpdir.join('file.txt') | ||
path.write(input) | ||
assert main([str(path)]) == expected |