|
| 1 | +import pytest |
| 2 | + |
| 3 | +from detect_secrets.plugins.gitlab_token import GitLabTokenDetector |
| 4 | + |
| 5 | + |
| 6 | +class TestGitLabTokenDetector: |
| 7 | + @pytest.mark.parametrize( |
| 8 | + 'payload, should_flag', |
| 9 | + [ |
| 10 | + ( |
| 11 | + # valid PAT prefix and token length |
| 12 | + 'glpat-hellOworld380_testin', |
| 13 | + True, |
| 14 | + ), |
| 15 | + ( |
| 16 | + # spaces are not part of the token |
| 17 | + 'glpat-hellOWorld380 testin', |
| 18 | + False, |
| 19 | + ), |
| 20 | + ( |
| 21 | + # invalid separator (underscore VS dash) |
| 22 | + 'glpat_hellOworld380_testin', |
| 23 | + False, |
| 24 | + ), |
| 25 | + ( |
| 26 | + # valid different prefix and token length |
| 27 | + 'gldt-HwllOuhfw-wu0rlD_yep', |
| 28 | + True, |
| 29 | + ), |
| 30 | + ( |
| 31 | + # token < 20 chars should be too short |
| 32 | + 'gldt-seems_too000Sshorty', |
| 33 | + False, |
| 34 | + ), |
| 35 | + ( |
| 36 | + # invalid prefix, but valid token length |
| 37 | + 'foo-hello-world80_testin', |
| 38 | + False, |
| 39 | + ), |
| 40 | + ( |
| 41 | + # token length may vary depending on the impl., but <= 50 chars should be fine |
| 42 | + 'glsoat-PREfix_helloworld380_testin_pretty_long_token_long', |
| 43 | + True, |
| 44 | + ), |
| 45 | + ( |
| 46 | + # token > 50 chars is too long |
| 47 | + 'glsoat-PREfix_helloworld380_testin_pretty_long_token_long_', |
| 48 | + False, |
| 49 | + ), |
| 50 | + ( |
| 51 | + # GitLab is not GitHub |
| 52 | + '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', |
| 53 | + False, |
| 54 | + ), |
| 55 | + ], |
| 56 | + ) |
| 57 | + def test_base_token_format(self, payload, should_flag): |
| 58 | + logic = GitLabTokenDetector() |
| 59 | + output = logic.analyze_line(filename='mock_filename', line=payload) |
| 60 | + assert len(output) == int(should_flag) |
| 61 | + |
| 62 | + @pytest.mark.parametrize( |
| 63 | + 'payload, should_flag', |
| 64 | + [ |
| 65 | + ('GR1348941PREfix_helloworld380', True), |
| 66 | + ('GR1348941PREfix_helloworld380_testin_pretty_long_token_long', True), |
| 67 | + ('GR1348941PREfix_helloworld380_testin_pretty_long_token_long_', False), # too long |
| 68 | + ('GR1348941helloWord0', False), # too short |
| 69 | + ], |
| 70 | + ) |
| 71 | + def test_runner_registration_token(self, payload, should_flag): |
| 72 | + logic = GitLabTokenDetector() |
| 73 | + output = logic.analyze_line(filename='mock_filename', line=payload) |
| 74 | + assert len(output) == int(should_flag) |
| 75 | + |
| 76 | + @pytest.mark.parametrize( |
| 77 | + 'payload, should_flag', |
| 78 | + [ |
| 79 | + ('glcbt-helloworld380_testin', True), |
| 80 | + ], |
| 81 | + ) |
| 82 | + def test_cicd_token(self, payload, should_flag): |
| 83 | + logic = GitLabTokenDetector() |
| 84 | + output = logic.analyze_line(filename='mock_filename', line=payload) |
| 85 | + assert len(output) == int(should_flag) |
| 86 | + |
| 87 | + @pytest.mark.parametrize( |
| 88 | + 'payload, should_flag', |
| 89 | + [ |
| 90 | + ('glimt-my-tokens_are-correctAB38', True), |
| 91 | + ('glimt-my-tokens_are-correctAB', False), # too short |
| 92 | + ('glimt-my-tokens_are-correctAB38_280', False), # too long |
| 93 | + ], |
| 94 | + ) |
| 95 | + def test_incoming_mail_token(self, payload, should_flag): |
| 96 | + logic = GitLabTokenDetector() |
| 97 | + output = logic.analyze_line(filename='mock_filename', line=payload) |
| 98 | + assert len(output) == int(should_flag) |
| 99 | + |
| 100 | + @pytest.mark.parametrize( |
| 101 | + 'payload, should_flag', |
| 102 | + [ |
| 103 | + ('glptt-Need5_T00-be-exactly-40-chars--ELse_fail', True), |
| 104 | + ('glptt-Need5_T00-be-exactly-40-chars--ELse_failing', False), # too long |
| 105 | + ('glptt-hellOworld380_testin', False), # too short |
| 106 | + ], |
| 107 | + ) |
| 108 | + def test_trigger_token(self, payload, should_flag): |
| 109 | + logic = GitLabTokenDetector() |
| 110 | + output = logic.analyze_line(filename='mock_filename', line=payload) |
| 111 | + assert len(output) == int(should_flag) |
| 112 | + |
| 113 | + @pytest.mark.parametrize( |
| 114 | + 'payload, should_flag', |
| 115 | + [ |
| 116 | + ('glagent-Need5_T00-bee-longer-than-50_chars-or-else-failING', True), |
| 117 | + ('glagent-Need5_T00-bee-longer-than-50_chars-or-else-failING-still_OK', True), |
| 118 | + (('glagent-' + 'X' * 1025), False), # 2 long |
| 119 | + ('glagent-hellOworld380_testin', False), # len 20 is too short |
| 120 | + ], |
| 121 | + ) |
| 122 | + def test_agent_token(self, payload, should_flag): |
| 123 | + logic = GitLabTokenDetector() |
| 124 | + output = logic.analyze_line(filename='mock_filename', line=payload) |
| 125 | + assert len(output) == int(should_flag) |
| 126 | + |
| 127 | + @pytest.mark.parametrize( |
| 128 | + 'payload, should_flag', |
| 129 | + [ |
| 130 | + ('gloas-checking_Length-Is-_exactly_64--checking_Length-Is-_exactly_64--', True), |
| 131 | + ('gloas-checking_Length-Is-checking_Length-Is-', False), # too short |
| 132 | + ('gloas-checking_Length-Is-_exactly_64--Xchecking_Length-Is-_longer_longer', False), |
| 133 | + ], |
| 134 | + ) |
| 135 | + def test_oauth_application_secret(self, payload, should_flag): |
| 136 | + logic = GitLabTokenDetector() |
| 137 | + output = logic.analyze_line(filename='mock_filename', line=payload) |
| 138 | + assert len(output) == int(should_flag) |
0 commit comments