Skip to content

Commit 583b6ea

Browse files
committed
Guard attachment paths against nil work-package links
validateAttachment and upload dereferenced WorkPackageDto.Links without a nil check, so a server payload lacking _links, or a work package re-fetched after a custom action that dropped the addAttachment link, panicked the CLI. Convert() was already hardened for nil Links; make both attachment paths agree by returning a handled error instead.
1 parent 60a279f commit 583b6ea

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package work_packages
2+
3+
import (
4+
"testing"
5+
6+
"github.com/opf/openproject-cli/dtos"
7+
)
8+
9+
// A work package whose payload has no _links object must not panic the
10+
// attachment validation; it should be reported as not accepting attachments.
11+
func TestValidateAttachmentNilLinks(t *testing.T) {
12+
err := validateAttachment(&dtos.WorkPackageDto{Links: nil}, "irrelevant")
13+
if err == nil {
14+
t.Fatal("validateAttachment(nil Links) = nil, want error")
15+
}
16+
}
17+
18+
// upload must self-guard: the work package may be re-fetched after a custom
19+
// action, dropping the addAttachment link that validateAttachment saw earlier.
20+
func TestUploadNilAddAttachment(t *testing.T) {
21+
cases := map[string]*dtos.WorkPackageDto{
22+
"nil links": {Links: nil},
23+
"nil add attachment": {Links: &dtos.WorkPackageLinksDto{}},
24+
}
25+
for name, dto := range cases {
26+
t.Run(name, func(t *testing.T) {
27+
if err := upload(dto, "irrelevant"); err == nil {
28+
t.Errorf("upload(%s) = nil, want error", name)
29+
}
30+
})
31+
}
32+
}

components/resources/work_packages/update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ func executePatch(workPackage, patchDto *dtos.WorkPackageDto, updateString strin
143143
}
144144

145145
func validateAttachment(workPackage *dtos.WorkPackageDto, path string) error {
146+
if workPackage.Links == nil {
147+
return fmt.Errorf("this work package does not accept attachments (missing permission?)")
148+
}
146149
if workPackage.Links.PrepareAttachment != nil {
147150
return fmt.Errorf("uploads to fog storages are currently not supported")
148151
}

components/resources/work_packages/upload.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
// upload assumes validateAttachment already confirmed the work package
1717
// accepts direct uploads (addAttachment link present, no fog storage).
1818
func upload(dto *dtos.WorkPackageDto, path string) error {
19+
if dto.Links == nil || dto.Links.AddAttachment == nil {
20+
return fmt.Errorf("work package no longer accepts attachments")
21+
}
1922
printer.Info(fmt.Sprintf("Uploading %s to work package ...", printer.Yellow(filepath.Base(path))))
2023
link := dto.Links.AddAttachment
2124
reader, contentType, err := bodyReader(path)

0 commit comments

Comments
 (0)