Skip to content

Commit b11d624

Browse files
Merge pull request #144 from contentstack/fix/dx-2192-html-injection
2 parents 60e794a + 9661d22 commit b11d624

File tree

9 files changed

+672
-183
lines changed

9 files changed

+672
-183
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22

3-
## [1.3.18](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.17) (2025-02-17)
3+
## [1.3.19](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.19) (2025-02-24)
4+
- Fix: Added fix for html injection in keys and values of attributes
5+
6+
## [1.3.18](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.18) (2025-02-17)
47
- Fix: Added fix for html injection
58

69
## [1.3.17](https://github.com/contentstack/contentstack-utils-javascript/tree/v1.3.17) (2025-02-11)

__test__/attributes-to-string.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,92 @@ describe('Attributes to String', () => {
4949
expect(resultString).toEqual(' style="text-align:left; " rows="4" cols="2" colWidths="250, 250"')
5050
done()
5151
})
52+
it('Should rignore attributes with forbidden characters in keys and values', done => {
53+
const attr = {
54+
"style": {
55+
"text-align": "left"
56+
},
57+
"rows": 4,
58+
"cols": 2,
59+
"colWidths": [250, 250],
60+
"<ls": "\"></p><h1>test</h1><p class=\"",
61+
"\"></p><h1>test</h1><p class=\"": 1
62+
} as Attributes;
63+
64+
const resultString = attributeToString(attr);
65+
66+
expect(resultString).toEqual(' style=\"text-align:left; \" rows=\"4\" cols=\"2\" colWidths=\"250, 250\"')
67+
done();
68+
});
69+
it('Should handle object attribute values correctly', done => {
70+
const attr = {
71+
"style": {
72+
"color": "red",
73+
"font-size": "14px"
74+
}
75+
} as Attributes;
76+
77+
const resultString = attributeToString(attr);
78+
79+
expect(resultString).toEqual(' style="color:red; font-size:14px; "');
80+
done();
81+
});
82+
it('Should convert arrays into comma-separated values', done => {
83+
const attr = {
84+
"data-values": [10, 20, 30]
85+
} as Attributes;
86+
87+
const resultString = attributeToString(attr);
88+
89+
expect(resultString).toEqual(' data-values="10, 20, 30"');
90+
done();
91+
});
92+
it('Should handle special characters in values properly', done => {
93+
const attr = {
94+
"title": 'This & That > Those < Them "Quoted"',
95+
"description": "Hello <script>alert(xss)</script>"
96+
} as Attributes;
97+
98+
const resultString = attributeToString(attr);
99+
100+
expect(resultString).toEqual(' title="This &amp; That &gt; Those &lt; Them &quot;Quoted&quot;" description="Hello &lt;script&gt;alert(xss)&lt;/script&gt;"');
101+
done();
102+
});
103+
104+
it('Should handle mixed types of values properly', done => {
105+
const attr = {
106+
"rows": 5,
107+
"isEnabled": true,
108+
"ids": [101, 102],
109+
"style": { "margin": "10px", "padding": "5px" }
110+
} as Attributes;
111+
112+
const resultString = attributeToString(attr);
113+
114+
expect(resultString).toEqual(' rows="5" isEnabled="true" ids="101, 102" style="margin:10px; padding:5px; "');
115+
done();
116+
});
117+
it('Should sanitize both keys and values to prevent HTML injection', done => {
118+
const attr = {
119+
"<script>alert('key')</script>": "test",
120+
"safeKey": "<script>alert(xss)</script>"
121+
} as Attributes;
122+
123+
const resultString = attributeToString(attr);
124+
125+
expect(resultString).toEqual(' safeKey="&lt;script&gt;alert(xss)&lt;/script&gt;"');
126+
done();
127+
});
128+
it('Should ignore attributes with forbidden characters in keys', done => {
129+
const attr = {
130+
"validKey": "safeValue",
131+
'in"valid': "should be ignored",
132+
"another>invalid": "should also be ignored"
133+
} as Attributes;
134+
135+
const resultString = attributeToString(attr);
52136

137+
expect(resultString).toEqual(' validKey="safeValue"');
138+
done();
139+
});
53140
})

__test__/mock/json-element-mock-result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const styleObjHtml = "<h1 style=\"text-align:justify;\">heading1</h1><h2 style=\
2424
const referenceObjHtml = "<p><a class=\"embedded-entry redactor-component block-entry\" href=\"/test\" target=\"_self\">Embed entry as a link</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-3\" target=\"_blank\">Open entry as a link in new tab</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-2\" target=\"_self\">Bold entry</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"/entry-4\" target=\"_blank\"><strong>Bold entry open in new tab</strong></a></p>"
2525
const referenceObjHtmlBlock = "<p><a class=\"embedded-entry redactor-component block-entry\" href=\"/Test\" target=\"_self\">Embed entry as a link</a></p><p><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_blank\">Embed entry as a link open in new tab</a></p><ul><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_self\">Entry as a link</a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_blank\">Open entry as a link in new tab</a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"undefined\" target=\"_self\"><strong><u>Entry as a link bold</u></strong></a></li><li><a class=\"embedded-entry redactor-component block-entry\" href=\"khjgf\" target=\"_blank\"><strong><u>Open bold entry as a link in new tab </u></strong></a></li><li><a href=\"https://\" target=\"_self\"><strong><u>Link URL</u></strong></a></li><li><a href=\"https://\" target=\"_blank\"><strong><u>Open link in new tab</u></strong></a></li></ul>"
2626
const imagetags = "<figure style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\"><a href=\"https://batman.com\" target=\"_blank\"><img asset_uid=\"asset-UID\" class=\"embedded-asset\" src=\"https://images.contentstack.io/v3/assets/api-key/asset-UID/random-uid/batman.png\" alt=\"batman\" target=\"_blank\" style=\"text-align:right;max-width:137px;float:right;width:137px;max-height:257px;height:257px;\" /></a><figcaption>The Batman</figcaption></figure>"
27-
const escapeHtml = "<p>&lt;p&gt;Welcome to Contentstack! &lt;script&gt;console.log(/\"Hello from Contentstack!/\");&lt;/script&gt; Explore our platform to create, manage, and publish content seamlessly.&lt;/p&gt;</p>"
27+
const escapeHtml = "<p>&lt;p&gt;Welcome to Contentstack! &lt;script&gt;console.log(/&quot;Hello from Contentstack!/&quot;);&lt;/script&gt; Explore our platform to create, manage, and publish content seamlessly.&lt;/p&gt;</p>"
2828

2929
export {
3030
h1Html,

0 commit comments

Comments
 (0)