sillydaddy
5.47D
4.89D

Cookie 的 Secure 有点迷惑人

  •  
  •   sillydaddy · Oct 12, 2022 · 1472 views
    This topic created in 1331 days ago, the information mentioned may be changed or developed.

    根据官方说明,cookie 的属性项包含 secure 时,不会将 cookie 在 header 中明文发送到服务器,也就是说只有 https 协议下才会发送此 cookie ,防止被中间人攻击。

    比如

    document.cookie = "tagname = test; secure" 
    

    本来这点是确保安全的,挺好。

    但是根据 https://stackoverflow.com/questions/37234687/how-to-set-cookie-secure-flag-using-javascript 的解释,如果是 http 协议的话,不光不能发送,连在浏览器端通过 document.cookie=来设置都不行了——这个“禁止”动作的思维跳跃比较大。

    那我怎么存一些只在浏览器端保存一段时间的信息啊?😳

    3 replies    2022-10-13 09:22:14 +08:00
    liyang5945
        1
    liyang5945  
       Oct 12, 2022
    Localstorage
    sillydaddy
        2
    sillydaddy  
    OP
       Oct 13, 2022
    @liyang5945
    只能如此了。
    sillydaddy
        3
    sillydaddy  
    OP
       Oct 13, 2022
    刚看到,文档里确实有说 “Insecure sites (with http: in the URL) can't set cookies with the Secure attribute. ”

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5718 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 262ms · UTC 03:05 · PVG 11:05 · LAX 20:05 · JFK 23:05
    ♥ Do have faith in what you're doing.