Skip to content

Commit a3d7895

Browse files
committed
Fix RichTextField code scanning issues
1 parent 9676993 commit a3d7895

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/ra-ui-materialui/src/field/RichTextField.spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ describe('stripTags', () => {
4545
'All our base is belong to us.'
4646
);
4747
});
48+
49+
it('should drop an unfinished opening tag', () => {
50+
expect(removeTags('Safe<script alert(1)')).toEqual('Safe');
51+
});
4852
});
4953

5054
describe('<RichTextField />', () => {

packages/ra-ui-materialui/src/field/RichTextField.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,34 @@ export interface RichTextFieldProps<
8787
purifyOptions?: PurifyOptions;
8888
}
8989

90-
export const removeTags = (input: string) =>
91-
input ? input.replace(/<[^>]+>/gm, '') : '';
90+
export const removeTags = (input: string) => {
91+
if (!input) {
92+
return '';
93+
}
94+
95+
let output = '';
96+
let isInsideTag = false;
97+
98+
for (const character of input) {
99+
if (character === '<') {
100+
isInsideTag = true;
101+
continue;
102+
}
103+
104+
if (character === '>') {
105+
if (isInsideTag) {
106+
isInsideTag = false;
107+
continue;
108+
}
109+
}
110+
111+
if (!isInsideTag) {
112+
output += character;
113+
}
114+
}
115+
116+
return output;
117+
};
92118

93119
const PREFIX = 'RaRichTextField';
94120

0 commit comments

Comments
 (0)