Skip to content

Commit

Permalink
improve sanitization logic and dev warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Oct 21, 2018
1 parent e2b38b6 commit 9c6c782
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
39 changes: 39 additions & 0 deletions index.compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,8 @@ describe('links', () => {
});

it('should sanitize links containing JS expressions', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});

render(compiler('[foo](javascript:doSomethingBad)'));

expect(root.innerHTML).toMatchInlineSnapshot(`
Expand All @@ -811,9 +813,45 @@ describe('links', () => {
</a>
`);

expect(console.warn).toHaveBeenCalled();
});

it('should sanitize links containing encoded JS expressions', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});

render(compiler('[foo](javascript%3AdoSomethingBad)'));

expect(root.innerHTML).toMatchInlineSnapshot(`
<a data-reactroot>
foo
</a>
`);

expect(console.warn).toHaveBeenCalled();
});

it('should sanitize links containing padded JS expressions', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});

render(compiler('[foo]( javascript%3AdoSomethingBad)'));

expect(root.innerHTML).toMatchInlineSnapshot(`
<a data-reactroot>
foo
</a>
`);

expect(console.warn).toHaveBeenCalled();
});

it('should sanitize links containing invalid characters', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});

render(compiler('[foo](https://google.com/%AF)'));

expect(root.innerHTML).toMatchInlineSnapshot(`
Expand All @@ -823,6 +861,7 @@ describe('links', () => {
</a>
`);
expect(console.warn).toHaveBeenCalled();
});

it('should handle a link with a URL in the text', () => {
Expand Down
28 changes: 19 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,26 @@ function reactFor(outputFunc) {

function sanitizeUrl(url) {
try {
const prot = decodeURIComponent(url)
.replace(/[^A-Z0-9/:]/gi, '')
.toLowerCase();
const decoded = decodeURIComponent(url);

if (decoded.match(/^\s*javascript:/i)) {
if (process.env.NODE_ENV !== 'production') {
console.warn(
'Anchor URL contains an unsafe JavaScript expression, it will not be rendered.',
decoded
);
}

if (prot.indexOf('javascript:') === 0) {
return null;
}
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
console.warn(
'Anchor URL could not be decoded due to malformed syntax or characters, it will not be rendered.',
url
);
}

// decodeURIComponent sometimes throws a URIError
// See `decodeURIComponent('a%AFc');`
// http://stackoverflow.com/questions/9064536/javascript-decodeuricomponent-malformed-uri-exception
Expand Down Expand Up @@ -667,6 +679,7 @@ function parseCaptureInline(capture, parse, state) {
function captureNothing() {
return {};
}

function renderNothing() {
return null;
}
Expand All @@ -677,11 +690,8 @@ function ruleOutput(rules) {
};
}

function cx() {
return Array.prototype.slice
.call(arguments)
.filter(Boolean)
.join(' ');
function cx(...args) {
return args.filter(Boolean).join(' ');
}

function get(src, path, fb) {
Expand Down

0 comments on commit 9c6c782

Please sign in to comment.