361

I have a drop down on a web page which is breaking when the value string contains a quote.

The value is "asd, but in the DOM it always appears as an empty string.

I have tried every way I know to escape the string properly, but to no avail.

<option value=""asd">test</option>
<option value="\"asd">test</option>
<option value="&quot;asd">test</option>
<option value="&#34;asd">test</option>

How do I render this on the page so the postback message contains the correct value?

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Oct 25, 2010 at 14:08
7
  • How are you generating the page? Commented Oct 25, 2010 at 14:11
  • 2
    What if you use single quotes? <option value='"asd'>test</option> Commented Oct 25, 2010 at 14:11
  • 8
    I have to point out none of these answers say how to properly escape strings for use inside html attributes Commented Feb 6, 2013 at 18:46
  • 5
    @reconbot That would depend on how the HTML was being generated. The question was about quotes, so technically the accepted answer answers the question asked. As to how to properly escape strings, I don't have a link handy for the general case, but in PHP you'd use htmlentities. Commented Feb 25, 2013 at 16:50
  • possible duplicate of how to have quotation marks in html input values Commented Jul 28, 2014 at 16:24

6 Answers 6

446

&quot; is the correct way, the third of your tests:

<option value="&quot;asd">test</option>

You can see this working below, or on jsFiddle.

alert($("option")[0].value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
 <option value="&quot;asd">Test</option>
</select>

Alternatively, you can delimit the attribute value with single quotes:

<option value='"asd'>test</option>
jpaugh
7,1845 gold badges46 silver badges94 bronze badges
answered Oct 25, 2010 at 14:11
Sign up to request clarification or add additional context in comments.

8 Comments

OP's fourth option, &#34;, is also a valid way to escape quotes. There's a benefit to using numeric html entities over named entities, in that named entities do not cover all characters, while numeric entities do. The full HTML4 list is at w3.org/TR/html4/sgml/entities.html .
@atk: yes, &quot; maps to the same character as &#34;, but there's no benefit of using the numeric option here because &quot; is a defined named entity. &quot; is also easier to remember.
I agree. In this particular case, it's easier to use &quot;. I intended only to point out the general case.
@SIDU: change it to &amp;quot;a (replace the & with &amp;)
^ infinite loop
|
30

Per HTML syntax, and even HTML5, the following are all valid options:

<option value="&quot;asd">test</option>
<option value="&#34;asd">test</option>
<option value='"asd'>test</option>
<option value='&quot;asd'>test</option>
<option value='&#34;asd'>test</option>
<option value=&quot;asd>test</option>
<option value=&#34;asd>test</option>

Note that if you are using XML syntax the quotes (single or double) are required.

Here's a jsfiddle showing all of the above working.

answered Jan 19, 2016 at 15:43

Comments

26

If you are using PHP, try calling htmlentities or htmlspecialchars function.

answered Jun 13, 2013 at 12:36

2 Comments

just using them may not be enough, try <option value='<?php echo htmlentities("' onmouseover='alert(123);' foo='"); ?>' /> - make sure you use it with ENT_QUOTES, this is safe: <option value='<?php echo htmlentities("' onmouseover='alert(123);' foo='", ENT_QUOTES); ?>' /> , but in addition to ENT_QUOTES you should also add ENT_SUBSTITUTE and ENT_DISALLOWED, personally i've used this wrapper for years: function hhb_tohtml(string $str):string { return htmlentities($str, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED, 'UTF-8', true); }
Don't understand this comment.
11

Another option is replacing double quotes with single quotes if you don't mind whatever it is. But I don't mention this one:

<option value='"asd'>test</option>

I mention this one:

<option value="'asd">test</option>

In my case I used this solution.

answered Nov 27, 2012 at 9:00

2 Comments

But if the value contains single & double quotes, this will fail
@Raptor I said if the value contains double quotes, convert them to single quotes. If the value contains single quotes, then it will be no problem.
3

If you are using JavaScript and Lodash, then you can use _.escape(), which escapes ", ', <, >, and &.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Nov 14, 2019 at 23:33

Comments

2

You really should only allow untrusted data into a whitelist of good attributes like: align, alink, alt, bgcolor, border, cellpadding, cellspacing, class , color, cols, colspan , coords, dir, face, height, hspace, ismap, lang, marginheight, marginwidth, multiple, nohref, noresize, noshade, nowrap, ref, rel, rev, rows, rowspan, scrolling, shape, span , summary, tabindex, title , usemap, valign, value, vlink, vspace, and width.

You really want to keep untrusted data out of JavaScript handlers as well as id or name attributes (they can clobber other elements in the DOM).

Also, if you are putting untrusted data into a SRC or HREF attribute, then it’s really a untrusted URL, so you should validate the URL. Make sure it’s not a JavaScript: URL, and then HTML entity encode.

More details on all of there here: https://www.owasp.org/index.php/Abridged_XSS_Prevention_Cheat_Sheet

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Nov 20, 2011 at 2:12

3 Comments

I know this is late, but almost all of those attributes are deprecated in HTML4.01 and removed in 5. It may not matter now anyway, as there are better ways to protect yourself, just pointing it out.
The question is asking about data with quote characters in it, not about untrusted data.
The www.owasp.org link is broken: "404 - Not Found"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.