diff --git a/detect_secrets/plugins/npm.py b/detect_secrets/plugins/npm.py new file mode 100644 index 000000000..9d7fcfcc2 --- /dev/null +++ b/detect_secrets/plugins/npm.py @@ -0,0 +1,17 @@ +""" +This plugin searches for NPM tokens +""" +import re + +from detect_secrets.plugins.base import RegexBasedDetector + + +class NpmDetector(RegexBasedDetector): + """Scans for NPM tokens.""" + secret_type = 'NPM tokens' + + denylist = [ + # npmrc authToken + # ref. https://stackoverflow.com/questions/53099434/using-auth-tokens-in-npmrc + re.compile(r'\/\/.+\/:_authToken=.+'), + ] diff --git a/tests/plugins/npm_test.py b/tests/plugins/npm_test.py new file mode 100644 index 000000000..910f4ba55 --- /dev/null +++ b/tests/plugins/npm_test.py @@ -0,0 +1,22 @@ +import pytest + +from detect_secrets.plugins.npm import NpmDetector + + +class TestNpmDetector: + + @pytest.mark.parametrize( + 'payload, should_flag', + [ + ('//registry.npmjs.org/:_authToken=xxxxxxxxxxxxxxxxxxxx', True), + ('//registry.npmjs.org:_authToken=xxxxxxxxxxxxxxxxxxxx', False), + ('registry.npmjs.org/:_authToken=xxxxxxxxxxxxxxxxxxxx', False), + ('///:_authToken=xxxxxxxxxxxxxxxxxxxx', False), + ('_authToken=xxxxxxxxxxxxxxxxxxxx', False), + ('foo', False), + ], + ) + def test_analyze(self, payload, should_flag): + logic = NpmDetector() + output = logic.analyze_line(filename='mock_filename', line=payload) + assert len(output) == int(should_flag)