Skip to content

Commit

Permalink
add header check for ClientOption (#1174)
Browse files Browse the repository at this point in the history
Signed-off-by: yxxhero <[email protected]>
  • Loading branch information
yxxhero authored and gaius-qi committed Jun 28, 2023
1 parent 9666b89 commit 7167c01
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
4 changes: 1 addition & 3 deletions client/config/deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ type SupernodesValue struct {
// String implements the pflag.Value interface.
func (sv *SupernodesValue) String() string {
var result []string
for _, v := range sv.Nodes {
result = append(result, v)
}
result = append(result, sv.Nodes...)
return strings.Join(result, ",")
}

Expand Down
23 changes: 23 additions & 0 deletions client/config/dfget.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func (cfg *ClientOption) Validate() error {
return errors.Wrapf(dferrors.ErrInvalidArgument, "output: %v", err)
}

if err := cfg.checkHeader(); err != nil {
return errors.Wrapf(dferrors.ErrInvalidHeader, "output: %v", err)
}

return nil
}

Expand Down Expand Up @@ -198,6 +202,25 @@ func (cfg *ClientOption) String() string {
return string(js)
}

// checkHeader is for checking the header format
func (cfg *ClientOption) checkHeader() error {
if len(cfg.Header) == 0 {
return nil
}

for _, header := range cfg.Header {
if !strings.Contains(header, ":") {
return fmt.Errorf("header format error: %v", header)
}
idx := strings.Index(header, ":")
if len(strings.TrimSpace(header[:idx])) == 0 || len(strings.TrimSpace(header[idx+1:])) == 0 {
return fmt.Errorf("header format error: %v", header)
}
}

return nil
}

// This function must be called after checkURL
func (cfg *ClientOption) checkOutput() error {
if !filepath.IsAbs(cfg.Output) {
Expand Down
25 changes: 25 additions & 0 deletions client/config/dfget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,28 @@ func TestMkdirAll(t *testing.T) {
})
}
}

func TestCheckHeader(t *testing.T) {
cfg := NewDfgetConfig()

// test empty header
testifyassert.Nil(t, cfg.checkHeader())

// test normal headers
cfg.Header = []string{
"a:b",
"1:1",
"1:a",
"a:1",
}
testifyassert.Nil(t, cfg.checkHeader())

// test error headers
cfg.Header = []string{
":",
"a",
" :a",
"a: ",
}
testifyassert.NotNil(t, cfg.checkHeader())
}
1 change: 1 addition & 0 deletions internal/dferrors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
// common and framework errors
var (
ErrInvalidArgument = errors.New("invalid argument")
ErrInvalidHeader = errors.New("invalid Header")
ErrDataNotFound = errors.New("data not found")
ErrEmptyValue = errors.New("empty value")
ErrConvertFailed = errors.New("convert failed")
Expand Down

0 comments on commit 7167c01

Please sign in to comment.