From a748a405b66546367f0160425d135be88812763d Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Tue, 10 Feb 2026 07:23:22 -0800 Subject: [PATCH] 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 --- .../src/parsers/__tests__/error-utils-test.js | 100 +++++++++++++++++- .../src/parsers/error-utils.js | 11 ++ .../src/parsers/errors.js | 16 +++ 3 files changed, 125 insertions(+), 2 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js index 5feec430b06..6e893f6e419 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -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', () => { diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index 0ebf2aa9ecf..da39e9d6a1e 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -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( diff --git a/packages/react-native-codegen/src/parsers/errors.js b/packages/react-native-codegen/src/parsers/errors.js index 9ae9e983a57..fececdedfc4 100644 --- a/packages/react-native-codegen/src/parsers/errors.js +++ b/packages/react-native-codegen/src/parsers/errors.js @@ -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}() 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, };