Skip to content

Commit 182644a

Browse files
committed
feat: add param to disable url checks for social embed
1 parent 51c0960 commit 182644a

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/toRedactor.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,15 +494,19 @@ export const toRedactor = (jsonValue: any,options?:IJsonToHtmlOptions) : string
494494
}
495495
figureStyles.fieldsEdited.push(figureStyles.caption)
496496
}
497-
if (jsonValue['type'] === 'social-embeds') {
498-
const sanitizedHTML = DOMPurify.sanitize(allattrs['src'])
499-
const urlMatch:any = sanitizedHTML.match(/https?:\/\/[^\s"'>]+/);
500-
if (urlMatch && urlMatch[0] !== 'undefined') {
501-
attrsJson['src'] = decodeURIComponent(urlMatch[0])
502-
} else{
503-
attrsJson['src'] = " "
504-
}
505-
}
497+
498+
if (!options?.skipURLSanitization && jsonValue['type'] === 'social-embeds') {
499+
const sanitizedHTML = DOMPurify.sanitize(allattrs['src']);
500+
501+
const urlMatch = sanitizedHTML.match(/https?:\/\/[^\s"'<>()]+/);
502+
503+
if (urlMatch) {
504+
attrsJson['src'] = decodeURIComponent(urlMatch[0]);
505+
} else {
506+
delete attrsJson['src'];
507+
}
508+
}
509+
506510
if(!(options?.customElementTypes && !isEmpty(options.customElementTypes) && options.customElementTypes[jsonValue['type']])) {
507511
delete attrsJson['url']
508512
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ export interface IJsonToHtmlOptions {
2020
customElementTypes?: IJsonToHtmlElementTags,
2121
customTextWrapper?: IJsonToHtmlTextTags,
2222
allowNonStandardTypes?: boolean,
23+
skipURLSanitization?:boolean
2324
}

test/expectedJson.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,9 +2002,9 @@ export default {
20022002
},
20032003
"RT-360":{
20042004
"html": [
2005-
`<iframe src="https://www.youtube.com/embed/VD6xJq8NguY" width="560" height="320" data-type="social-embeds" ></iframe>`,
2006-
`<iframe src=" " width="560" height="320" data-type="social-embeds" ></iframe>`,
2007-
'<iframe src=" " width="560" height="320" data-type="social-embeds" ></iframe>',
2005+
`<iframe src="https://www.youtube.com/watch?v=Gw7EqoOYC9A" width="560" height="320" data-type="social-embeds" ></iframe>`,
2006+
`<iframe width="560" height="320" data-type="social-embeds" ></iframe>`,
2007+
'<iframe width="560" height="320" data-type="social-embeds" ></iframe>',
20082008
],
20092009
"json":
20102010
[
@@ -2017,7 +2017,7 @@ export default {
20172017
"uid": "45a850acbeb949db86afe415625ad1ce",
20182018
"type": "social-embeds",
20192019
"attrs": {
2020-
"src": "https://www.youtube.com/embed/VD6xJq8NguY\"></iframe><script>alert(document.cookie)</script><iframe ",
2020+
"src": "https://www.youtube.com/watch?v=Gw7EqoOYC9A\"></iframe><script>alert(document.cookie)</script><iframe ",
20212021
"width": 560,
20222022
"height": 320
20232023
},
@@ -2061,7 +2061,7 @@ export default {
20612061
"uid": "45a850acbeb949db86afe415625ad1ce",
20622062
"type": "social-embeds",
20632063
"attrs": {
2064-
"src": "www.youtube.com/embed/VD6xJq8NguY",
2064+
"src": "www.youtube.com/watch?v=Gw7EqoOYC9A",
20652065
"width": 560,
20662066
"height": 320
20672067
},

0 commit comments

Comments
 (0)