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 6708505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:46:04+00:00 2026-05-26T07:46:04+00:00

I’m trying to prevent CSRF attacks in a an existing large web app. I’ve

  • 0

I’m trying to prevent CSRF attacks in a an existing large web app. I’ve opted for a token that is injected using JS into any form and a Java Servlet filter that will check for its presence before allowing the request to continue. Firstly we are happy to require the user has JS.

Secondly – how do I do it?

The simplest idea: $(document).live("submit", myTokenAddFn); doesn’t work.

The token injection part is proving very tricky since, as I said, the web app is very large, and there a number of bridges to cross:

  • There are forms that are created (using JS) after the page has loaded
  • We make use of JSF (which has onclick attributes on anchor tags to submit forms)
  • There are instances of jQuery form submits using AJAX
  • There are plenty of onclick handlers attached to form submit elements already

I feel I am very close but I can’t believe I’m not impacting performance on slower browsers. I am starting to think that there might be a better way.

What I have done:

  • To overcome jQuery AJAX form submissions: Using the ajaxSend method to append my token to the real data values before the AJAX request is sent
  • To add the token to simple form elements on the page that are there on page load: On page load effectively loop through forms and add the token, then add jQuery data that says this form has the token (to improve performance of next step)
  • To overcome dynamically created forms: $("input[type='submit'],input[type='image'],button,a[onclick]").live("mousedown", myTokenAddFn);
    The above line copes with a lot and the ‘myTokenAddFn’ actually just loops through the page again looking for all forms that don’t have the jQuery data item I added on page load.
    It copes with JSF forms that are submitted with an anchor tag’s onclick attribute, buttons that don’t reside in a form but submit a form and ensures that the token is always added before any other onclick handler submits the form (that might be present).

The latest issue:

A JS form is dynamically added to the page and the user submits it using the enter key while the cursor is in an input text field. The user (thanks to my Java Servlet) is logged out.

The easiest fix would be to include keypress listeners to all input elements, but the performance hit here worries me.

Any suggestions? Ideas for a fresh start?

Thanks in advance.

  • 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-26T07:46:05+00:00Added an answer on May 26, 2026 at 7:46 am

    In your simple idea, you’re binding the event to document, but I’d bind the live listener to "form" – selecting all form tags. Also, in the event listener, just add e.preventDefault(). Like so:

    $("form").live("submit", submitListener);
    
    function submitListener(e) {
       // your handling stuff goes here
       e.preventDefault();
    }
    

    I’ve done the above method on my forms before and it seems to work fine. Hope this does the trick!

    EDIT: I just wanted to be sure this actually works, so here’s a quick example. As you can see, it blocks the submission to google.com and flashes up with a “nope!” to show the submission was blocked.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.