Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/ra-ui-materialui/src/field/RichTextField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ describe('stripTags', () => {
'All our base is belong to us.'
);
});

it('should drop an unfinished opening tag', () => {
expect(removeTags('Safe<script alert(1)')).toEqual('Safe');
});
});

describe('<RichTextField />', () => {
Expand Down
30 changes: 28 additions & 2 deletions packages/ra-ui-materialui/src/field/RichTextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,34 @@ export interface RichTextFieldProps<
purifyOptions?: PurifyOptions;
}

export const removeTags = (input: string) =>
input ? input.replace(/<[^>]+>/gm, '') : '';
export const removeTags = (input: string) => {
if (!input) {
return '';
}

let output = '';
let isInsideTag = false;

for (const character of input) {
if (character === '<') {
isInsideTag = true;
continue;
}

if (character === '>') {
if (isInsideTag) {
isInsideTag = false;
continue;
}
}

if (!isInsideTag) {
output += character;
}
}

return output;
};

const PREFIX = 'RaRichTextField';

Expand Down
Loading