-
Notifications
You must be signed in to change notification settings - Fork 86
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
Extend sanitize #171
Open
kascote
wants to merge
3
commits into
google:master
Choose a base branch
from
kascote:extend-sanitize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Extend sanitize #171
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -200,6 +200,47 @@ final _elementAttributeValidators = | |
'Q': _citeAttributeValidator, | ||
}; | ||
|
||
/// Callback function used to check wich tags are allowed | ||
/// The function will return true if the tag is allowed of false if not. | ||
typedef AllowTagCB = bool Function(String tag); | ||
|
||
/// Callback function used to check wich attributes are allowed | ||
/// The function will return an [AllowAttributeResponse] object | ||
/// with the result of the check. | ||
typedef AllowAttributeCB = AllowAttributeResponse Function( | ||
String tag, String attribute, String value); | ||
|
||
/// Possible outcome actions of [AllowAttributeResponse] | ||
enum ResponseAction { | ||
unchanged, | ||
edit, | ||
remove, | ||
} | ||
|
||
/// Response object returned by [AllowAttributeCB] | ||
/// The [action] attribute determine the action to take | ||
/// and can be [unchanged], [edit] or [remove]. | ||
/// In case of [edit], the [value] field can be used to | ||
/// change the content of the attribute. | ||
class AllowAttributeResponse { | ||
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. What examples are there of attributes where this makes sense? |
||
ResponseAction action; | ||
String value; | ||
|
||
AllowAttributeResponse(this.action, [this.value = '']); | ||
|
||
/// Factory function for unchaged action | ||
factory AllowAttributeResponse.unchanged() => | ||
AllowAttributeResponse(ResponseAction.unchanged); | ||
|
||
/// Factory function for edit action | ||
factory AllowAttributeResponse.edit(String value) => | ||
AllowAttributeResponse(ResponseAction.edit, value); | ||
|
||
/// Factory function for remove action | ||
factory AllowAttributeResponse.remove() => | ||
AllowAttributeResponse(ResponseAction.remove); | ||
} | ||
|
||
/// An implementation of [html.NodeValidator] that only allows sane HTML tags | ||
/// and attributes protecting against XSS. | ||
/// | ||
|
@@ -213,38 +254,80 @@ class SaneHtmlValidator { | |
final bool Function(String)? allowClassName; | ||
final Iterable<String>? Function(String)? addLinkRel; | ||
|
||
/// Callback function to check for allowed tags | ||
final AllowTagCB? allowTag; | ||
|
||
/// Callback function to check for allowed attributes | ||
final AllowAttributeCB? allowAttribute; | ||
|
||
late AllowTagCB _allowTagFn; | ||
late AllowAttributeCB _allowAttributeFn; | ||
|
||
SaneHtmlValidator({ | ||
required this.allowElementId, | ||
required this.allowClassName, | ||
required this.addLinkRel, | ||
}); | ||
required this.allowTag, | ||
required this.allowAttribute, | ||
}) { | ||
_allowTagFn = allowTag ?? _defaultAllowedElements; | ||
_allowAttributeFn = allowAttribute ?? _defaultAllowedAttributes; | ||
} | ||
|
||
String sanitize(String htmlString) { | ||
final root = html_parser.parseFragment(htmlString); | ||
_sanitize(root); | ||
return root.outerHtml; | ||
} | ||
|
||
bool _defaultAllowedElements(String tagName) => | ||
_allowedElements.contains(tagName); | ||
|
||
AllowAttributeResponse _defaultAllowedAttributes( | ||
String tagName, String attrName, String attrValue) { | ||
if (attrName == 'id') { | ||
if (allowElementId == null) return AllowAttributeResponse.remove(); | ||
return allowElementId!(attrValue) | ||
? AllowAttributeResponse.unchanged() | ||
: AllowAttributeResponse.remove(); | ||
} | ||
if (attrName == 'class') { | ||
if (allowClassName == null) return AllowAttributeResponse.remove(); | ||
final klasses = attrValue.split(' '); | ||
klasses.removeWhere((cn) => !allowClassName!(cn)); | ||
return klasses.isEmpty | ||
? AllowAttributeResponse.remove() | ||
: AllowAttributeResponse.edit(klasses.join(' ')); | ||
} | ||
return _isAttributeAllowed(tagName, attrName, attrValue) | ||
? AllowAttributeResponse.unchanged() | ||
: AllowAttributeResponse.remove(); | ||
} | ||
|
||
void _sanitize(Node node) { | ||
if (node is Element) { | ||
final tagName = node.localName!.toUpperCase(); | ||
if (!_allowedElements.contains(tagName)) { | ||
if (!_allowTagFn(tagName)) { | ||
node.remove(); | ||
return; | ||
} | ||
node.attributes.removeWhere((k, v) { | ||
final attrName = k.toString(); | ||
if (attrName == 'id') { | ||
return allowElementId == null || !allowElementId!(v); | ||
} | ||
if (attrName == 'class') { | ||
if (allowClassName == null) return true; | ||
node.classes.removeWhere((cn) => !allowClassName!(cn)); | ||
return node.classes.isEmpty; | ||
final rc = _allowAttributeFn(tagName, attrName, v); | ||
if (rc.action == ResponseAction.remove) return true; | ||
if (rc.action == ResponseAction.edit) { | ||
if (attrName == 'class') { | ||
final klasses = rc.value.split(' '); | ||
node.classes.removeWhere((klass) => !klasses.contains(klass)); | ||
} else { | ||
node.attributes[k] = rc.value; | ||
} | ||
return false; | ||
} | ||
return !_isAttributeAllowed(tagName, attrName, v); | ||
return false; | ||
}); | ||
if (tagName == 'A') { | ||
// When use a custom tag list, the default rule for anchor/rel will not work | ||
if ((allowTag == null) && (tagName == 'A')) { | ||
final href = node.attributes['href']; | ||
if (href != null && addLinkRel != null) { | ||
final rels = addLinkRel!(href); | ||
|
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
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.
This doesn't need a typedef... also why give a callback... why not just make it a list of string?