Skip to content

fix(webhook): render placeholders when a webhook policy has no targets - #1095

Open
Somilg11 wants to merge 1 commit into
goharbor:mainfrom
Somilg11:fix/webhook-list-empty-targets
Open

Somilg11 wants to merge 1 commit into
goharbor:mainfrom
Somilg11:fix/webhook-list-empty-targets

Conversation

@Somilg11

@Somilg11 Somilg11 commented Sep 5, 2026

Copy link
Copy Markdown

Description

harbor webhook list panics with an index-out-of-range error whenever Harbor returns a webhook policy with an empty targets array. The list view indexed webhook.Targets[0] unconditionally, but targets is optional in the generated WebhookPolicy model, so a policy without targets crashes the command instead of
rendering.

cmd/harbor/root/webhook/edit.go:146 already guards len(Targets) > 0, so the list view was the only unguarded access left.

Type of Change

Please select the relevant type.

  • Bug fix
  • New feature
  • Refactor
  • Documentation update
  • Chore / maintenance

Changes

  • Extract row construction from ListWebhooks into a testable webhookRows helper.
  • Fall back to a -- placeholder for Endpoint URL, Notify Type and Payload Format when the first target is missing, nil, or has empty fields.
  • Skip nil policies instead of dereferencing them.
  • Add unit tests covering: policy with a target, policy with no targets, nil target in the slice, target with empty fields, nil policy, and cell/column arity.

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 targets is optional in the generated WebhookPolicy model — which is why edit.go already guards it. I reproduced the response shape with a small stub server returning "targets": [] for GET /api/v2.0/projects/demo/webhook/policies, serving two
policies: broken-hook (no targets) and healthy-hook (a normal target), so a single run exercises both the crash case and the unaffected case.

Before (main) — harbor webhook list demo panics at pkg/views/webhook/list/view.go:48:

panic before fix

After (this branch) — same stub, same command. broken-hook renders with --
placeholders; healthy-hook renders exactly as before:

rendered table after fix

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

unit tests passing

Full suite passes: go build ./..., go vet ./pkg/views/webhook/..., go test ./....

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>
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.

[bug]: webhook list panics when a policy has no targets

1 participant