-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CSRF protection to the Hypervisor API (#1604)
* Add CSRF protection to the Hypervisor API * Param for disabling CSRF protection
- Loading branch information
Showing
4 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Package visor pkg/visor/hypervisor.go | ||
package visor | ||
|
||
import ( | ||
"time" | ||
|
||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
|
||
"github.com/skycoin/skycoin/src/cipher" | ||
) | ||
|
||
const ( | ||
// CSRFHeaderName is the name of the CSRF header | ||
CSRFHeaderName = "X-CSRF-Token" | ||
|
||
// CSRFMaxAge is the lifetime of a CSRF token in seconds | ||
CSRFMaxAge = time.Second * 30 | ||
|
||
csrfSecretLength = 64 | ||
|
||
csrfNonceLength = 64 | ||
) | ||
|
||
var ( | ||
// ErrCSRFInvalid is returned when the the CSRF token is in invalid format | ||
ErrCSRFInvalid = errors.New("invalid CSRF token") | ||
// ErrCSRFExpired is returned when the csrf token has expired | ||
ErrCSRFExpired = errors.New("csrf token expired") | ||
) | ||
|
||
var csrfSecretKey []byte | ||
|
||
func init() { | ||
csrfSecretKey = cipher.RandByte(csrfSecretLength) | ||
} | ||
|
||
// CSRFToken csrf token | ||
type CSRFToken struct { | ||
Nonce []byte | ||
ExpiresAt time.Time | ||
} | ||
|
||
// newCSRFToken generates a new CSRF Token | ||
func newCSRFToken() (string, error) { | ||
token := &CSRFToken{ | ||
Nonce: cipher.RandByte(csrfNonceLength), | ||
ExpiresAt: time.Now().Add(CSRFMaxAge), | ||
} | ||
|
||
tokenJSON, err := json.Marshal(token) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
h := hmac.New(sha256.New, csrfSecretKey) | ||
_, err = h.Write([]byte(tokenJSON)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
sig := base64.RawURLEncoding.EncodeToString(h.Sum(nil)) | ||
|
||
signingString := base64.RawURLEncoding.EncodeToString(tokenJSON) | ||
|
||
return strings.Join([]string{signingString, sig}, "."), nil | ||
} | ||
|
||
// verifyCSRFToken checks validity of the given token | ||
func verifyCSRFToken(headerToken string) error { | ||
tokenParts := strings.Split(headerToken, ".") | ||
if len(tokenParts) != 2 { | ||
return ErrCSRFInvalid | ||
} | ||
|
||
signingString, err := base64.RawURLEncoding.DecodeString(tokenParts[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h := hmac.New(sha256.New, csrfSecretKey) | ||
_, err = h.Write([]byte(signingString)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sig := base64.RawURLEncoding.EncodeToString(h.Sum(nil)) | ||
|
||
if sig != tokenParts[1] { | ||
return ErrCSRFInvalid | ||
} | ||
|
||
var csrfToken CSRFToken | ||
err = json.Unmarshal(signingString, &csrfToken) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if time.Now().After(csrfToken.ExpiresAt) { | ||
return ErrCSRFExpired | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters