string.link does not escape url

RESOLVED FIXED in mozilla17

Status

()

Core
JavaScript Engine
--
minor
RESOLVED FIXED
11 years ago
4 years ago

People

(Reporter: Jesse Ruderman, Assigned: Ms2ger)

Tracking

({dev-doc-complete, sec-want, testcase})

Trunk
mozilla17
dev-doc-complete, sec-want, testcase
Points:
---
Bug Flags:
wanted1.9.1 +

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [sg:want])

Attachments

(3 attachments)

(Reporter)

Description

11 years ago
js> "foo".link("http://www.google.com/search?q=foo&num=100")
<a href="http://www.google.com/search?q=foo&num=100">foo</a>

In HTML, & in links needs to be escaped as &amp;
Assignee: general → jwalden+bmo
(Reporter)

Comment 1

11 years ago
This is worse than I thought: the double-quote character (") isn't escaped either.  This caused an XSS hole in mozilla.com (bug 357750), where the developer assumed that String.link was an API for creating a link from any link text and URL.  I think we should meet that assumption (rather than telling developers not to use String.link or to escape or sanitize text passed to it), but we'd need to get other browser vendors to fix their String.link implementations too for that problem to really go away.
Assignee: jwalden+bmo → general

Updated

9 years ago
Duplicate of this bug: 437618
As pointed out in bug 437618 the problem is not limited to .link(), it's all the HTML-helper string methods. e.g.

> javascript:"foo".anchor("hello\"><script>alert(1)</script>")
> javascript:"foo".fontcolor("hello\"><script>alert(1)</script>")
> javascript:"foo".fontsize("hello\"><script>alert(1)</script>")
> javascript:"foo".link("hello\"><script>alert(1)</script>")
Blocks: 301375
Flags: wanted1.9.1?
Whiteboard: [sg:want]

Comment 4

9 years ago
It's not clear that we can fix this, but we'll mark it for investigation.
Flags: wanted1.9.1? → wanted1.9.1+
OS: Mac OS X → All
Hardware: Macintosh → All
Duplicate of this bug: 536837
Duplicate of this bug: 649818
Keywords: sec-want
(Assignee)

Updated

5 years ago
Assignee: general → Ms2ger

Comment 7

5 years ago
Here’s a spec for the non-standard string extensions; an attempt to get
browsers to align their implementations:
http://mathias.html5.org/specs/javascript/#escapeattributevalue

It only requires escaping U+0022 QUOTATION MARK as &quot; (not U+0026
AMPERSAND as &amp;) — see note below for the reasoning behind this.

Tests: http://mathias.html5.org/tests/javascript/string/

(In reply to Robert Sayre from comment #4)
> It's not clear that we can fix this, but we'll mark it for investigation.

Note that WebKit does escape U+0022 QUOTATION MARK as &quot;. It also escapes
U+0027 APOSTROPHE as &#039;, although that seems pointless. It doesn’t escape
U+0026 AMPERSAND, though. No browser does.

As it doesn’t seem to cause any compatibility problems for WebKit, it seems
possible to fix the &quot; issue. I’m afraid the &amp; “issue” is harder to
fix, as there’s no precedent.

Comment 8

5 years ago
I agree we shouldn't change &. There may be sites that "do the right thing" and escape both & and " before using these methods, that would break if we start escaping &. It also means it is harder for sites to plug their XSS hole for legacy browsers if new browsers start escaping &.

Escaping " seems like a good thing to do, IMHO.

Comment 9

5 years ago
My previous comment (comment 7) should’ve said V8 instead of WebKit.

For some reason I’m not allowed to add these URLs to the “See Also” list, so here goes:

* Safari/JavaScriptCore: https://bugs.webkit.org/show_bug.cgi?id=90667
* IE/Chakra: https://connect.microsoft.com/IE/feedback/details/752391
* Opera/Carakan: https://bugs.opera.com/browse/CORE-47348 (DSK-369206/CORE-47348)
(Assignee)

Comment 10

5 years ago
Created attachment 639678 [details] [diff] [review]
Part a: Use StringBuffer

For nice code.
Attachment #639678 - Flags: review?(jorendorff)
(Assignee)

Comment 11

5 years ago
Created attachment 639680 [details] [diff] [review]
Part b: Get rid of some strlen() calls

For ever so slightly faster code
Attachment #639680 - Flags: review?(jorendorff)
(Assignee)

Comment 12

5 years ago
Created attachment 639681 [details] [diff] [review]
Part c: Escape quotes

For better conforming code.
Attachment #639681 - Flags: review?(jorendorff)

Comment 13

5 years ago
See also: https://bugs.ecmascript.org/show_bug.cgi?id=406
Comment on attachment 639680 [details] [diff] [review]
Part b: Get rid of some strlen() calls

We shouldn't optimize stuff like this. It's better to do the slow thing than to have the compiler spit out a dozen instantiations of a template.

If you really want to eliminate the strlen calls, make tagify_impl take beginLen and endLen as arguments (it can assert that those arguments are actually equal to strlen(begin) and strlen(end)), then make an inline function template that uses this trick to pass them in. Then measure the perf difference. This doesn't seem worth the effort to me though.
Attachment #639680 - Flags: review?(jorendorff)
Comment on attachment 639678 [details] [diff] [review]
Part a: Use StringBuffer

Review of attachment 639678 [details] [diff] [review]:
-----------------------------------------------------------------

r=me with or without the changes.

::: js/src/jsstr.cpp
@@ +2791,1 @@
>          taglen += 2 + parlen + 1;                       /* '="param"' */

Feel free to eliminate the temporary entirely (and then the curly braces here).

@@ +2799,5 @@
>          return false;
> +
> +    sb.infallibleAppend('<');
> +
> +    MOZ_ALWAYS_TRUE(sb.appendInflated(begin, beglen));

Feel free to add methods to vm/StringBuffer.h:
    inline void infallibleAppendInflated(const char *cstr, size_t len);
    inline void infallibleAppend(JSLinearString *str);
that do this assertion.
Attachment #639678 - Flags: review?(jorendorff) → review+
Attachment #639681 - Flags: review?(jorendorff) → review+
(Assignee)

Comment 16

5 years ago
https://hg.mozilla.org/mozilla-central/rev/75b38c234582
https://hg.mozilla.org/mozilla-central/rev/355e0e643065
Status: NEW → RESOLVED
Last Resolved: 5 years ago
Keywords: dev-doc-needed
Resolution: --- → FIXED
Target Milestone: --- → mozilla17
Backed out with the mass tree revert to get rid of the OS X M5 orange:
https://hg.mozilla.org/mozilla-central/rev/c801b99d726f
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
https://hg.mozilla.org/integration/mozilla-inbound/rev/1fc4a72e45f7
https://hg.mozilla.org/integration/mozilla-inbound/rev/65e5f28b578c
https://hg.mozilla.org/mozilla-central/rev/1fc4a72e45f7
https://hg.mozilla.org/mozilla-central/rev/65e5f28b578c
Status: REOPENED → RESOLVED
Last Resolved: 5 years ago5 years ago
Resolution: --- → FIXED
Mentioned on 
https://developer.mozilla.org/en-US/docs/Firefox_17_for_developers
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/link
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/anchor
Keywords: dev-doc-needed → dev-doc-complete
You need to log in before you can comment on or make changes to this bug.