@@ -17,6 +17,37 @@ describe('Image', () => {
1717 expect ( tree ) . toMatchSnapshot ( ) ;
1818 } ) ;
1919
20+ describe ( 'it should prevent unsafe image uri protocols in the rendered image src' , ( ) => {
21+ it ( 'should prevent javascript protocol in image src' , ( ) => {
22+ // eslint-disable-next-line no-script-url
23+ const xssJavascriptUri = 'javascript:alert("p0wn3d")' ;
24+ const { getByTestId } = render ( < Image image_url = { xssJavascriptUri } /> ) ;
25+ expect ( getByTestId ( 'image-test' ) ) . not . toHaveAttribute (
26+ 'src' ,
27+ xssJavascriptUri ,
28+ ) ;
29+ } ) ;
30+ it ( 'should prevent javascript protocol in thumbnail src' , ( ) => {
31+ // eslint-disable-next-line no-script-url
32+ const xssJavascriptUri = 'javascript:alert("p0wn3d")' ;
33+ const { getByTestId } = render ( < Image thumb_url = { xssJavascriptUri } /> ) ;
34+ expect ( getByTestId ( 'image-test' ) ) . not . toHaveAttribute (
35+ 'src' ,
36+ xssJavascriptUri ,
37+ ) ;
38+ } ) ;
39+ it ( 'should prevent dataUris in image src' , ( ) => {
40+ const xssDataUri = 'data:image/svg+xml;base64,DANGEROUSENCODEDSVG' ;
41+ const { getByTestId } = render ( < Image image_url = { xssDataUri } /> ) ;
42+ expect ( getByTestId ( 'image-test' ) ) . not . toHaveAttribute ( 'src' , xssDataUri ) ;
43+ } ) ;
44+ it ( 'should prevent dataUris in thumb src' , ( ) => {
45+ const xssDataUri = 'data:image/svg+xml;base64,DANGEROUSENCODEDSVG' ;
46+ const { getByTestId } = render ( < Image thumb_url = { xssDataUri } /> ) ;
47+ expect ( getByTestId ( 'image-test' ) ) . not . toHaveAttribute ( 'src' , xssDataUri ) ;
48+ } ) ;
49+ } ) ;
50+
2051 it ( 'should open modal on image click' , async ( ) => {
2152 jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => null ) ;
2253 const { getByTestId, getByTitle } = render (
0 commit comments