Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 598499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:23:42+00:00 2026-05-13T16:23:42+00:00

To protect against CSRF you should put a nonce in a hidden field in

  • 0

To protect against CSRF you should put a nonce in a hidden field in the form, and in a cookie or in the session variable. But what if the user opens several pages in different tabs? In this case each tab would have a form with a unique nonce, but there would be only one nonce stored in the session variable or cookie. Or if you try to store all the nonces in the cookie/session variable, how would you identify which one belongs to which form?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:23:42+00:00Added an answer on May 13, 2026 at 4:23 pm

    You can store the same nonce in each of the forms. The easiest way to do it is to tie the nonce to the session ID, so that those forms only work in that session.

    You will want to make it hard for attackers to snarf session IDs and create their own nonces. So, one way to go about it is to use HMAC-SHA256 (or the like) to hash the session ID, using a key that you do not expose to the public.

    (Obviously if the attacker can get the actual session ID itself, they can already do session hijacking. So that’s not what I’m talking about, but rather the ability for an attacker to craft a script (that runs on the victim’s computer) that can somehow grab the session ID and use that to dynamically generate a URL with the nonce pre-filled.)


    ETA: Whether the above approach is enough on its own depends on how long you expect your typical sessions to last. If users usually use long-lasting sessions spanning longer than a few hours, you’ll need to use something more sophisticated.

    One approach is to create a new nonce for each form, that contains the timestamp, as well as hash(timestamp . sessionid) (where hash is some variant of HMAC as described above, to prevent forgery, and . is string concatenation). You then verify the nonce by:

    1. checking the timestamp to ensure that the nonce is fresh enough (this is up to your policy, but a few hours is typical)
    2. then, calculating the hash based on the timestamp and session ID, and comparing against the nonce, to verify that the nonce is authentic

    If the nonce check fails, you’ll want to display a new form, pre-populated with the user’s submission (so that if they took a whole day to write their post, they won’t lose all their hard work), as well as a fresh nonce. Then the user can resubmit straight away successfully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to protect my .NET web site against CSRF attacks using a hidden
I am using ZF's Zend_Form_Element_Hash to protect my form against CSRF . The problem
How does Windows protect against a user-mode thread from arbitrarily transitioning the CPU to
Does build protect against sql injection? Example: @post = @user.posts.build(params[:post]) @post.save Didn't see build
Does ModelName.new protect against sql injection? Example: @user = User.new(params[:user]) @user.save I've read the
Possible Duplicate: Can I protect against SQL Injection by escaping single-quote and surrounding user
So the typical CSRF protection method is storing a nonce in a session and
Are there any libraries to protect against CSRF(PHP5.1/5.2) or do I need to create
Possible Duplicate: Can I protect against SQL Injection by escaping single-quote and surrounding user
What can a driver do defensively to protect against a user-space app that issues

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.