Skip to content

Commit 922d39a

Browse files
committed
Validate start URL flag values
1 parent 58c81c9 commit 922d39a

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

cmd/browser_pools.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func (c BrowserPoolsCmd) Create(ctx context.Context, in BrowserPoolsCreateInput)
9696
if in.Output != "" && in.Output != "json" {
9797
return fmt.Errorf("unsupported --output value: use 'json'")
9898
}
99+
if err := validateStartURLFlag(in.StartURL); err != nil {
100+
return err
101+
}
99102

100103
params := kernel.BrowserPoolNewParams{
101104
Size: in.Size,
@@ -234,6 +237,9 @@ func (c BrowserPoolsCmd) Update(ctx context.Context, in BrowserPoolsUpdateInput)
234237
if in.Output != "" && in.Output != "json" {
235238
return fmt.Errorf("unsupported --output value: use 'json'")
236239
}
240+
if err := validateStartURLFlag(in.StartURL); err != nil {
241+
return err
242+
}
237243
if in.StartURL != "" && in.ClearStartURL {
238244
return fmt.Errorf("cannot specify both --start-url and --clear-start-url")
239245
}
@@ -692,6 +698,13 @@ func buildProfileParam(profileID, profileName string, saveChanges BoolFlag) (*ke
692698
return &profile, nil
693699
}
694700

701+
func validateStartURLFlag(startURL string) error {
702+
if strings.HasPrefix(startURL, "-") {
703+
return fmt.Errorf("--start-url requires a URL value")
704+
}
705+
return nil
706+
}
707+
695708
func buildExtensionsParam(extensions []string) []kernel.BrowserExtensionParam {
696709
if len(extensions) == 0 {
697710
return nil

cmd/browser_pools_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestBrowserPoolsUpdate_RejectsStartURLFlagToken(t *testing.T) {
12+
err := BrowserPoolsCmd{}.Update(context.Background(), BrowserPoolsUpdateInput{
13+
IDOrName: "pool123",
14+
StartURL: "--discard-all-idle",
15+
})
16+
17+
require.Error(t, err)
18+
assert.Contains(t, err.Error(), "--start-url requires a URL value")
19+
}

cmd/browsers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ func (b BrowsersCmd) Create(ctx context.Context, in BrowsersCreateInput) error {
347347
if in.Output != "" && in.Output != "json" {
348348
return fmt.Errorf("unsupported --output value: use 'json'")
349349
}
350+
if err := validateStartURLFlag(in.StartURL); err != nil {
351+
return err
352+
}
350353

351354
if in.Output != "json" {
352355
pterm.Info.Println("Creating browser session...")

cmd/browsers_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,23 @@ func TestBrowsersCreate_WithStartURL(t *testing.T) {
16501650
assert.Equal(t, "https://example.com", captured.StartURL.Value)
16511651
}
16521652

1653+
func TestBrowsersCreate_RejectsStartURLFlagToken(t *testing.T) {
1654+
called := false
1655+
fake := &FakeBrowsersService{NewFunc: func(ctx context.Context, body kernel.BrowserNewParams, opts ...option.RequestOption) (*kernel.BrowserNewResponse, error) {
1656+
called = true
1657+
return &kernel.BrowserNewResponse{}, nil
1658+
}}
1659+
b := BrowsersCmd{browsers: fake}
1660+
1661+
err := b.Create(context.Background(), BrowsersCreateInput{
1662+
StartURL: "--headless",
1663+
})
1664+
1665+
require.Error(t, err)
1666+
assert.Contains(t, err.Error(), "--start-url requires a URL value")
1667+
assert.False(t, called)
1668+
}
1669+
16531670
func TestBrowsersCreate_WithInvalidViewport(t *testing.T) {
16541671
setupStdoutCapture(t)
16551672
fake := &FakeBrowsersService{}

0 commit comments

Comments
 (0)