# H() or html\_escape() not escape the single quote... risky?

**URL:** <https://www.ruby-forum.com/t/h-or-html-escape-not-escape-the-single-quote-risky/149667>\
**Category:** Rails\
**Created:** [September 27, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/h-or-html-escape-not-escape-the-single-quote-risky/149667 "2008-09-27T21:29:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 27, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/h-or-html-escape-not-escape-the-single-quote-risky/149667/1 "2008-09-27T21:29:21Z")

</div>

so h() is an alias for html\_escape() and they convert the following 4  
characters

\< \> & "

into

\< \> & "

the single quote is not converted…

I just wonder sometimes we happen to write code such as

and it can cause an cross-site scripting (XSS) attack?

we usually use double quote but sometimes we use single quote like  
somebody can write

puts “”

(sorry i have used PHP for quite some time and so by Ruby is rusty…)

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 27, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/h-or-html-escape-not-escape-the-single-quote-risky/149667/2 "2008-09-27T22:23:24Z")

</div>

This is a Rails question. Please ask Rails questions in a Rails forum,  
not on the Ruby mailing list.

SpringFlowers AutumnMoon wrote:

> the single quote is not converted…
> 
> I just wonder sometimes we happen to write code such as

Just don’t, it’s not correct HTML.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 27, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/h-or-html-escape-not-escape-the-single-quote-risky/149667/3 "2008-09-27T23:02:53Z")

</div>

Hi,

At Sun, 28 Sep 2008 04:28:45 +0900,  
SpringFlowers AutumnMoon wrote in [ruby-talk:316193]:

> the single quote is not converted…

I guess that is because the character entity reference of  
single quote isn’t defined in HTML.

> we usually use double quote but sometimes we use single quote like  
> somebody can write
> 
> puts “”

You can use other delimiters than double quote and single quote.

puts %[]

or heredoc.

puts \<\<HIDDEN  
  
HIDDEN

Heredocs include the last newline, but no differences to use  
with #puts.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 28, 2008, 4:19am UTC](https://www.ruby-forum.com/t/h-or-html-escape-not-escape-the-single-quote-risky/149667/4 "2008-09-28T04:19:43Z")

</div>

Andreas S. wrote:

> > 
> 
> Just don’t, it’s not correct HTML.

really – i thought HTML 4.01 allows using either double or single  
quote?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 30, 2008, 4:51am UTC](https://www.ruby-forum.com/t/h-or-html-escape-not-escape-the-single-quote-risky/149667/5 "2008-09-30T04:51:21Z")

</div>

On Sep 27, 10:19 pm, SpringFlowers AutumnMoon \<rails-mailing-  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

> Andreas S. wrote:
> 
> > >
> 
> > Just don’t, it’s not correct HTML.
> 
> really – i thought HTML 4.01 allows using either double or single  
> quote?

perhaps, but if h() doesn’t escape single quotes then you’d want to  
avoid using single quotes as the attribute value delimiter.
