-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Drop XSS escaping because it should be aside HTML escaping. #4555
Closed
+98
−33
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8148350
HTML.escape - Switch to Rack::Utils.escape_html behavior by default. …
akzhan 4c731e8
Drop XSS escaping because it should be aside HTML escaping.
akzhan b25f2a8
oops, formatting
akzhan 7728d37
Follow @straight-shoota to simplify HTML.escape API.
akzhan 26c6109
Escaping of forward slash is obsolete practice.
akzhan 1a6f502
HTML.escape_javascript with specs.
akzhan efc4df2
doc. update
akzhan cb86d0e
typo in doc, thanks @straight-shoota
akzhan 86c0872
reword example descriptions, thanks @straight-shoota
akzhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
# Handles encoding and decoding of HTML entities. | ||
module HTML | ||
SUBSTITUTIONS = { | ||
'!' => "!", | ||
'"' => """, | ||
'$' => "$", | ||
'%' => "%", | ||
'&' => "&", | ||
'\'' => "'", | ||
'(' => "(", | ||
')' => ")", | ||
'=' => "=", | ||
'>' => ">", | ||
'<' => "<", | ||
'+' => "+", | ||
'@' => "@", | ||
'[' => "[", | ||
']' => "]", | ||
'`' => "`", | ||
'{' => "{", | ||
'}' => "}", | ||
'\u{a0}' => " ", | ||
# `HTML.escape` escaping mode. | ||
ESCAPE_SUBST = { | ||
# Escapes '&', '<' and '>' chars. | ||
# | ||
# Like PHP htmlspecialchars (with ENT_NOQUOTES), Python cgi.escape, W3C recommendation. | ||
false => { | ||
'&' => "&", | ||
'<' => "<", | ||
'>' => ">", | ||
}, | ||
# Escapes '&', '<' and '>', '"' and '\'' chars. | ||
# | ||
# Like Ruby CGI.escape, PHP htmlspecialchars (with ENT_QUOTES), Rack::Utils.escape_html. | ||
true => { | ||
'&' => "&", | ||
'"' => """, | ||
'<' => "<", | ||
'>' => ">", | ||
'\'' => "", | ||
}, | ||
} | ||
ESCAPE_JAVASCRIPT_SUBST = { | ||
'\'' => "\\'", | ||
'"' => "\\\"", | ||
'\\' => "\\\\", | ||
'\u2028' => "
", | ||
'\u2029' => "
", | ||
'\n' => "\\n", | ||
'\r' => "\\n", | ||
} | ||
|
||
# Encodes a string with HTML entity substitutions. | ||
|
@@ -29,8 +38,8 @@ module HTML | |
# | ||
# HTML.escape("Crystal & You") # => "Crystal & You" | ||
# ``` | ||
def self.escape(string : String) : String | ||
string.gsub(SUBSTITUTIONS) | ||
def self.escape(string : String, escape_quotes : Bool = true) : String | ||
string.gsub(ESCAPE_SUBST[escape_quotes]) | ||
end | ||
|
||
# Encodes a string to HTML, but writes to the `IO` instance provided. | ||
|
@@ -40,9 +49,45 @@ module HTML | |
# HTML.escape("Crystal & You", io) # => nil | ||
# io.to_s # => "Crystal & You" | ||
# ``` | ||
def self.escape(string : String, io : IO) | ||
def self.escape(string : String, io : IO, escape_quotes : Bool = true) : Nil | ||
subst = ESCAPE_SUBST[escape_quotes] | ||
string.each_char do |char| | ||
io << subst.fetch(char, char) | ||
end | ||
end | ||
|
||
# Encodes a string with JavaScript escaping substitutions. | ||
# | ||
# ``` | ||
# require "html" | ||
# | ||
# HTML.escape_javascript("</crystal> \u2028") # => "<\\/crystal> 
" | ||
# ``` | ||
def self.escape_javascript(string : String) : String | ||
string.gsub("\r\n", "\n").gsub(ESCAPE_JAVASCRIPT_SUBST).gsub("</", "<\\/") | ||
end | ||
|
||
# Encodes a string with JavaScript escaping, but writes to the `IO` instance provided. | ||
# | ||
# ``` | ||
# io = IO::Memory.new | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be better to use a string builder as example. |
||
# HTML.escape_javascript("</crystal> \u2028", io) # => nil | ||
# io.to_s # => "<\\/crystal> 
" | ||
# ``` | ||
def self.escape_javascript(string : String, io : IO) : Nil | ||
previous_char = '\0' | ||
string.each_char do |char| | ||
io << SUBSTITUTIONS.fetch(char, char) | ||
if previous_char == '\r' && char == '\n' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this would be better as a case
when previous_char == '\r' && char == '\n'
when previous_char == '<' && char == '/'
io << '\\' << '/'
else
io << ESCAPE_JAVASCRIPT_SUBST.fetch(char, char)
end
previous_char = char |
||
previous_char = '\n' | ||
next | ||
end | ||
if previous_char == '<' && char == '/' | ||
previous_char = '/' | ||
io << '\\' << '/' | ||
next | ||
end | ||
io << ESCAPE_JAVASCRIPT_SUBST.fetch(char, char) | ||
previous_char = char | ||
end | ||
end | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to list all character substitutions explicitly, so you don't need to look at the source code to figure out what exactly gets escaped and why.
Suggestion:
The other methods should have similar explanations.