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

fix: Set 'Secure' attribute to true for sensitive cookies #3407

Closed

Conversation

DharunKumar04
Copy link
Contributor

This pull request addresses the security issue raised by Snyk regarding sensitive cookies in the account package not having the 'Secure' attribute set. The 'Secure' attribute is essential for ensuring that cookies are transmitted securely over HTTPS, preventing potential man-in-the-middle attacks.

Changes Made:

  • Added the 'Secure' attribute to cookies in the 'includeTokenCookie' and 'deleteTokenCookieIfPresent' functions.
  • The 'Secure' attribute is set to true when the request is made over HTTPS, ensuring that sensitive cookies are only sent over encrypted connections.

@@ -33,6 +33,9 @@ func includeTokenCookie(
cookie.Expires = time.UnixMilli(*tokenResponse.Token.ExpiresAt)
}

// Set the Secure attribute to true
cookie.Secure = true
Copy link
Contributor

@enver-bisevac enver-bisevac Oct 9, 2023

Choose a reason for hiding this comment

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

A cookie with the Secure attribute is only sent to the server with an encrypted request over the HTTPS protocol. MDN
why we enforce true here?, bcoz in newEmptyTokenCookie there is already isSecure value.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree - this will actually break gitness running on http (localhost might work as some browsers ignore secure flag there and still send the cookie). I've set a custom domain in my local /etc/hosts file, and with the change here it'll break that

@@ -48,16 +51,21 @@ func deleteTokenCookieIfPresent(r *http.Request, w http.ResponseWriter, cookieNa
cookie.Value = ""
cookie.Expires = time.UnixMilli(0) // this effectively tells the browser to delete the cookie

// Set the Secure attribute to true
cookie.Secure = true
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above ^^^

http.SetCookie(w, cookie)
}

func newEmptyTokenCookie(r *http.Request, cookieName string) *http.Cookie {
isSecure := r.TLS != nil // Check if the request is using HTTPS
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@DharunKumar04
Copy link
Contributor Author

Hi @enver-bisevac

Thank you for your feedback and the reference to the MDN documentation on cookies.As you mentioned, a cookie with the 'Secure' attribute is sent to the server only with an encrypted request over the HTTPS protocol.I understand the concern and would like to clarify my approach The reason for enforcing the 'Secure' attribute as true in the includeTokenCookie and deleteTokenCookieIfPresent functions is to provide an explicit security measure. Since the authentication token cookie lacks the 'Secure' attribute, it maybe sent in plaintext over an unencrypted HTTP connection.And attackers can use this vulnerability as an attack surface.

By setting the secure attribute to true it serves as a clear indication that these cookies should only be transmitted over HTTPS, aligning with the best practices outlined in the MDN documentation.And also explicitly setting 'Secure' within these functions, we adopt a defensive coding approach. This ensures that even if the isSecure variable in the newEmptyTokenCookie function were to change in the future due to code evolution, our security measure remains intact.

If you believe it's unnecessary for our codebase or if there are better ways to handle this, please let me know, or we can close this pull request.Thanks :)

http.SetCookie(w, cookie)
}

func newEmptyTokenCookie(r *http.Request, cookieName string) *http.Cookie {
isSecure := r.TLS != nil // Check if the request is using HTTPS
Copy link
Collaborator

@johannesHarness johannesHarness Oct 9, 2023

Choose a reason for hiding this comment

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

Not sure how r.TLS is fixing anything specific, could you elaborate?
One scenario I'm thinking of improving is gitness behind a reverse proxy with TLS offloading - in that case r.TLS would be nil and r.URL.Proto would be http, which would lead to cookie.Secure = false eventhough the caller is using https.

So an improvement would be to check for the availability of a X-Forwarded-Proto header (otherwise fall back to r.URL.Proto or r.TLS) to determine whether cookie.Secure should be set?

cc @enver-bisevac

Copy link
Contributor

Choose a reason for hiding this comment

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

@CLAassistant
Copy link

CLAassistant commented Oct 10, 2023

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ DharunKumar04
❌ ATBBt26NFnTCjw8nhPR8HVcqYZxeBF349DC8


ATBBt26NFnTCjw8nhPR8HVcqYZxeBF349DC8 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

None yet

4 participants