This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Don't use dangerous HTML in Linkify #10
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7354bf3
fix(Linkify): escape HTML characters
e6c82f4
Don't set HTML dangerously
6ad9d3b
Fix test and copy-pasta
297520f
Improve link parsing; Update docs to showcase more complex functionality
kylealwyn 0252a69
Fix doc formatting
kylealwyn 98742c1
Remove unnecessary filter
kylealwyn ab1637c
Add linkStyle test
kylealwyn 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { renderWithTheme } from '../../test/utils'; | ||
import Linkify from './Linkify'; | ||
|
||
describe('Linkify', () => { | ||
test('converts links to anchor tags', () => { | ||
const component = renderWithTheme(<Linkify>Hello! https://google.com is a cool site.</Linkify>); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); | ||
|
||
test('escapes HTML entities', () => { | ||
const component = renderWithTheme(<Linkify>{`<span onmouseover=alert('XSS')></span>`}</Linkify>); | ||
|
||
expect(component).toMatchSnapshot(); | ||
}); | ||
}); |
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,37 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Linkify converts links to anchor tags 1`] = ` | ||
.c0 a { | ||
color: inherit; | ||
-webkit-text-decoration: underline; | ||
text-decoration: underline; | ||
} | ||
|
||
<span | ||
className="c0" | ||
dangerouslySetInnerHTML={ | ||
Object { | ||
"__html": "Hello! <a target=\\"_blank\\" rel=\\"noopener noreferrer\\" href=\\"https://google.com\\">https://google.com</a> is a cool site.", | ||
} | ||
} | ||
style={Object {}} | ||
/> | ||
`; | ||
|
||
exports[`Linkify escapes HTML entities 1`] = ` | ||
.c0 a { | ||
color: inherit; | ||
-webkit-text-decoration: underline; | ||
text-decoration: underline; | ||
} | ||
|
||
<span | ||
className="c0" | ||
dangerouslySetInnerHTML={ | ||
Object { | ||
"__html": "<span onmouseover=alert('XSS')></span>", | ||
} | ||
} | ||
style={Object {}} | ||
/> | ||
`; |
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 think you can do this without
dangerouslySetInnerHTML
if you iterate through regex matches and return a[React.Component]
. see https://github.com/tasti/react-linkify/blob/master/src/components/Linkify.jsx#L75 (could also use that library directly, up to you)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.
While I think using a library like this would be ideal, our use case for this component usually resolves parsing strings as opposed to React elements. So, for example, we currently use the component like this:
as opposed to like this.
While I definitely do agree about with the general notion of making code as general and extensible as possible, I think in this case I would hold off on addressing edge cases until and if we get to them.
What do you think? @choochootrain 😄
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.
discussed offline-but-actually-online-because-slack - i think if we defer html escaping to react then we can be p confident that future bugs will have a lot more eyeballs on them :)