Skip to content

Commit 39f924b

Browse files
authored
Merge pull request #11224 from marmelab/fix-form-data-consumer-redos
Fix `FormDataConsumer` to avoid ReDoS in source detection
2 parents 4e4e898 + b039b06 commit 39f924b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

packages/ra-core/src/form/FormDataConsumer.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ export const FormDataConsumerView = <
7272
const finalSource = useWrappedSource(source || '');
7373

7474
// Passes an empty string here as we don't have the children sources and we just want to know if we are in an iterator
75-
const matches = ArraySourceRegex.exec(finalSource);
75+
const arraySource = getArraySource(finalSource);
7676

7777
// If we have an index, we are in an iterator like component (such as the SimpleFormIterator)
78-
if (matches) {
79-
const scopedFormData = get(formData, matches[0]);
78+
if (arraySource) {
79+
const scopedFormData = get(formData, arraySource);
8080
result = children({ formData, scopedFormData });
8181
} else {
8282
result = children({ formData });
@@ -85,7 +85,12 @@ export const FormDataConsumerView = <
8585
return result === undefined ? null : result;
8686
};
8787

88-
const ArraySourceRegex = new RegExp(/.+\.\d+$/);
88+
const getArraySource = (source: string) =>
89+
source.lastIndexOf('.') > 0 && ArraySourceRegex.test(source)
90+
? source
91+
: undefined;
92+
93+
const ArraySourceRegex = /\.\d+$/;
8994

9095
export interface FormDataConsumerRenderParams<
9196
TFieldValues extends FieldValues = FieldValues,

0 commit comments

Comments
 (0)