Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails not handling links with & correctly

#162

Certain browsers including Firefox does not convert links with & amp ; in it to &. That causes problems further in rails applications, where params gets created using parse_query_parameters method.

The query strings with & amp ; get parsed as key part e.g. query sting with & amp ;name=david would get converted to params as params[amp;name] instead of params[:name]. Ideally browser should convert & amp ; to & whenever it parses query strings. But that is not the case for current browsers.

This could be serious issue. The rails helper page.redirect_to outputs window.location links to have & amp ; in it. So page.redirect_to having additional params(other than controller, action, id) simply doesn't work.

I have attached related fix diff file.

Reported by Anil Wadghule · May 10th, 2008 @ 02:24 PM

State: invalid
Milestone: 3.0.2
Assigned to: Pratik Pratik
Importance: Low

Activity

  1. Stephen Celis
    Stephen Celis
    • Title changed from [PATCH] Rails not handling links with & correctly to Rails not handling links with & correctly

    Please use the "patch" tag now rather than preface with "[PATCH]"

    May 10th, 2008 @ 05:19 PM

  2. blj
    blj

    I am pretty sure this is invalid.

    May 11th, 2008 @ 02:06 AM

  3. Pratik
    Pratik

    Why can't the fix be in page.redirect_to ?

    May 11th, 2008 @ 08:40 PM

  4. Pratik
    Pratik
    • Assigned user changed from DHH to Pratik

    May 11th, 2008 @ 08:40 PM

  5. Daniel Morrison
    Daniel Morrison

    -1

    & shouldn't be in your URLs, and if it is, it should be there for a reason.

    Rails is acting appropriately.

    If there's a bug in page.redirect_to, it should be fixed there, not here.

    May 13th, 2008 @ 03:23 AM

  6. Pratik
    Pratik
    • State changed from new to incomplete

    May 13th, 2008 @ 10:46 AM

  7. Anil Wadghule
    Anil Wadghule

    okay, I will fix page.redirect_to. The url passed to it should not be html escaped as it is directly given to window.location, which is browser location bar.

    May 18th, 2008 @ 01:32 PM

  8. Mike Breen
    Mike Breen
    • Tag set to actionpack, bug, edge, patch, tests

    Here's a patch that fixes this in page.redirect.

    March 26th, 2009 @ 01:49 PM

  9. Pratik
    Pratik
    • State changed from incomplete to open

    March 26th, 2009 @ 01:56 PM

  10. joshsz
    joshsz

    The patch works for me. I'm unsure of the utility of this though. I think you're probably doing something wrong if you have to write out urls with & amp ;, but I'd have to examine your specific case to make a real judgement.

    April 24th, 2010 @ 05:33 PM

  11. Rizwan Reza
    Rizwan Reza
    • Tag changed from actionpack, bug, edge, patch, tests to actionpack, bug, bugmash, edge, patch, tests

    May 15th, 2010 @ 03:38 PM

  12. Rizwan Reza
    Rizwan Reza
    • Milestone cleared.

    +1 verified

    I have attached a patch without trailing whitespace error. It preserves Mike's authorship.

    May 15th, 2010 @ 03:38 PM

  13. Rizwan Reza
  14. Rizwan Reza
    Rizwan Reza
    • State changed from open to invalid

    May 15th, 2010 @ 03:44 PM

  15. Xavier Noria
    Xavier Noria
    • State changed from invalid to open

    Just a clarification.

    & per se does not belong to URLs. You output & when the URL lives inside of an HTML page. Reason is attribute values have to be HTML-escaped as content is.

    So a URL should be HTML-escaped if it goes into a href attribute, and should not if it goes in a HTTP header, belongs to the body of a text mail, or any other context.

    You should escape also quotes and angles, except you won't see those in a URL, so in practice the simple gsub may do (not 100% sure, but I have no counterxample by now).

    I think it would be better to include the ampersand in the regexp though, since the string "amp;" may appear by itself, as in http://www.example.com?foo=amp;bar=woo. And to add a test that asserts such a query string remains untouched.

    May 15th, 2010 @ 03:52 PM

  16. Xavier Noria
    Xavier Noria

    Mike, who is passing a location argument that it is HTML-escaped? HTML-escaping should only happen in the view layer, it is suspicious that location is escaped at all. Better understand that before we consider this patch.

    May 15th, 2010 @ 04:04 PM

  17. Santiago Pastorino
    Santiago Pastorino

    Yeah i was about to said the same Xavier said if the patch is going to be valid it's safer to do gsub('&', '&')

    May 15th, 2010 @ 05:32 PM

  18. Santiago Pastorino
    Santiago Pastorino

    Sorry i didn't check the formatting. Hehe do this gsub('&' + 'amp;', '&') but without the +, all toghether

    May 15th, 2010 @ 05:35 PM

  19. Anil Wadghule
    Anil Wadghule

    I've attached corrected patch.

    Basically it replaces "& amp ;"(without spaces) character with the & character. "& amp ;" is valid character entity reference for &. See 'Character Entity Reference' section in http://www.w3.org/TR/REC-html40/charset.html

    May 15th, 2010 @ 08:26 PM

  20. Anil Wadghule
    Anil Wadghule

    Attached is a app which demonstrates this issue.

    See the pastie for the behavior before patch and after patch

    http://pastie.org/961893

    To answer Xavier, I remember earlier Rails versions used to generate urls with & amp ; in it, which triggered this ticket. But looks like it is not the case now.

    And when such HTML-escaped urls passed to page.redirect_to helper which basically has just "window.location.href = url". It would just pass wrong HTML-escaped link as it is.

    That's why this patch looks necessary.

    PS - Rails has html_escape defined in rails/activesupport/lib/active_support/core_ext/string/output_safety.rb
    I think if we can have html_unescape, we can use it to HTML unescape string urls in redirect_to method.

    Thouhts?

    May 15th, 2010 @ 09:58 PM

  21. José Valim
    José Valim
    • State changed from open to invalid

    Anil, thanks for the patch but the question is: how, in the first place, the url ended up with "&"? Rails urls are properly generated so I cannot see an use case for automatically removing &.

    May 15th, 2010 @ 10:29 PM

  22. Rizwan Reza
    Rizwan Reza
    • Tag changed from actionpack, bug, bugmash, edge, patch, tests to actionpack, bug, edge, patch, tests

    May 16th, 2010 @ 02:10 AM

  23. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM

  24. hgj
    hgj

    thanks you very much for your information

    April 18th, 2011 @ 07:43 AM