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

securecookies #31

Closed
komuw opened this issue Jun 14, 2022 · 6 comments · Fixed by #143
Closed

securecookies #31

komuw opened this issue Jun 14, 2022 · 6 comments · Fixed by #143

Comments

@komuw
Copy link
Owner

komuw commented Jun 14, 2022

@komuw
Copy link
Owner Author

komuw commented Jun 19, 2022

@komuw
Copy link
Owner Author

komuw commented Jul 2, 2022

@komuw
Copy link
Owner Author

komuw commented Sep 23, 2022

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/komuw/ong/cookie"
	"github.com/komuw/ong/enc"
)

func someHandler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		key := "hard-passwd"
		e := enc.New(key)

		cookieName := "logID"
		cookieVal := "slkPPjadm"
		encryptedEncodedCookieVal := e.EncryptEncode(cookieVal)

		cookie.Set(
			w,
			cookieName,
			encryptedEncodedCookieVal,
			"example.com",
			15*time.Minute,
			false,
		)

		fmt.Fprint(w, "hello")
	}
}

@komuw
Copy link
Owner Author

komuw commented Sep 29, 2022

https://www.alexedwards.net/blog/working-with-cookies-in-go

  • Effectively, encrypting our cookie data using AES-GCM is a relatively easy way to give us confidential, tamper-proof, cookies in a single step.

  • You can use hmac to make sure cookies are tamper proof

  • Or you can use an authenticated encryption to achieve encryption and tamper proofing

  • Cookies can be edited by clients and hence the server should never trust them.
    You can however verify(on the server) if a cookie has been edited. One way is to generate a HMAC signature of the cookie name and value, and then prepend this signature to the cookie value before sending it to the client.
    This is tamper proofing(or message authentication)

  • Hmac signed cookies can be read/seen by the client but cannot be changed(or rather, any change would be caught on the server).
    There are times when we would not want our cookies to be seen by clients. We can use encryption for that; when clients read, they get gibberish.
    Using a cipher.AEAD can achieve both encryption & tamper-proofing.

@komuw
Copy link
Owner Author

komuw commented Sep 29, 2022

Note, you should encrypt both the name and value together, and store the result as the cookie value.
This is because enc.New uses cipher.AEAD which provides encryption and authentication.
We also want to authenticate(ie, ensure it has not been tampered with) the cookie name.

func someHandler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		key := "hard-passwd"
		e := enc.New(key)

		cookieName := "logID"
		cookieVal := "slkPPjadm"
		encryptedEncodedCookie := e.EncryptEncode(fmt.Sprintf("%s:%s", cookieName, cookieVal))

		cookie.Set(
			w,
			cookieName,
			encryptedEncodedCookie,
			"example.com",
			15*time.Minute,
			false,
		)

		fmt.Fprint(w, "hello")
	}
}

@komuw komuw closed this as completed in #143 Oct 2, 2022
komuw added a commit that referenced this issue Oct 2, 2022
What:
- add secure/encrypted cookies

Why:
- Fixes: #31
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 a pull request may close this issue.

1 participant