Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
f77264b7
Commit
f77264b7
authored
Jul 05, 2016
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve basicauth password comparison
Thanks to @jaredfolkins for the feedback
parent
fdb6d64f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
5 deletions
+16
-5
caddyhttp/basicauth/basicauth.go
caddyhttp/basicauth/basicauth.go
+16
-5
No files found.
caddyhttp/basicauth/basicauth.go
View file @
f77264b7
// Package basicauth implements HTTP Basic Authentication.
// Package basicauth implements HTTP Basic Authentication for Caddy.
//
// This is useful for simple protections on a website, like requiring
// a password to access an admin interface. This package assumes a
// fairly small threat model.
package
basicauth
package
basicauth
import
(
import
(
"bufio"
"bufio"
"crypto/sha1"
"crypto/subtle"
"crypto/subtle"
"fmt"
"fmt"
"io"
"io"
...
@@ -29,7 +34,6 @@ type BasicAuth struct {
...
@@ -29,7 +34,6 @@ type BasicAuth struct {
// ServeHTTP implements the httpserver.Handler interface.
// ServeHTTP implements the httpserver.Handler interface.
func
(
a
BasicAuth
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
func
(
a
BasicAuth
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
var
hasAuth
bool
var
hasAuth
bool
var
isAuthenticated
bool
var
isAuthenticated
bool
...
@@ -47,7 +51,6 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
...
@@ -47,7 +51,6 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
if
!
ok
||
if
!
ok
||
username
!=
rule
.
Username
||
username
!=
rule
.
Username
||
!
rule
.
Password
(
password
)
{
!
rule
.
Password
(
password
)
{
//subtle.ConstantTimeCompare([]byte(password), []byte(rule.Password)) != 1 {
continue
continue
}
}
...
@@ -140,9 +143,17 @@ func parseHtpasswd(pm map[string]PasswordMatcher, r io.Reader) error {
...
@@ -140,9 +143,17 @@ func parseHtpasswd(pm map[string]PasswordMatcher, r io.Reader) error {
}
}
// PlainMatcher returns a PasswordMatcher that does a constant-time
// PlainMatcher returns a PasswordMatcher that does a constant-time
// byte
-wise comparison
.
// byte
comparison against the password passw
.
func
PlainMatcher
(
passw
string
)
PasswordMatcher
{
func
PlainMatcher
(
passw
string
)
PasswordMatcher
{
// compare hashes of equal length instead of actual password
// to avoid leaking password length
passwHash
:=
sha1
.
New
()
passwHash
.
Write
([]
byte
(
passw
))
passwSum
:=
passwHash
.
Sum
(
nil
)
return
func
(
pw
string
)
bool
{
return
func
(
pw
string
)
bool
{
return
subtle
.
ConstantTimeCompare
([]
byte
(
pw
),
[]
byte
(
passw
))
==
1
pwHash
:=
sha1
.
New
()
pwHash
.
Write
([]
byte
(
pw
))
pwSum
:=
pwHash
.
Sum
(
nil
)
return
subtle
.
ConstantTimeCompare
([]
byte
(
pwSum
),
[]
byte
(
passwSum
))
==
1
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment