Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Validate cookie name #278

Merged
merged 2 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"os"
"regexp"
Expand Down Expand Up @@ -200,6 +201,7 @@ func (o *Options) Validate() error {
}

msgs = parseSignatureKey(o, msgs)
msgs = validateCookieName(o, msgs)

if len(msgs) != 0 {
return fmt.Errorf("Invalid configuration:\n %s",
Expand Down Expand Up @@ -261,6 +263,14 @@ func parseSignatureKey(o *Options, msgs []string) []string {
return msgs
}

func validateCookieName(o *Options, msgs []string) []string {
cookie := &http.Cookie{Name: o.CookieName}
if cookie.String() == "" {
return append(msgs, fmt.Sprintf("invalid cookie name: %q", o.CookieName))
}
return msgs
}

func addPadding(secret string) string {
padding := len(secret) % 4
switch padding {
Expand Down
15 changes: 15 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto"
"fmt"
"net/url"
"strings"
"testing"
Expand Down Expand Up @@ -216,3 +217,17 @@ func TestValidateSignatureKeyUnsupportedAlgorithm(t *testing.T) {
assert.Equal(t, err.Error(), "Invalid configuration:\n"+
" unsupported signature hash algorithm: "+o.SignatureKey)
}

func TestValidateCookie(t *testing.T) {
o := testOptions()
o.CookieName = "_valid_cookie_name"
assert.Equal(t, nil, o.Validate())
}

func TestValidateCookieBadName(t *testing.T) {
o := testOptions()
o.CookieName = "_bad_cookie_name{}"
err := o.Validate()
assert.Equal(t, err.Error(), "Invalid configuration:\n"+
fmt.Sprintf(" invalid cookie name: %q", o.CookieName))
}