Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set "Secure" on Same SIte cookies #1119

Merged
merged 3 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion usersync/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,24 @@ func (cookie *PBSCookie) SetCookieOnResponse(w http.ResponseWriter, setSiteCooki
currSize = len([]byte(httpCookie.String()))
}

uidsCookieStr := httpCookie.String()
var uidsCookieStr string
var sameSiteCookie *http.Cookie
if setSiteCookie {
httpCookie.Secure = true
uidsCookieStr := httpCookie.String()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uidsCookieStr has already been declared and it doesn't need the := operator. The uidsCookieStr = httpCookie.String() assignation happens in both the true and false branches of if statement in line 198, so maybe we can simplify:

196 -     var uidsCookieStr string
    +     uidsCookieStr := httpCookie.String()
197       var sameSiteCookie *http.Cookie
198       if setSiteCookie {
199           httpCookie.Secure = true
200 -         uidsCookieStr = httpCookie.String()
201           uidsCookieStr += SameSiteAttribute
202           sameSiteCookie = &http.Cookie{
203               Name:    SameSiteCookieName,
204               Value:   SameSiteCookieValue,
205               Expires: time.Now().Add(ttl),
206               Path:    "/",
207               Secure:  true,
208           }
209           sameSiteCookieStr := sameSiteCookie.String()
210           sameSiteCookieStr += SameSiteAttribute
211           w.Header().Add("Set-Cookie", sameSiteCookieStr)
212 -     } else {
213 -         uidsCookieStr = httpCookie.String()
214       }
215       w.Header().Add("Set-Cookie", uidsCookieStr)
216   }
usersync/cookie.go

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, that is actually a bug. If it was simply it didn't need the := operator, then Go would flag that as a compiler error. What Go actually did is see that I used uidsCookieStr := inside of a scope {}, so assumed I was declaring a new variable of the same name, that would only exist inside that code block. That is valid, but not the intended behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, variable shadowing can be tricky in Go. Other languages ban it outright, like C#.

uidsCookieStr += SameSiteAttribute
sameSiteCookie = &http.Cookie{
Name: SameSiteCookieName,
Value: SameSiteCookieValue,
Expires: time.Now().Add(ttl),
Path: "/",
Secure: true,
}
sameSiteCookieStr := sameSiteCookie.String()
sameSiteCookieStr += SameSiteAttribute
w.Header().Add("Set-Cookie", sameSiteCookieStr)
} else {
uidsCookieStr = httpCookie.String()
}
w.Header().Add("Set-Cookie", uidsCookieStr)
}
Expand Down
3 changes: 3 additions & 0 deletions usersync/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ func TestSetCookieOnResponseForSameSiteNone(t *testing.T) {
if !strings.Contains(writtenCookie, "SSCookie=1") {
t.Error("Set-Cookie should contain SSCookie=1")
}
if !strings.Contains(writtenCookie, "; Secure;") {
t.Error("Set-Cookie should contain Secure")
}
}

func TestSetCookieOnResponseForOlderChromeVersion(t *testing.T) {
Expand Down