mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Sanitize module name to prevent Java code injection (#55484)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55484 This change adds content validation to ensure module names only contain safe identifier characters (alphanumeric and underscores, matching `^[a-zA-Z_][a-zA-Z0-9_]*$`). A new error class `IncorrectModuleRegistryCallArgumentValueParserError` provides a clear error message when an unsafe module name is detected. Changelog: [Android][Fixed] - Validate module names in codegen Reviewed By: CalixTang Differential Revision: D92736956 fbshipit-source-id: 29b2c603bf97f9a2c30c8350d29b468d315765f0
This commit is contained in:
1 parent
9db85e7503
commit
a748a405b6
3 files changed
+125
-2
No files matched your search
@@ -38,6 +38,7 @@ const {
|
||||
} = require('../error-utils');
|
||||
const {
|
||||
IncorrectModuleRegistryCallArgumentTypeParserError,
|
||||
IncorrectModuleRegistryCallArgumentValueParserError,
|
||||
IncorrectModuleRegistryCallArityParserError,
|
||||
IncorrectModuleRegistryCallTypeParameterParserError,
|
||||
MisnamedModuleInterfaceParserError,
|
||||
@@ -489,7 +490,7 @@ describe('throwIfIncorrectModuleRegistryCallArgument', () => {
|
||||
});
|
||||
|
||||
it("don't throw error if callExpressionArg type is `Literal` in Flow", () => {
|
||||
const callExpressionArg = {type: 'Literal'};
|
||||
const callExpressionArg = {type: 'Literal', value: 'ValidModuleName'};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
@@ -511,7 +512,7 @@ describe('throwIfIncorrectModuleRegistryCallArgument', () => {
|
||||
});
|
||||
|
||||
it("don't throw error if callExpressionArg type is `StringLiteral` in TypeScript", () => {
|
||||
const callExpressionArg = {type: 'StringLiteral'};
|
||||
const callExpressionArg = {type: 'StringLiteral', value: 'ValidModuleName'};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
@@ -520,6 +521,101 @@ describe('throwIfIncorrectModuleRegistryCallArgument', () => {
|
||||
);
|
||||
}).not.toThrow(IncorrectModuleRegistryCallArgumentTypeParserError);
|
||||
});
|
||||
|
||||
it('throw error if callExpressionArg value contains double quotes', () => {
|
||||
const callExpressionArg = {
|
||||
type: 'StringLiteral',
|
||||
value:
|
||||
'Malicious"; static { System.exit(1); } public static final String IGNORE = "',
|
||||
};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
);
|
||||
}).toThrow(IncorrectModuleRegistryCallArgumentValueParserError);
|
||||
});
|
||||
|
||||
it('throw error if callExpressionArg value contains newlines', () => {
|
||||
const callExpressionArg = {
|
||||
type: 'StringLiteral',
|
||||
value: 'Malicious\nCode',
|
||||
};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
);
|
||||
}).toThrow(IncorrectModuleRegistryCallArgumentValueParserError);
|
||||
});
|
||||
|
||||
it('throw error if callExpressionArg value contains spaces', () => {
|
||||
const callExpressionArg = {type: 'StringLiteral', value: 'has spaces'};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
);
|
||||
}).toThrow(IncorrectModuleRegistryCallArgumentValueParserError);
|
||||
});
|
||||
|
||||
it('throw error if callExpressionArg value contains special characters', () => {
|
||||
const callExpressionArg = {type: 'StringLiteral', value: 'Module;Drop'};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
);
|
||||
}).toThrow(IncorrectModuleRegistryCallArgumentValueParserError);
|
||||
});
|
||||
|
||||
it('throw error if callExpressionArg value is empty string', () => {
|
||||
const callExpressionArg = {type: 'StringLiteral', value: ''};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
);
|
||||
}).toThrow(IncorrectModuleRegistryCallArgumentValueParserError);
|
||||
});
|
||||
|
||||
it('throw error if callExpressionArg value starts with a number', () => {
|
||||
const callExpressionArg = {type: 'StringLiteral', value: '1Module'};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
);
|
||||
}).toThrow(IncorrectModuleRegistryCallArgumentValueParserError);
|
||||
});
|
||||
|
||||
it("don't throw error for valid module name with underscores", () => {
|
||||
const callExpressionArg = {type: 'StringLiteral', value: 'My_Module_123'};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it("don't throw error for valid module name starting with underscore", () => {
|
||||
const callExpressionArg = {type: 'StringLiteral', value: '_PrivateModule'};
|
||||
expect(() => {
|
||||
throwIfIncorrectModuleRegistryCallArgument(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
);
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('throwIfUntypedModule', () => {
|
||||
|
||||
@@ -17,6 +17,7 @@ import type {Parser} from './parser';
|
||||
|
||||
const {
|
||||
IncorrectModuleRegistryCallArgumentTypeParserError,
|
||||
IncorrectModuleRegistryCallArgumentValueParserError,
|
||||
IncorrectModuleRegistryCallArityParserError,
|
||||
IncorrectModuleRegistryCallTypeParameterParserError,
|
||||
MisnamedModuleInterfaceParserError,
|
||||
@@ -309,6 +310,16 @@ function throwIfIncorrectModuleRegistryCallArgument(
|
||||
type,
|
||||
);
|
||||
}
|
||||
|
||||
const value = callExpressionArg.value;
|
||||
if (typeof value !== 'string' || !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value)) {
|
||||
throw new IncorrectModuleRegistryCallArgumentValueParserError(
|
||||
nativeModuleName,
|
||||
callExpressionArg,
|
||||
methodName,
|
||||
String(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function throwIfPartialNotAnnotatingTypeParameter(
|
||||
|
||||
@@ -425,6 +425,21 @@ class IncorrectModuleRegistryCallArgumentTypeParserError extends ParserError {
|
||||
}
|
||||
}
|
||||
|
||||
class IncorrectModuleRegistryCallArgumentValueParserError extends ParserError {
|
||||
constructor(
|
||||
nativeModuleName: string,
|
||||
flowArgument: $FlowFixMe,
|
||||
methodName: string,
|
||||
value: string,
|
||||
) {
|
||||
super(
|
||||
nativeModuleName,
|
||||
flowArgument,
|
||||
`Please call TurboModuleRegistry.${methodName}<Spec>() with a safe module name. Module names must only contain alphanumeric characters and underscores (matching /^[a-zA-Z_][a-zA-Z0-9_]*$/). Got '${value.replace(/[^\x20-\x7e]/g, '?')}'`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ParserError,
|
||||
MissingTypeParameterGenericParserError,
|
||||
@@ -452,4 +467,5 @@ module.exports = {
|
||||
IncorrectModuleRegistryCallTypeParameterParserError,
|
||||
IncorrectModuleRegistryCallArityParserError,
|
||||
IncorrectModuleRegistryCallArgumentTypeParserError,
|
||||
IncorrectModuleRegistryCallArgumentValueParserError,
|
||||
};
|
||||
Reference in new issue
Block a user