-
Notifications
You must be signed in to change notification settings - Fork 42
fix: extend offline secret caching to token-based auth and fix connection check #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,8 +41,12 @@ func WriteToFile(fileName string, dataToWrite []byte, filePerm os.FileMode) erro | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| func ValidateInfisicalAPIConnection() (ok bool) { | ||||||||||||||||||||||||
| _, err := http.Get(fmt.Sprintf("%v/status", config.INFISICAL_URL)) | ||||||||||||||||||||||||
| return err == nil | ||||||||||||||||||||||||
| resp, err := http.Get(fmt.Sprintf("%v/status", config.INFISICAL_URL)) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| defer resp.Body.Close() | ||||||||||||||||||||||||
| return resp.StatusCode >= 200 && resp.StatusCode < 300 | ||||||||||||||||||||||||
|
Comment on lines
+44
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Added |
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| func GetRestyClientWithCustomHeaders() (*resty.Client, error) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -374,6 +374,33 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo | |
| errorToReturn = err | ||
| secretsToReturn = res.Secrets | ||
| } | ||
|
|
||
| // cache secrets on success, fallback to cached secrets on connection/server failure | ||
| if errorToReturn == nil && params.WorkspaceId != "" { | ||
| if backupEncryptionKey, err := GetBackupEncryptionKey(); err == nil { | ||
| WriteBackupSecrets(params.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey, secretsToReturn) | ||
| } | ||
| } else if errorToReturn != nil && params.WorkspaceId != "" { | ||
| // Only fall back to cache for connection errors or server errors (5xx). | ||
| // Do not mask client errors (4xx) like 401/403 which indicate auth issues. | ||
| shouldFallback := true | ||
| var apiErr *api.APIError | ||
| if errors.As(errorToReturn, &apiErr) && apiErr.StatusCode >= 400 && apiErr.StatusCode < 500 { | ||
| shouldFallback = false | ||
| } | ||
|
|
||
| if shouldFallback { | ||
| backupEncryptionKey, _ := GetBackupEncryptionKey() | ||
| if backupEncryptionKey != nil { | ||
| backedUpSecrets, err := ReadBackupSecrets(params.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey) | ||
| if len(backedUpSecrets) > 0 { | ||
| PrintWarning("Unable to fetch the latest secret(s) due to connection error, serving secrets from last successful fetch. For more info, run with --debug") | ||
| secretsToReturn = backedUpSecrets | ||
| errorToReturn = err | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+383
to
+403
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The user-auth path (line 349) only reads from cache when
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch β fixed. The fallback now only triggers on connection errors or server errors (5xx). Client errors (4xx) like 401/403 are no longer masked β they propagate as-is so the user sees an actionable auth failure message instead of stale cached secrets. shouldFallback := true
var apiErr *api.APIError
if errors.As(errorToReturn, &apiErr) && apiErr.StatusCode >= 400 && apiErr.StatusCode < 500 {
shouldFallback = false
} |
||
| } | ||
|
|
||
| return secretsToReturn, errorToReturn | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
INFISICAL_URLconfig.INFISICAL_URLis user-supplied and passed directly tohttp.Get. An attacker who can influence this value (e.g., via a misconfigured environment variable) could use it to probe internal network endpoints β making the CLI an SSRF oracle. Consider validating that the URL is a known-safe external host, or at minimum ensuring the scheme ishttpsand the host is not a private/loopback address. This vector existed before this PR but the change makes it more prominent as the health-check path is now load-bearing for the offline fallback decision.Context Used: Flag SSRF risks (source)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acknowledged β this is a pre-existing pattern (the URL was already used in
http.Getbefore this PR). HardeningINFISICAL_URLvalidation is a good idea but out of scope for this bugfix PR.