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

server, lint: centralize and restrict HTTP cookie construction #119748

Merged
merged 2 commits into from
Mar 6, 2024

Conversation

dhartunian
Copy link
Collaborator

server: centralize cookie construction

All cookie construction for session and tenant cookies is now moved to
explicit constructors instead of initializing http.Cookie{} structs
directly. This reduces the chance of error and explicitly requires
setting the secure flag via the constructor.

The constructs were created to exactly mimic all existing cookie
settings. No change in flags should happen as a result of this commit.

A separate commit will introduce a linter to prevent raw cookie
creation.

Epic: None
Release note: None

lint: add linter to reject raw http.Cookie structs

We previously introduced bugs when creating HTTP cookies in new code
that did not have the proper fields set. This linter aims to enforce
use of cookies only through pre-generated constructors in pkg/server/ authserver/cookie.go. This will reduce the chance of errors in the
future and keep things consistent.

Epic: None
Release note: None

@dhartunian dhartunian requested review from a team as code owners February 28, 2024 22:08
@dhartunian dhartunian requested review from a team February 28, 2024 22:08
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@dhartunian
Copy link
Collaborator Author

Please only review last 2 commits. First two getting merged here: #119261

@dhartunian dhartunian force-pushed the centralize-cookie-construction branch from ad493f8 to e07d9d7 Compare February 28, 2024 22:12
Copy link
Collaborator

@stevendanna stevendanna left a comment

Choose a reason for hiding this comment

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

Thanks for going the extra mile here.

I left some comments but neither are blocking.

pkg/server/authserver/cookie.go Show resolved Hide resolved
Comment on lines 11 to 17
var Analyzer = &analysis.Analyzer{
Name: "norawcookies",
Doc: `check for correct use of Cookie constructors`,
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

You've already written it so I wouldn't change it. But I do wonder for a linter like this whether we get most of the benefit out of a git-grep based linter like some of the others in pkg/testutils/lint_test.go, which I only mention because I imagine it would be a lot faster.

That said, this lays the groundwork for adding more checks in the future around aspects of cookie creation if we want.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm just learning about the linter infra so I'll give this a quick shot and see if I get anywhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

git grep linter is way simpler so I'm keeping it. thanks for the pointer.

pkg/testutils/lint/passes/norawcookies/testdata/src/a/a.go Outdated Show resolved Hide resolved
@dhartunian dhartunian force-pushed the centralize-cookie-construction branch 2 times, most recently from e0327c9 to ce788bf Compare March 4, 2024 22:42
@dhartunian dhartunian requested a review from stevendanna March 4, 2024 22:43
Copy link
Contributor

@abarganier abarganier left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @dhartunian, @stevendanna, and @xinhaoz)


pkg/server/server_controller_http.go line 101 at r5 (raw file):

		// Clear session and tenant cookies since it appears they reference invalid state.
		http.SetCookie(w, authserver.CreateEmptySessionCookieWithImmediateExpiry(!c.disableTLSForHTTP))
		http.SetCookie(w, authserver.CreateEmptyTenantCookieWithImmediateExpiry(!c.disableTLSForHTTP))

Nice! This is a big win for readability. Good idea.

Code quote:

authserver.CreateEmptyTenantCookieWithImmediateExpiry

pkg/server/authserver/cookie.go line 214 at r5 (raw file):

// be optionally marked `Secure` if you're runinning in secure mode by
// setting the `forHttpsOnly` argument to true.
func CreateTenantCookie(value string, forHTTPSOnly bool) *http.Cookie {

nit: Here and below, would TenantSelect instead of just Tenant be a bit clearer?

Code quote:

CreateTenantCookie

All cookie construction for session and tenant cookies is now moved to
explicit constructors instead of initializing `http.Cookie{}` structs
directly. This reduces the chance of error and explicitly requires
setting the `secure` flag via the constructor.

The constructs were created to exactly mimic all existing cookie
settings. No change in flags should happen as a result of this commit.

A separate commit will introduce a linter to prevent raw cookie
creation.

Epic: None
Release note: None
We previously introduced bugs when creating HTTP cookies in new code
that did not have the proper fields set. This linter aims to enforce
use of cookies only through pre-generated constructors in `pkg/server/
authserver/cookie.go`. This will reduce the chance of errors in the
future and keep things consistent.

Epic: None
Release note: None
@dhartunian dhartunian force-pushed the centralize-cookie-construction branch from ce788bf to d63936b Compare March 6, 2024 17:14
@dhartunian
Copy link
Collaborator Author

TFTRs

bors r+

@craig
Copy link
Contributor

craig bot commented Mar 6, 2024

Build succeeded:

@craig craig bot merged commit 549a5c2 into cockroachdb:master Mar 6, 2024
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants