Skip to content

Commit c0ebf89

Browse files
authored
Merge pull request #2 from SenseUnit/strict_pass_len
Strict mode
2 parents 8a37a56 + c9169bb commit c0ebf89

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Usage of /usr/local/bin/basic_hmac_auth:
4545
hex-encoded HMAC secret value
4646
-secret-file string
4747
file containing single line with hex-encoded secret
48+
-strict
49+
require shortest possible, invariant form of encoding (default true)
4850
-version
4951
show program version and exit
5052
```

cmd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222

2323
var (
2424
bufferSize = flag.Int("buffer-size", 0, "initial buffer size for stream parsing")
25+
strict = flag.Bool("strict", true, "require shortest possible, invariant form of encoding")
2526
hexSecret = flag.String("secret", "", "hex-encoded HMAC secret value")
2627
hexSecretFile = flag.String("secret-file", "", "file containing single line with hex-encoded secret")
2728
showVersion = flag.Bool("version", false, "show program version and exit")
@@ -80,6 +81,7 @@ func run() int {
8081
err = (&handler.BasicHMACAuthHandler{
8182
Secret: secret,
8283
BufferSize: *bufferSize,
84+
Strict: *strict,
8385
}).Run(os.Stdin, os.Stdout)
8486
if err != nil {
8587
log.Printf("auth handler terminated with error: %v", err)

handler/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
type BasicHMACAuthHandler struct {
1919
Secret []byte
2020
BufferSize int
21+
Strict bool
2122
}
2223

2324
func (a *BasicHMACAuthHandler) Run(input io.Reader, output io.Writer) error {
@@ -28,7 +29,7 @@ func (a *BasicHMACAuthHandler) Run(input io.Reader, output io.Writer) error {
2829
rd := bufio.NewReaderSize(input, bufSize)
2930
scanner := proto.NewElasticLineScanner(rd, '\n')
3031

31-
verifier := hmac.NewVerifier(a.Secret)
32+
verifier := hmac.NewVerifier(a.Secret, a.Strict)
3233

3334
emitter := proto.NewResponseEmitter(output)
3435

hmac/hmac.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
const (
1313
HMACSignaturePrefix = "dumbproxy grant token v1"
1414
HMACExpireSize = 8
15-
passwordBufferSize = HMACExpireSize + 64 // for worst case if 512-bit hash is used for some reason
1615
)
1716

1817
var hmacSignaturePrefix = []byte(HMACSignaturePrefix)
@@ -22,13 +21,21 @@ func NewHasher(secret []byte) hash.Hash {
2221
}
2322

2423
type Verifier struct {
25-
mac hash.Hash
26-
buf []byte
24+
mac hash.Hash
25+
buf []byte
26+
dec *base64.Encoding
27+
strict bool
2728
}
2829

29-
func NewVerifier(secret []byte) *Verifier {
30+
func NewVerifier(secret []byte, strict bool) *Verifier {
31+
dec := base64.RawURLEncoding
32+
if strict {
33+
dec = dec.Strict()
34+
}
3035
return &Verifier{
31-
mac: hmac.New(sha256.New, secret),
36+
mac: hmac.New(sha256.New, secret),
37+
strict: strict,
38+
dec: dec,
3239
}
3340
}
3441

@@ -39,10 +46,14 @@ func (v *Verifier) ensureBufferSize(size int) {
3946
}
4047

4148
func (v *Verifier) VerifyLoginAndPassword(login, password []byte) bool {
42-
v.ensureBufferSize(base64.RawURLEncoding.DecodedLen(len(password)))
49+
if v.strict && len(password) != v.dec.EncodedLen(HMACExpireSize+v.mac.Size()) {
50+
return false
51+
}
52+
53+
v.ensureBufferSize(v.dec.DecodedLen(len(password)))
4354
buf := v.buf
44-
n, err := base64.RawURLEncoding.Decode(buf, password)
45-
if err != nil {
55+
n, err := v.dec.Decode(buf, password)
56+
if v.strict && err != nil {
4657
return false
4758
}
4859
buf = buf[:n]

0 commit comments

Comments
 (0)