@@ -98,6 +98,55 @@ def test_content_within_100k_is_scanned(self):
9898 assert result is not None
9999
100100
101+ class TestRealPatternMatching :
102+ """Verify the actual DLP regexes match the credential formats they target.
103+
104+ Strings are constructed from fragments joined at runtime so no complete
105+ credential pattern appears as a literal in this source file — which would
106+ trigger node9's own DLP hook when writing or committing this file.
107+ """
108+
109+ def test_aws_key_in_content_is_detected (self ):
110+ # AWS Access Key ID: AKIA + 16 uppercase alphanumeric chars
111+ # Split to avoid a complete match in source text
112+ prefix = "AKI" + "A" # "AKIA" in source is never complete
113+ suffix = "X" * 8 + "0" * 8 # 16-char body, clearly synthetic
114+ fake_key = prefix + suffix
115+ result = dlp_scan ("config.txt" , f"aws_key={ fake_key } " )
116+ assert result is not None
117+ assert "AWS" in result
118+
119+ def test_github_token_in_content_is_detected (self ):
120+ # GitHub token: ghp_ + 36 alphanumeric
121+ fake_token = "gh" + "p_" + "A" * 36
122+ result = dlp_scan ("env.txt" , f"GH_TOKEN={ fake_token } " )
123+ assert result is not None
124+ assert "GitHub" in result
125+
126+ def test_pem_key_in_content_is_detected (self ):
127+ # PEM private key header — split across fragments so it never appears whole
128+ pem_begin = "-----BEGIN " + "RSA "
129+ pem_end = "PRIVATE KEY-----"
130+ result = dlp_scan ("key.txt" , pem_begin + pem_end )
131+ assert result is not None
132+ assert "Private Key" in result
133+
134+ def test_clean_content_with_normal_path_passes (self ):
135+ result = dlp_scan ("output.txt" , "def hello():\n return 42\n " )
136+ assert result is None
137+
138+ def test_sensitive_path_detected_regardless_of_content (self ):
139+ result = dlp_scan ("/home/user/.ssh/" + "id_rsa" , "normal content" )
140+ assert result is not None
141+
142+ def test_both_path_and_content_triggers (self ):
143+ # Both path and content are bad — path check runs first, result is non-None
144+ prefix = "AKI" + "A"
145+ fake_key = prefix + "X" * 8 + "0" * 8
146+ result = dlp_scan ("/home/user/.ssh/" + "id_rsa" , fake_key )
147+ assert result is not None
148+
149+
101150class TestSensitivePathDetection :
102151 """Test sensitive file path blocking — no credential content needed."""
103152
0 commit comments