Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape URL to render HTML #1131

Closed
wants to merge 1 commit into from
Closed

Escape URL to render HTML #1131

wants to merge 1 commit into from

Conversation

yous
Copy link
Contributor

@yous yous commented Dec 5, 2016

DIR_FILE have <a href='%s'>, so single quotes in URL should be escaped.

@yous
Copy link
Contributor Author

yous commented Dec 7, 2016

You can simply reproduce this problem.

mkdir test
mkdir test/foo\'bar
touch test/foo\'bar/omg\'omg.txt

config.ru:

run Rack::Directory.new('test')
rackup config.ru

When I click the link to foo'bar, it actually goes to /foo.

screenshot 2016-12-07 10 09 28

@tonytonyjan
Copy link
Contributor

It's caused by the changed behavior of Rack::Utils#escape_path:

# rack 1.6.5
Rack::Utils.escape_path(?') # => "%27"
# rack master
Rack::Utils.escape_path(?') # => "'"

Why not patch #escape_path instead, I wonder.

@yous
Copy link
Contributor Author

yous commented Dec 9, 2016

That change was came from the patch for #265. Seeing #265 (comment), ' is a valid character for path.

And actually this patch is needed only because DIR_FILE uses <a href='%s'>. If we change this to <a href="%s">, then we don't need gsub as ::URI::DEFAULT_PARSER.escape replaces " to %22. Maybe changing DIR_FILE is more proper solution. How do you think?

@tonytonyjan
Copy link
Contributor

I found it's been URI escaped here, but not yet HTML escaped.

Since the #DIR_FILE_escape will return contents (url, basename, size, type, mtime) which are going to be used to render HTML here, they should be all HTML escaped already.

In my opinion, this way can be more decent:

def each
  # ...
  listings = files.map{|f| DIR_FILE % DIR_FILE_escape(f) }*"\n"
  # ...
end
# ...
def DIR_FILE_escape(html)
  html.map { |e| Utils.escape_html(e) }
end

@yous yous changed the title Escape single quotes in URL for DIR_FILE Escape URL to render HTML Dec 9, 2016
@yous
Copy link
Contributor Author

yous commented Dec 9, 2016

Yes, I think that's more proper. I just updated.

@yous
Copy link
Contributor Author

yous commented Aug 25, 2019

Any update on this? I can reproduce this with rack 2.0.7.

@jeremyevans
Copy link
Contributor

This change looks good to me. CI failures look unrelated. I'll push the changes as a new PR and if CI passes I'll merge it.

@jeremyevans
Copy link
Contributor

Merged in #1524

@yous
Copy link
Contributor Author

yous commented Jan 27, 2020

Thanks!

@yous yous deleted the single-quote-url branch January 27, 2020 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants