Skip to content

Commit cf3f0ff

Browse files
committed
only allow calls to api endpoints
1 parent f7bddd8 commit cf3f0ff

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

pkg/cmd/api/api.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"io"
89
"net/http"
10+
"strings"
911

1012
"github.com/MakeNowJust/heredoc/v2"
1113
"github.com/OctopusDeploy/cli/pkg/apiclient"
@@ -38,6 +40,10 @@ func NewCmdAPI(f factory.Factory) *cobra.Command {
3840
}
3941

4042
func apiRun(cmd *cobra.Command, f factory.Factory, path string) error {
43+
if err := validateAPIPath(path); err != nil {
44+
return err
45+
}
46+
4147
client, err := f.GetSystemClient(apiclient.NewRequester(cmd))
4248
if err != nil {
4349
return err
@@ -73,3 +79,11 @@ func apiRun(cmd *cobra.Command, f factory.Factory, path string) error {
7379

7480
return nil
7581
}
82+
83+
func validateAPIPath(path string) error {
84+
trimmed := strings.TrimLeft(path, "/")
85+
if !strings.HasPrefix(trimmed, "api") {
86+
return fmt.Errorf("the api command only supports paths prefixed with /api (e.g. /api/spaces)")
87+
}
88+
return nil
89+
}

pkg/cmd/api/api_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ func TestApiCommand(t *testing.T) {
9191
assert.Equal(t, "OK", stdOut.String())
9292
}},
9393

94+
{"rejects path not prefixed with /api", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
95+
defer api.Close()
96+
rootCmd.SetArgs([]string{"api", "/some/other/path"})
97+
_, err := rootCmd.ExecuteC()
98+
assert.Error(t, err)
99+
assert.Contains(t, err.Error(), "only supports paths prefixed with /api")
100+
}},
101+
94102
{"requires an argument", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) {
95103
defer api.Close()
96104
rootCmd.SetArgs([]string{"api"})

0 commit comments

Comments
 (0)