@@ -99,6 +99,57 @@ describe('sanitizeHtmlMention', () => {
9999 } ) ;
100100} ) ;
101101
102+ describe ( 'sanitizeHtml <img>' , ( ) => {
103+ const urlOnlyRegex = / ^ (?: e n r i c h e d : \/ \/ \S + | h t t p s ? : \/ \/ \S + ) $ / i;
104+
105+ it ( 'keeps src, width, and height with the default config' , ( ) => {
106+ const out = sanitizeHtml (
107+ '<img src="https://example.com/a.png" width="80" height="60">'
108+ ) ;
109+ expect ( out ) . toContain ( 'src="https://example.com/a.png"' ) ;
110+ expect ( out ) . toContain ( 'width="80"' ) ;
111+ expect ( out ) . toContain ( 'height="60"' ) ;
112+ } ) ;
113+
114+ it ( 'keeps width and height even when a URL-only linkRegex is supplied' , ( ) => {
115+ const out = sanitizeHtml (
116+ '<img src="https://example.com/a.png" width="80" height="60" alt="cat">' ,
117+ { linkRegex : urlOnlyRegex }
118+ ) ;
119+ expect ( out ) . toContain ( 'width="80"' ) ;
120+ expect ( out ) . toContain ( 'height="60"' ) ;
121+ expect ( out ) . toContain ( 'src="https://example.com/a.png"' ) ;
122+ expect ( out ) . toContain ( 'alt="cat"' ) ;
123+ } ) ;
124+
125+ it ( 'still validates the img src protocol against the custom linkRegex' , ( ) => {
126+ const out = sanitizeHtml (
127+ '<img src="ftp://example.com/a.png" width="80" height="60">' ,
128+ { linkRegex : urlOnlyRegex }
129+ ) ;
130+ expect ( out ) . not . toContain ( 'ftp://' ) ;
131+ expect ( out ) . toContain ( 'width="80"' ) ;
132+ expect ( out ) . toContain ( 'height="60"' ) ;
133+ } ) ;
134+
135+ it ( 'strips a javascript: src' , ( ) => {
136+ const out = sanitizeHtml (
137+ '<img src="javascript:alert(1)" width="80" height="60">' ,
138+ { linkRegex : urlOnlyRegex }
139+ ) ;
140+ // eslint-disable-next-line no-script-url
141+ expect ( out ) . not . toContain ( 'javascript:' ) ;
142+ } ) ;
143+
144+ it ( 'strips event handlers from img' , ( ) => {
145+ const out = sanitizeHtml (
146+ '<img src="https://example.com/a.png" onerror="alert(1)" width="80">'
147+ ) ;
148+ expect ( out ) . not . toContain ( 'onerror' ) ;
149+ expect ( out ) . toContain ( 'width="80"' ) ;
150+ } ) ;
151+ } ) ;
152+
102153describe ( 'sanitizeLinkAttributes' , ( ) => {
103154 it ( 'strips javascript: URLs from links' , ( ) => {
104155 const out = sanitizeHtml ( '<a href="javascript:alert(1)">x</a>' ) ;
0 commit comments