Skip to content

Commit

Permalink
Allow image data URI schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
samclarke committed Nov 14, 2016
1 parent 22fc15e commit c7ba24e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/lib/escape.js
@@ -1,8 +1,17 @@
define(function (require, exports) {
'use strict';

/* jshint maxlen: false */
// Must start with a valid scheme
// ^
// Schemes that are considered safe
// (https?|s?ftp|mailto|spotify|skype|ssh|teamspeak|tel):|
// Relative schemes (//:) are considered safe
// (\\/\\/)|
// Image data URI's are considered safe
// data:image\\/(png|bmp|gif|p?jpe?g);
var VALID_SCHEME_REGEX =
/^(?:https?|s?ftp|mailto|spotify|skype|ssh|teamspeak|tel):|(?:\/\/)/i;
/^(https?|s?ftp|mailto|spotify|skype|ssh|teamspeak|tel):|(\/\/)|data:image\/(png|bmp|gif|p?jpe?g);/i;

/**
* Escapes a string so it's safe to use in regex
Expand Down Expand Up @@ -72,6 +81,7 @@ define(function (require, exports) {
* teamspeak
* tel
* //
* data:image/(png|jpeg|jpg|pjpeg|bmp|gif);
*
* **IMPORTANT**: This does not escape any HTML in a url, for
* that use the escape.entities() method.
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/lib/escape.js
Expand Up @@ -77,7 +77,11 @@ define([
'ssh:user@host.com:22',
'teamspeak:12345',
'tel:12345',
'//www.example.com/test?id=123'
'//www.example.com/test?id=123',
'data:image/png;test',
'data:image/gif;test',
'data:image/jpg;test',
'data:image/bmp;test'
];

expect(urls.length);
Expand All @@ -101,7 +105,8 @@ define([
var urls = [
'javascript:alert("XSS");',
'jav ascript:alert(\'XSS\');',
'vbscript:msgbox("XSS")'
'vbscript:msgbox("XSS")',
'data:application/javascript;alert("xss")'
];

expect(urls.length);
Expand Down

0 comments on commit c7ba24e

Please sign in to comment.