-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Escape characters to prevent xss sinks * Escape the value of html attributes * Differentiate the character escaping of an attribute and an element body * Secure the components as well * Add the option for the autoescaping to the vapor provider With the option, people can decide if they want it or not.
- Loading branch information
1 parent
5990bb3
commit 676792d
Showing
11 changed files
with
320 additions
and
42 deletions.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public class Security { | ||
|
||
public var autoEscaping: Bool | ||
|
||
public init() { | ||
|
||
self.autoEscaping = true | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
Sources/HTMLKitComponents/Extensions/Components+String.swift
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
extension String { | ||
|
||
internal func escape() -> String { | ||
|
||
return self.replacingOccurrences(of: "&", with: "&") | ||
.replacingOccurrences(of: "<", with: "<") | ||
.replacingOccurrences(of: ">", with: ">") | ||
.replacingOccurrences(of: "\"", with: """) | ||
.replacingOccurrences(of: "'", with: "'") | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import HTMLKit | ||
import HTMLKitComponents | ||
import XCTest | ||
|
||
final class SecurityTests: XCTestCase { | ||
|
||
struct TestView: View { | ||
|
||
@ContentBuilder<Content> var body: Content | ||
} | ||
|
||
var renderer = Renderer() | ||
|
||
func testEncodingAttributeContext() throws { | ||
|
||
let attack = "\" onclick=\"alert(1);\"" | ||
|
||
let view = TestView { | ||
Button(role: .button) { | ||
"Show" | ||
} | ||
.tag(attack) | ||
} | ||
|
||
XCTAssertEqual(try renderer.render(view: view), | ||
""" | ||
<button type="button" class="button" id="" onclick="alert(1);"">Show</button> | ||
""" | ||
) | ||
} | ||
|
||
func testEncodingActionContext() throws { | ||
|
||
let attack = "test').appendTo(''); $('#test" | ||
|
||
let view = TestView { | ||
Button(role: .button) { | ||
"Show" | ||
} | ||
.tag("sender") | ||
.onClick { button in | ||
button.show(attack) | ||
} | ||
|
||
} | ||
|
||
XCTAssertEqual(try renderer.render(view: view), | ||
""" | ||
<button type="button" class="button" id="sender">Show</button>\ | ||
<script>\ | ||
$('#sender').onClick(function(){\ | ||
$('#test').appendTo(''); $('#test').show();\ | ||
});\ | ||
</script> | ||
""" | ||
) | ||
} | ||
|
||
func testEncodingCssContext() throws { | ||
|
||
let attack = "test\" style=\"property: unsafe\"" | ||
|
||
let view = TestView { | ||
Text { | ||
"Text" | ||
} | ||
.backgroundColor(.custom(attack)) | ||
} | ||
|
||
XCTAssertEqual(try renderer.render(view: view), | ||
""" | ||
<p class="text background:test" style="property: unsafe"">Text</p> | ||
""" | ||
) | ||
} | ||
} | ||
|
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
Oops, something went wrong.