-
Notifications
You must be signed in to change notification settings - Fork 160
Sanitize dartdoc's HTML output. #5181
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:html/dom.dart'; | ||
import 'package:html/parser.dart' as html_parser; | ||
|
||
class SanitizerResult { | ||
final bool passed; | ||
final String contentHtml; | ||
final List<String> removed; | ||
|
||
SanitizerResult(this.passed, this.contentHtml, this.removed); | ||
} | ||
|
||
/// Removes unsafe elements and attributes from the output of dartdoc. | ||
SanitizerResult sanitizeDartdocHtml(String input, int directoryLevel) { | ||
final parsed = html_parser.parse(input); | ||
final removed = <String>[]; | ||
|
||
void visit(Element elem) { | ||
// remove elements based on tag and position | ||
final tag = elem.localName?.toLowerCase() ?? ''; | ||
if (tag == 'iframe') { | ||
removed.add('iframe src="${elem.attributes['src']}"'); | ||
elem.remove(); | ||
return; | ||
} | ||
|
||
// allow some <script> and remove everything else | ||
if (tag == 'script') { | ||
final src = elem.attributes['src'] ?? ''; | ||
final prefix = '../' * directoryLevel; | ||
final allowed = [ | ||
'${prefix}static-assets/highlight.pack.js?v1', | ||
'${prefix}static-assets/script.js?v1', | ||
]; | ||
if (!allowed.contains(src)) { | ||
removed.add('script src="${elem.attributes['src']}"'); | ||
elem.remove(); | ||
return; | ||
} | ||
} | ||
|
||
// remove on* attributes | ||
elem.attributes.removeWhere((a, value) { | ||
final key = a is String ? a : (a as AttributeName).name; | ||
if (key.toLowerCase().startsWith('on')) { | ||
removed.add('$tag $key'); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
// visit children | ||
for (final c in elem.children) { | ||
visit(c); | ||
} | ||
} | ||
|
||
visit(parsed.documentElement!); | ||
return SanitizerResult( | ||
removed.isEmpty, | ||
removed.isNotEmpty ? parsed.outerHtml : input, | ||
removed, | ||
); | ||
} |
This file contains hidden or 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 hidden or 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,63 @@ | ||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:pub_dev/dartdoc/dartdoc_sanitizer.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'customization_test.dart'; | ||
|
||
void main() { | ||
test('accept golden files', () async { | ||
final files = Directory(goldenDir) | ||
.listSync() | ||
.whereType<File>() | ||
.where((f) => f.path.endsWith('.html')) | ||
.where((f) => !f.path.endsWith('.out.html')) | ||
// dygraph templates are not updated to the latest dartdoc | ||
.where((f) => !f.path.contains('/dygraph_')) | ||
.toList(); | ||
|
||
int _level(String path) { | ||
if (path.endsWith('_index.html')) return 0; | ||
if (path.endsWith('_class.html')) return 1; | ||
if (path.endsWith('_field.html')) return 2; | ||
if (path.endsWith('_constructor.html')) return 2; | ||
return 1; | ||
} | ||
|
||
for (final f in files) { | ||
final level = _level(f.path); | ||
final rs = sanitizeDartdocHtml(await f.readAsString(), level); | ||
expect(rs.passed, isTrue, reason: f.path); | ||
} | ||
}); | ||
|
||
test('unauthorized <script> is removed', () { | ||
final rs = sanitizeDartdocHtml( | ||
'<html><body><p>1</p><script src="x.js"></script><p>2</p></body></html>', | ||
0); | ||
expect(rs.passed, false); | ||
expect(rs.contentHtml, | ||
'<html><head></head><body><p>1</p><p>2</p></body></html>'); | ||
}); | ||
|
||
test('onclick="" is removed', () { | ||
final rs = sanitizeDartdocHtml( | ||
'<html><body><p>1</p><div onclick="window.alert(\'x\');"></div><p>2</p></html>', | ||
0); | ||
expect(rs.passed, false); | ||
expect(rs.contentHtml, | ||
'<html><head></head><body><p>1</p><div></div><p>2</p></body></html>'); | ||
}); | ||
|
||
test('<iframe> is removed', () { | ||
final rs = sanitizeDartdocHtml( | ||
'<html><body><p>1</p><iframe src="x.html"></iframe><p>2</p></html>', 0); | ||
expect(rs.passed, false); | ||
expect(rs.contentHtml, | ||
'<html><head></head><body><p>1</p><p>2</p></body></html>'); | ||
}); | ||
} |
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.
I don't think this is sufficient, we need to allow list tags, attributes and possibly even validate attribute values.