Skip to content

Commit

Permalink
filemode validation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperBuker authored and a3s7p committed Oct 6, 2022
1 parent 403013b commit f48dcbf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
30 changes: 4 additions & 26 deletions api/socket/filemode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,18 @@ package socket
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"regexp"
"strconv"
)

var ErrUnsupportedBase = errors.New("unsuported base")
var ErrInvalidNumber = errors.New("value doesn't correspond with base")
var ErrInvalidPermissions = errors.New("invalid file permissions")

// Validate number corresponds with base
func validateBase(s string, base uint32) error {
// Only bases up to 10 are supported, this might chante in the future.
if base == 0 || base > 10 {
return ErrUnsupportedBase
}

pattern := fmt.Sprintf("^[0-%v]{3}$", base-1)

if match, err := regexp.MatchString(pattern, s); err != nil {
return err // Invalid pattern
} else if !match {
return ErrInvalidNumber
}

return nil
}

type FileMode fs.FileMode

// MarshalJSON implements json.Marshaler.
func (fm FileMode) MarshalJSON() ([]byte, error) {

if fm > 511 {
if fm > 0777 {
// Detect wrong permissions
return nil, ErrInvalidPermissions
}
Expand Down Expand Up @@ -69,10 +47,10 @@ func (fm *FileMode) UnmarshalJSON(data []byte) (err error) {

if err = json.Unmarshal(data, &value); err != nil {
// probably it's an valid json
} else if err = validateBase(value, 8); err != nil {
} else if len(value) != 3 {
err = ErrInvalidPermissions // Validate permissions length
} else if intVal, err = strconv.ParseUint(value, 8, 9); err != nil {
err = ErrInvalidPermissions // Validate permissions format
} else if intVal, err = strconv.ParseUint(value, 8, 32); err != nil {
err = ErrInvalidPermissions // This should never fail
} else {
*fm = FileMode(uint32(intVal))
}
Expand Down
33 changes: 17 additions & 16 deletions api/socket/filemode_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package socket

import (
"strconv"
"testing"
)

/**
func TestBaseConversion(t *testing.T) {
result := baseConversion(10, 10, 8)
if result != uint32(12) {
t.Fatal("Invalid result")
}
result = baseConversion(20, 8, 10)
if result != uint32(16) {
t.Fatal("Invalid result")
func TestValidateBase(t *testing.T) {
i, err := strconv.ParseUint("777", 8, 9)
if err != nil {
t.Fatal(err)
} else if i != 0777 {
t.Fatal("Value wrongly parsed")
}
}

func TestValidateBase(t *testing.T) {
err := validateBase(0, 11)
_, err = strconv.ParseUint("002", 2, 9)
if err == nil {
t.Fatal("Must have failed")
}

err = validateBase(2, 2)
i, err = strconv.ParseUint("003", 4, 9)
if err != nil {
t.Fatal(err)
} else if i != 3 {
t.Fatal("Value wrongly parsed")
}

_, err = strconv.ParseUint("1000", 8, 9)
if err == nil {
t.Fatal("Must have failed")
}

err = validateBase(3, 4)
_, err = strconv.ParseUint("1000", 8, 10)
if err != nil {
t.Fatal(err)
}
}
**/

func TestFileModeUnmarshal(t *testing.T) {
var fm FileMode
Expand Down

0 comments on commit f48dcbf

Please sign in to comment.