@@ -71,3 +71,57 @@ func TestRunSecretsProvider(t *testing.T) {
7171 })
7272 }
7373}
74+
75+ func TestRunSecretsProvider_PathTraversal (t * testing.T ) {
76+ t .Parallel ()
77+
78+ // Create a temporary directory structure
79+ tmpDir := t .TempDir ()
80+
81+ // Create a "secrets" subdirectory
82+ secretsDir := filepath .Join (tmpDir , "secrets" )
83+ require .NoError (t , os .Mkdir (secretsDir , 0o755 ))
84+
85+ // Create a legitimate secret inside the secrets directory
86+ secretFile := filepath .Join (secretsDir , "api_key" )
87+ require .NoError (t , os .WriteFile (secretFile , []byte ("SECRET_VALUE" ), 0o644 ))
88+
89+ // Create a sensitive file OUTSIDE the secrets directory (simulating /etc/passwd, etc.)
90+ sensitiveFile := filepath .Join (tmpDir , "sensitive.txt" )
91+ require .NoError (t , os .WriteFile (sensitiveFile , []byte ("SENSITIVE_DATA" ), 0o644 ))
92+
93+ provider := & RunSecretsProvider {
94+ root : secretsDir ,
95+ }
96+
97+ // Test 1: Normal access should work
98+ result := provider .Get (t .Context (), "api_key" )
99+ assert .Equal (t , "SECRET_VALUE" , result , "Normal secret access should work" )
100+
101+ // Test 2: Path traversal with ../ should be blocked
102+ result = provider .Get (t .Context (), "../sensitive.txt" )
103+ assert .Empty (t , result , "Path traversal with ../ should be blocked and return empty string" )
104+
105+ // Test 3: Multiple path traversal levels
106+ result = provider .Get (t .Context (), "../../sensitive.txt" )
107+ assert .Empty (t , result , "Multiple path traversal levels should be blocked" )
108+
109+ // Test 4: Absolute path outside secrets dir should be blocked
110+ result = provider .Get (t .Context (), sensitiveFile )
111+ assert .Empty (t , result , "Absolute path outside secrets dir should be blocked" )
112+
113+ // Test 5: Path with ../ that normalizes to valid path within directory
114+ // This is NOT a vulnerability - it's proper path normalization
115+ subDir := filepath .Join (secretsDir , "subdir" )
116+ require .NoError (t , os .Mkdir (subDir , 0o755 ))
117+ subSecret := filepath .Join (subDir , "subsecret" )
118+ require .NoError (t , os .WriteFile (subSecret , []byte ("SUB_VALUE" ), 0o644 ))
119+
120+ // subdir/../api_key normalizes to api_key, which is valid
121+ result = provider .Get (t .Context (), "subdir/../api_key" )
122+ assert .Equal (t , "SECRET_VALUE" , result , "Path that normalizes to valid location should work" )
123+
124+ // But accessing subsecret directly should work
125+ result = provider .Get (t .Context (), "subdir/subsecret" )
126+ assert .Equal (t , "SUB_VALUE" , result , "Subdirectory access should work" )
127+ }
0 commit comments