108

I know that a cookie with secure flag won't be sent via an unencrypted connection. I wonder how this works in-depth.

Who is responsible for determining whether the cookie will be sent or not?

76484
  • 5,791
  • 3
  • 16
  • 27
ted
  • 5,051
  • 6
  • 35
  • 60

2 Answers2

96

The client sets this only for encrypted connections and this is defined in RFC 6265:

The Secure attribute limits the scope of the cookie to "secure" channels (where "secure" is defined by the user agent). When a cookie has the Secure attribute, the user agent will include the cookie in an HTTP request only if the request is transmitted over a secure channel (typically HTTP over Transport Layer Security (TLS) [RFC2818]).

Although seemingly useful for protecting cookies from active network attackers, the Secure attribute protects only the cookie's confidentiality. An active network attacker can overwrite Secure cookies from an insecure channel, disrupting their integrity (see Section 8.6 for more details).

Community
  • 1
  • 1
Cratylus
  • 51,496
  • 63
  • 200
  • 333
  • 4
    in case the client-side doesn't have cookie yet and they should be sent from server-side(e.g. logging in) will server-side be the one to decide to include cookie in response? – ted Dec 05 '12 at 21:57
  • 3
    Server initially sets cookies via "Set-Cookie headers" – Ivan May 25 '16 at 00:54
52

Just another word on the subject:

Omitting secure because your website example.com is fully https is not enough.

If your user is explicitly reaching http://example.com, they will be redirected to https://example.com but that's too late already; the first request contained the cookie.

ruffin
  • 15,005
  • 8
  • 80
  • 126
Alain Tiemblo
  • 34,627
  • 15
  • 118
  • 149