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

apiv2: accept cookie auth when header is non-empty #84617

Merged
merged 1 commit into from
Aug 2, 2022

Conversation

dhartunian
Copy link
Collaborator

@dhartunian dhartunian commented Jul 18, 2022

In order to make use of HTTP endpoints under /api/v2 in the DB Console
it is necessary to support cookie-based authentication for ergonomic
Javascript use.

Previously, header-based auth was not possible to use in the DB Console
because the login endpoint we use returns the session in a Cookie.
Moving this cookie into a header would require us to read into a
less-secure storage method (local storage, redux, etc.) instead of
keeping it secure in the browser's cookie storage.

We implement a suggestion to rely on Cookie auth by requiring the
presence of the auth header with a magic value of "cookie" that tells the
server to look for the session in the session cookie. This forces the caller
to modify the request via JS, which protects us from CSRF since
cross-origin requests can only be "simple". See the issue for further
discussion.

Resolves #84311

Release note (security update): The HTTP endpoints under the /api/v2 prefix
will now accept cookie-based authentication similar to other HTTP endpoints
used by the DB Console. The encoded session must be in a cookie named
"session", and the "X-Cockroach-API-Session" header is required to be set
to "cookie" for the session to be read from the cookie header. A cookie
provided without the custom header present will be ignored.

@dhartunian dhartunian requested a review from knz July 18, 2022 21:48
@dhartunian dhartunian requested review from a team as code owners July 18, 2022 21:48
@dhartunian dhartunian requested a review from a team July 18, 2022 21:48
@dhartunian dhartunian requested a review from a team as a code owner July 18, 2022 21:48
@dhartunian dhartunian requested review from erikgrinaker and removed request for a team July 18, 2022 21:48
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@dhartunian dhartunian requested review from xinhaoz and a team July 18, 2022 22:06
Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

May I recommend a release note in category "security update" that explains the change, e.g. "The /api/v2 HTTP endpoints now accept authentication information using a cookie (FIXME: insert required cookie name here), as long as the X-Cockroach-API-Session header is also present in the request and has a non-empty value."

I would also like to suggest that you fix the expected value of the header to a specific string, e.g. yes in your test, and make it part of your check. This way, we could use different values in the header in the future to support multiple authn protocols side-by-side.

Reviewed 3 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @erikgrinaker, @kpatron-cockroachlabs, and @xinhaoz)

Copy link
Contributor

@kpatron-cockroachlabs kpatron-cockroachlabs left a comment

Choose a reason for hiding this comment

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

:lgtm:

For my own curiosity, is there a reason we use a custom header for the API token instead of the standard Authorization: Bearer <value> pattern?

From a person only looking at the client interactions it may make things slightly easier to understand if we use a separate header for CSRF protection vs API token.

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

@erikgrinaker erikgrinaker removed their request for review July 19, 2022 14:35
Copy link
Collaborator Author

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

@knz do you know of a historical reason why we don't use the Authorization: Bearer pattern?

@kpatron-cockroachlabs thanks for the suggestion. Perhaps I'll use X-Cockroach-API-Auth with a value of cookie to handle this particular case.

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

@knz
Copy link
Contributor

knz commented Jul 19, 2022

do you know of a historical reason why we don't use the Authorization: Bearer pattern?

When this was first implemented I have a vague recollection of us (@bdarnell , @itsbilal and I) discussing CSRF protection and one of us determined a custom header was an existing established practice. Maybe Ben and/or Bilal can comment further?

Copy link
Contributor

@kpatron-cockroachlabs kpatron-cockroachlabs left a comment

Choose a reason for hiding this comment

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

My understanding is the Authorization header should also satisfy those CSRF protections as well.

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

Copy link
Member

@xinhaoz xinhaoz left a comment

Choose a reason for hiding this comment

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

I tested my changes on top of this and everything works as expected. Thanks for this, David!

