refactor: add validation to vault key concatenation - #1189
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1189 +/- ##
==========================================
+ Coverage 49.57% 50.96% +1.39%
==========================================
Files 146 146
Lines 13455 13603 +148
==========================================
+ Hits 6670 6933 +263
+ Misses 6207 6076 -131
- Partials 578 594 +16 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR hardens Vault KV v2 secret-path handling by validating and safely joining path-based keys under the configured Vault namespace, and expands tests to cover the new validation and common Vault interactions.
Changes:
- Added path-key validation and a scoped path-join helper to prevent path traversal/namespace escape when building Vault secret paths.
- Refactored Vault read/write/delete logic to use the scoped path builder for both path-based and field-based secret storage.
- Replaced prior mocking with HTTP-level tests using
httptestto validate request paths and payload shapes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pkg/secrets/vault/secrets.go | Adds key/path validation + scoped path builder; updates Vault secret operations to use validated paths. |
| pkg/secrets/vault/secrets_test.go | Adds extensive tests for key validation, scoped path building, and Vault API interactions via httptest. |
Suppressed comments (1)
pkg/secrets/vault/secrets.go:297
- SetObject's docstring says the key must contain "/" to specify a path-based secret, but the implementation accepts slash-free keys and will write to {basePath}/{key}. Consider enforcing the documented contract to prevent accidental writes to unexpected locations.
// SetObject stores a map of string values at a path-based secret.
// The key must contain "/" to specify the path: {basePath}/{key}.
func (c *Client) SetObject(key string, data map[string]string) error {
secretPath, err := buildScopedSecretPath(c.path, key)
if err != nil {
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
ab7a6c2 to
d805439
Compare
2135681 to
c710ff9
Compare
c710ff9 to
3424eac
Compare
This pull request introduces robust validation and normalization for path-based secret keys in the Vault secrets client, ensuring that secret operations are safe from path traversal and namespace escape issues. The changes also refactor the code to consistently use these validation and path-building utilities across all secret management methods.
Key improvements include:
Path Validation and Security
validatePathKeyfunction to strictly validate path-based secret keys, preventing empty keys, unsupported characters, absolute paths, path traversal, and non-normalized paths. This helps prevent security vulnerabilities related to improper key usage.buildScopedSecretPathfunction to safely join and normalize secret paths under the base namespace, ensuring secrets cannot escape their intended scope.Refactoring Secret Operations
GetKeyValue,SetKeyValue,DeleteKeyValue,GetObject, andSetObject) to use the new path validation and building utilities, replacing previous ad-hoc path concatenation logic. This centralizes and standardizes path handling throughout the codebase.Error Handling Improvements
ErrInvalidPathKeyand improved error messages for invalid key scenarios, making issues easier to diagnose and debug.Code Organization
setPathBasedKeyValue,setFieldKeyValue), improving readability and maintainability.These changes significantly increase the safety and reliability of secret storage operations by enforcing strict key validation and consistent path handling.