Skip to content

fix(objectstore): guard credential secret against unsafe args[0] indexing and reject extra args - #602

Open
karthik120710 wants to merge 1 commit into
civo:masterfrom
karthik120710:issue#601
Open

fix(objectstore): guard credential secret against unsafe args[0] indexing and reject extra args#602
karthik120710 wants to merge 1 commit into
civo:masterfrom
karthik120710:issue#601

Conversation

@karthik120710

Copy link
Copy Markdown

Description

close #601

objectStoreCredentialSecretCmd resolved its access key with:

var key string
if accessKey != "" {
    key = accessKey
} else if args[0] != "" {
    key = args[0]
}

args[0] was read without first checking len(args), and the command had no Args: validator. In the current code this is not reachable at runtime--access-key is already registered as a required flag (MarkFlagRequired), so cobra's flag validation rejects a missing key before Run ever executesbut the code was still fragile: it panics with an index-out-of-range error the moment that required-flag safeguard is removed or the command is reused elsewhere without it, and there was no test coverage protecting the key-resolution logic itself.

Changes

- Extracted the key-resolution logic into a small pure function, resolveAccessKey(accessKey string, args []string) string, which returns the access key: the --access-key flag takes precedence, otherwise the first positional argument is used if present, otherwise "".
- Replaced the unsafe args[0] != "" check with len(args) > 0 before indexing.
- Added Args: cobra.MaximumNArgs(1) to the command so a stray extra positional argument (e.g. civo objectstore credential secret a b) is now rejected with a clear cobra usage error instead of being silently ignored.
- Added objectstore_credential_secret_test.go with table-driven tests covering: no flag/no args, positional-arg-only, flag-only, and flag-takes-precedence-over-positional-arg.

… command argument handling

Signed-off-by: Karthik Rajan <karthikrajanmr@gmail.com>
@karthik120710

Copy link
Copy Markdown
Author

hi @giornetta @hlts2 i have fix the potential bug and added test case to verify my changes can u review my pr.

@giornetta

Copy link
Copy Markdown
Member

hi @giornetta @hlts2 i have fix the potential bug and added test case to verify my changes can u review my pr.

Thanks for the PR! I'll review it as soon as possible 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash: civo objectstore credential secret panics with no arguments

2 participants