Reviewed 1 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @dhartunian)

ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Jul 21, 2022
This commit adds the v1 Insights page to the DB Console, via the
cluster-ui package. The v1 Insights page only includes a
Transactions Insights overview page, populated with information
served a new "endpoint" built on top of the SQL-over-HTTP API.

Note this PR is dependent on the changes in cockroachdb#84617 and
xinhaoz/cockroach@c069738..

Release note (ui change): Added new Insights page to DB Console
@ericharmeling
Copy link
Contributor

I tested my changes on top of this and everything works as expected. Thanks for this, David!

Same (using @xinhaoz's work)! #84612

ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Jul 25, 2022
This commit adds the v1 Insights page to the DB Console, via the
cluster-ui package. The v1 Insights page only includes a
Transactions Insights overview page, populated with information
served a new "endpoint" built on top of the SQL-over-HTTP API.

Note this PR is dependent on the changes in cockroachdb#84617 and
xinhaoz/cockroach@c069738..

Release note (ui change): Added new Insights page to DB Console
ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Jul 25, 2022
This commit adds the v1 Insights page to the DB Console, via the
cluster-ui package. The v1 Insights page only includes a
Transactions Insights overview page, populated with information
served a new "endpoint" built on top of the SQL-over-HTTP API.

Note this PR is dependent on the changes in cockroachdb#84617 and
xinhaoz/cockroach@c069738..

Release note (ui change): Added new Insights page to DB Console
ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Jul 25, 2022
This commit adds the v1 Insights page to the DB Console, via the
cluster-ui package. The v1 Insights page only includes a
Transactions Insights overview page, populated with information
served a new "endpoint" built on top of the SQL-over-HTTP API.

Note this PR is dependent on the changes in cockroachdb#84617 and
xinhaoz/cockroach@c069738..

Release note (ui change): Added new Insights page to DB Console
ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Jul 26, 2022
This commit adds the v1 Insights page to the DB Console, via the
cluster-ui package. The v1 Insights page only includes a
Transactions Insights overview page, populated with information
served a new "endpoint" built on top of the SQL-over-HTTP API.

Note this PR is dependent on the changes in cockroachdb#84617 and
xinhaoz/cockroach@c069738..

Release note (ui change): Added new Insights page to DB Console
ericharmeling added a commit to ericharmeling/cockroach that referenced this pull request Jul 26, 2022
This commit adds the v1 Insights page to the DB Console, via the
cluster-ui package. The v1 Insights page only includes a
Transactions Insights overview page, populated with information
served a new "endpoint" built on top of the SQL-over-HTTP API.

Note this PR is dependent on the changes in cockroachdb#84617 and
xinhaoz/cockroach@c069738..

Release note (ui change): Added new Insights page to DB Console
@dhartunian dhartunian force-pushed the api-v2-accepts-cookies branch from 713ebb2 to 264ac95 Compare August 1, 2022 13:11
Copy link
Collaborator Author

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

I've decided that this PR is not the forum to modify the auth session header name and, am conservatively keeping the one custom header we have for now.

In the case of cookie-based auth, the session header will require a specific value of "cookie" in order to read the session from the cookie.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @knz and @xinhaoz)

@dhartunian
Copy link
Collaborator Author

@knz PTAL

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

:lgtm: modulo nits

Reviewed 3 of 3 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @dhartunian)


pkg/server/api_v2_auth.go line 290 at r2 (raw file):

// session can be looked up either from a session cookie as used in the
// non-v2 API server, or via the session header. In order for us to pick
// up a session, the header must still be non-empty in the case of

nit: update comment to mention 'cookie' keyword.


