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

  • Home
  • SEARCH
  • 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 879527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:56:47+00:00 2026-05-15T11:56:47+00:00

In my web app, users can input text data. This data can be shown

  • 0

In my web app, users can input text data. This data can be shown to other users, and the original author can also go back and edit their data. I’m looking for the correct way to safely escape this data.

I’m only sql sanitizing on the way in, so everything is stored as it reads. Let’s say I have "déjà vu" in the database. Or, to be more extreme, a <script> tag. It is possible that this may be valid, and not even maliciously intended, input.

I’m using htmlentities() on the way out to make sure everything is escaped. The problem is that html and input fields treat things differently. I want to make sure it’s safe in HTML, but that the author when editing the text, sees exactly what they typed in the input fields. I’m also using jQuery to fill form fields with the data dynamically.

If I do this:

 <p><?=htmlentities("déjà vu");?></p>
 <input type=text value="<?=htmlentities("déjà vu");?>">

The page source puts d&eacute;j&agrave; vu in both places (I had to backtick that or you would see "déjà vu"!) The problem is that the output in the <p> is correct, but the input just shows the escaped text. If the user resubmits their form, they double escape and ruin their input.

I know I still have to sanitize text that goes into the field, otherwise you can end the value quote and do bad things. The only solution I found is this. Again, I’m using jQuery.

var temp = $("<div></div>").html("<?=htmlentities("déjà vu");?>");
$("input").val(temp.html());

This works, as it causes the div to read the escaped text as encoded characters, and then the jquery copies those encoded characters to the input tag, properly preserved.

So my question: is this still safe, or is there a security hole somewhere? And more importantly, is this the only / correct way to do this? Am I missing something about how html and character encoding works that make this a trivial issue to solve?

EDIT

This is actually wrong, I oversimplified my example to the point of it not working. The problem is actually because I’m using jQuery’s val() to insert the text into the field.

<input>
<script>$("input").val("<?=htmlentities("déjà vu");?>");</script>

The reason for this is that the form is dynamic – the user can add or remove fields at will and so they are generated after page load.

So it seems that jQuery is escaping the data to go into the input, but it’s not quite good enough – if I don’t do anything myself, a user can still put in a </script> tag, killing my code and inserting malicious code. But there’s another argument to be made here. Since only the original author can see the text in an input box anyway, should I even bother? Basically the only people they could execute an XSS attack against is themselves.

  • 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-15T11:56:48+00:00Added an answer on May 15, 2026 at 11:56 am

    I’m sorry but I cannot reproduce the behaviour you describe. I’ve always used htmlspecialchars() (which does essentially the same task as htmlentities()) and it’s never lead to any sort of double-encoding. The page source shows d&eacute;j&agrave; vu in both places (of course! that’s the point!) but the rendered page shows the appropriate values and that’s what sent back to the server.

    Can you post a full self-contained code snippet that exhibits such behaviour?

    Update: some testing code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head><title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    
    <?php
    
    $default_value = 'déjà vu <script> ¿foo?';
    
    if( !isset($_GET['foo']) ){
        $_GET['foo'] = $default_value;
    }
    
    ?>
    
    <form action="" method="get">
        <p><?php echo htmlentities($_GET['foo']); ?></p>
        <input type="text" name="foo" value="<?php echo htmlentities($_GET['foo']); ?>">
        <input type="submit" value="Submit">
    </form>
    
    </body>
    </html>
    

    Answer to updated question

    The htmlentities() function, as its name suggests, is used when generating HTML output. That’s why it’s of little use in your second example: JavaScript is not HTML. It’s a language of its own with its own syntax.

    Now, the problem you want to fix is how to generate output that follows these two rules:

    1. It’s a valid string in JavaScript.
    2. It can be embedded safely in an HTML document.

    The closest PHP function for #1 I’m aware of is json_encode(). Since JSON syntax is a subset of JavaScript, if you feed it with a PHP string it will output a JavaScript string.

    As about #2, once the browser enters a JavaScript block it expects a </script> tag to leave it. The json_encode() function takes care of this and escapes it properly (<\/script>).

    My revised test code:

    <?php
    
    $default_value = 'déjà vu </script> ¿foo?';
    
    if( !isset($_GET['foo']) ){
        $_GET['foo'] = $default_value;
    }
    
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head><title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript"><!--
    $(function(){
        $("input[type=text]").val(<?php echo json_encode(utf8_encode($_GET['foo'])); ?>);
    });
    //--></script>
    </head>
    <body>
    
    
    <form action="" method="get">
        <p><?php echo htmlentities($_GET['foo']); ?></p>
        <input type="text" name="foo" value="(to be replaced)">
        <input type="submit" value="Submit">
    </form>
    
    </body>
    </html>
    

    Note: utf8_encode() converts from ISO-8859-1 to UTF-8 and it isn’t required if your data is already in UTF-8 (recommended).

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

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To convert a numerical character ('0' – '9') to its… May 16, 2026 at 5:01 pm
  • Editorial Team
    Editorial Team added an answer echo ${!${false}=getArray()}[0]; This is how it works, step by step… May 16, 2026 at 5:01 pm
  • Editorial Team
    Editorial Team added an answer NSString * param = @"foo"; NSString * jsCallBack = [NSString… May 16, 2026 at 5:01 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am building a new web-app, LAMP environment... I am wondering if preg_match can
Say I want to have a simple web app that takes some user input,
I'm still new to web app development, so bear with me if this sounds
I have a scenario where users of my ASP.NET web application submit testimonials consisting
I'm creating a web app (locally, so security doesn't matter) in PHP where the
I'm looking for advice on how to clean submitted html in a web app
I have a GWT+GAE web app with several service and modules. I am using
I'm doing some load testing on a web app deployed in JBoss. It starts
We have some applications (web and WPF) that call WCF services to access data.
I have a split view controller-based iPad app that uses a Web View to

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.