如何使用HttpOnly cookie

问题描述 投票:0回答:1
我了解cookie httpOnly的目标,因为我们无法使用document.cookie来获取cookie信息(例如XSS攻击),所以该系统仍然可以防止cookie的攻击。

我用烧瓶和PHP制作了POC来实践这个概念。

    我发出了POST请求,服务器使用httpOnly创建了response.set_cookie:
  • @app.route('/api/connexion', methods=['POST']) def conn(): login = request.form["login"] password = request.form["password"] response = make_response(jsonify({ 'login':login, 'password': password })) response.set_cookie('token', 't1',httponly=True) return response
      使用php代码,我发送请求并获得响应,但是如何保存cookie以便在其他请求中通过其他API调用重用它?
  • php代码:

    myForm = document.getElementById("myForm") myForm.addEventListener('submit', (e) => { console.log("in event"); e.preventDefault(); let form = new FormData(myForm); fetch("http://127.0.0.1:8000/api/connexion",{ method:'POST', body:form }).then((response) => { return response.json(); }).then((text)=> { console.log(text); }) })

    所以问题是:我是否需要获取cookie并手动保存它,否则cookie将在每个请求中自动发送,我想了解如何。

    非常感谢。

  • xss http cookies
    1个回答
    0
    投票
    只要日期在将来,浏览器就会自动保存cookie。日期过后,浏览器将“抛出” cookie,并将停止使用它。这是[add expiration date to your cookie]的

    良好实践浏览器保存cookie后,它将在请求的

    most

    中将其发送到cookie中指定的域。 在您的示例中-浏览器不会发送您刚定义的新令牌Cookie。这是因为fetch specification,您可以在凭据下查看。

    如何使用获取请求自动发送域cookie

    创建新的提取请求时,请确保init对象中包含{"credentials":"same-origin"}

    在您的代码中,应如下所示:

    myForm = document.getElementById("myForm") myForm.addEventListener('submit', (e)=>{ console.log("in event"); e.preventDefault(); let form = new FormData(myForm); let fetchInitObject = { method: 'POST', body: form, /* The following line will tell the browser to send all the cookies of the site in the URL, as long as the url and the site that runs the Javascript code are from the same origin In your example - the side that runs the javascript code, should be 127.0.0.1 in port 8000, without http */ credentials: 'same-origin' }; fetch("http://127.0.0.1:8000/api/connexion", fetchInitObject).then((response)=>{ return response.json(); } ).then((text)=>{ console.log(text); } ) } )

    © www.soinside.com 2019 - 2024. All rights reserved.