pkg/server/api_v2_auth.go line 322 at r2 (raw file):

	for i := range possibleSessions {
		decoded, err = base64.StdEncoding.DecodeString(possibleSessions[i])
		if err != nil {

here and below. log.Warningf for the error.


pkg/server/api_v2_auth.go line 329 at r2 (raw file):

			continue
		}
		// We've successfully decoded a session from cookie or header

nit: period at end of sentnece.

In order to make use of HTTP endpoints under `/api/v2` in the DB Console
it is necessary to support cookie-based authentication for ergonomic
Javascript use.

Previously, header-based auth was not possible to use in the DB Console
because the login endpoint we use returns the session in a Cookie.
Moving this cookie into a header would require us to read into a
less-secure storage method (local storage, redux, etc.) instead of
keeping it secure in the browser's cookie storage.

We implement a suggestion to rely on Cookie auth by requiring the
presence of the auth header with a magic value of `"cookie"` that tells the
server to look for the session in the session cookie. This forces the caller
to modify the request via JS, which protects us from CSRF since
cross-origin requests can only be "simple". See the issue for further
discussion.

Resolves cockroachdb#84311

Release note (security update): The HTTP endpoints under the `/api/v2` prefix
will now accept cookie-based authentication similar to other HTTP endpoints
used by the DB Console. The encoded session *must* be in a cookie named
`"session"`, and the `"X-Cockroach-API-Session"` header is required to be set
to `"cookie"` for the session to be read from the cookie header. A cookie
provided without the custom header present will be ignored.
@dhartunian dhartunian force-pushed the api-v2-accepts-cookies branch from 8bf2a58 to b5bad38 Compare August 2, 2022 14:59
Copy link
Collaborator Author

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @knz)


pkg/server/api_v2_auth.go line 290 at r2 (raw file):

Previously, knz (kena) wrote…

nit: update comment to mention 'cookie' keyword.

Done.


pkg/server/api_v2_auth.go line 322 at r2 (raw file):

Previously, knz (kena) wrote…

here and below. log.Warningf for the error.

Done.


pkg/server/api_v2_auth.go line 329 at r2 (raw file):

Previously, knz (kena) wrote…

nit: period at end of sentnece.

Done.

@dhartunian
Copy link
Collaborator Author

TFTRs

bors r=knz,kpatron-cockroachlabs,xinhaoz

@craig
Copy link
Contributor

craig bot commented Aug 2, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 2, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 2, 2022

Build succeeded:

@craig craig bot merged commit 05e3d5d into cockroachdb:master Aug 2, 2022
@blathers-crl
Copy link

blathers-crl bot commented Aug 2, 2022

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from b5bad38 to blathers/backport-release-22.1-84617: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 22.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

craig bot pushed a commit that referenced this pull request Aug 12, 2022
84612: ui: add insights overview page v1 r=maryliag a=ericharmeling

This commit adds the v1 Insights page to the DB Console, via the cluster-ui package. The v1 Insights page only includes a Transactions Insights overview page, populated with information served a new "endpoint" built on top of the SQL-over-HTTP API.

Note this PR is dependent on the changes in #84617 and #85080.

After #84998 is merged with all the needed columns, we can rewrite the insights API to query the internal insights table.

Fixes #83774.

Release note (ui change): Added new Insights page to DB Console

85757: backupccl,importer: remove `at_current_time` cluster settings r=adityamaru a=erikgrinaker

Release note (ops change): The cluster settings
`bulkio.restore_at_current_time.enabled` and
`bulkio.import_at_current_time.enabled`, which were introduced in 22.1
and defaulted to `true`, have been retired. They are now in effect
always enabled.

Co-authored-by: Eric Harmeling <[email protected]>
Co-authored-by: Erik Grinaker <[email protected]>
THardy98 pushed a commit to THardy98/cockroach that referenced this pull request Aug 12, 2022
This commit adds the v1 Insights page to the DB Console, via the
cluster-ui package. The v1 Insights page only includes a
Transactions Insights overview page, populated with information
served a new "endpoint" built on top of the SQL-over-HTTP API.

Note this PR is dependent on the changes in cockroachdb#84617 and
xinhaoz/cockroach@c069738..

Release note (ui change): Added new Insights page to DB Console
@dhartunian dhartunian deleted the api-v2-accepts-cookies branch November 14, 2022 16:37
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.

api: support cookie-based auth for read-only queries for sql-over-http
6 participants