mirror of
https://github.com/jimeh/ozu.io.git
synced 2026-02-19 08:06:39 +00:00
Validate UIDs on Lookup
This commit is contained in:
@@ -2,6 +2,7 @@ package shortener
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/jimeh/go-base58"
|
"github.com/jimeh/go-base58"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
|
|
||||||
var urlKeyPrefix = []byte("url:")
|
var urlKeyPrefix = []byte("url:")
|
||||||
var uidKeyPrefix = []byte("uid:")
|
var uidKeyPrefix = []byte("uid:")
|
||||||
|
var errInvalidUID = errors.New("invalid UID")
|
||||||
|
|
||||||
// NewBase58 returns a new *Base58Shortner that uses the given storage.Store.
|
// NewBase58 returns a new *Base58Shortner that uses the given storage.Store.
|
||||||
func NewBase58(store storage.Store) *Base58Shortener {
|
func NewBase58(store storage.Store) *Base58Shortener {
|
||||||
@@ -58,6 +60,11 @@ func (s *Base58Shortener) Shorten(rawURL []byte) (uid []byte, url []byte, err er
|
|||||||
|
|
||||||
// Lookup the URL of a given UID.
|
// Lookup the URL of a given UID.
|
||||||
func (s *Base58Shortener) Lookup(uid []byte) ([]byte, error) {
|
func (s *Base58Shortener) Lookup(uid []byte) ([]byte, error) {
|
||||||
|
_, err := base58.Decode(uid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errInvalidUID
|
||||||
|
}
|
||||||
|
|
||||||
uidKey := s.makeUIDKey(uid)
|
uidKey := s.makeUIDKey(uid)
|
||||||
|
|
||||||
url, err := s.Store.Get(uidKey)
|
url, err := s.Store.Get(uidKey)
|
||||||
|
|||||||
@@ -138,6 +138,16 @@ func (s *Base58ShortenerSuite) TestLookupNonExistant() {
|
|||||||
s.store.AssertExpectations(s.T())
|
s.store.AssertExpectations(s.T())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Base58ShortenerSuite) TestLookupInvalid() {
|
||||||
|
uid := []byte("ig\"; drop table haha")
|
||||||
|
|
||||||
|
rURL, err := s.shortener.Lookup(uid)
|
||||||
|
|
||||||
|
s.EqualError(err, "invalid UID")
|
||||||
|
s.Nil(rURL)
|
||||||
|
s.store.AssertExpectations(s.T())
|
||||||
|
}
|
||||||
|
|
||||||
// Run Suite
|
// Run Suite
|
||||||
|
|
||||||
func TestShortenerSuite(t *testing.T) {
|
func TestShortenerSuite(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user