Conversation
harbor webhook list panicked with an index-out-of-range error whenever Harbor returned a webhook policy with an empty targets array. The list view indexed Targets[0] unconditionally, but targets is optional in the generated WebhookPolicy model. Extract row construction into webhookRows and fall back to a placeholder for the endpoint URL, notify type and payload format when the target is missing, nil or empty. Nil policies are skipped rather than dereferenced. Fixes goharbor#973 Signed-off-by: Somil Gupta <gsomil93@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
harbor webhook listpanics with an index-out-of-range error whenever Harbor returns a webhook policy with an emptytargetsarray. The list view indexedwebhook.Targets[0]unconditionally, buttargetsis optional in the generatedWebhookPolicymodel, so a policy without targets crashes the command instead ofrendering.
cmd/harbor/root/webhook/edit.go:146already guardslen(Targets) > 0, so the list view was the only unguarded access left.Type of Change
Please select the relevant type.
Changes
ListWebhooksinto a testablewebhookRowshelper.--placeholder for Endpoint URL, Notify Type and Payload Format when the first target is missing, nil, or has empty fields.No behaviour change for policies that do have a target — the same values render as before.
Testing
Reproduction
Harbor rejects a webhook policy with no target at create time, but
targetsis optional in the generatedWebhookPolicymodel — which is whyedit.goalready guards it. I reproduced the response shape with a small stub server returning"targets": []forGET /api/v2.0/projects/demo/webhook/policies, serving twopolicies:
broken-hook(no targets) andhealthy-hook(a normal target), so a single run exercises both the crash case and the unaffected case.Before (
main) —harbor webhook list demopanics atpkg/views/webhook/list/view.go:48:After (this branch) — same stub, same command.
broken-hookrenders with--placeholders;
healthy-hookrenders exactly as before:Unit tests
The code-level repro from the issue (
&models.WebhookPolicy{}) is covered as a permanent regression guard:go test ./pkg/views/webhook/... -v -run TestWebhookRows -count=1
Full suite passes:
go build ./...,go vet ./pkg/views/webhook/...,go test ./....