Skip to content

Commit

Permalink
base52: check for invalid chars in Decode()
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis committed Jan 12, 2023
1 parent 71405f3 commit 465f2e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/base52/base52.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func Decode(encoded string) (int64, error) {
}
var id int64
for i := 0; i < len(encoded); i++ {
id = id*int64(base) + int64(strings.IndexByte(space, encoded[i]))
idx := strings.IndexByte(space, encoded[i])
if idx < 0 {
return 0, fmt.Errorf("invalid encoded string: '%s' contains invalid character", encoded)
}
id = id*int64(base) + int64(idx)
}
return id, nil
}
4 changes: 4 additions & 0 deletions pkg/base52/base52_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ func (s *base52Suite) TestDecode(c *C) {
decoded, err := Decode("2TPzw7")
c.Assert(decoded, Equals, int64(1000000000))
c.Assert(err, IsNil)

decoded, err = Decode("../../etc/passwd")
c.Assert(decoded, Equals, int64(0))
c.Assert(err, NotNil)
}

0 comments on commit 465f2e0

Please sign in to comment.