typeorm findOneBy( { id: null or undefined } ) method return the first record on the table !
Problem
Expected Behavior Return `null` Actual Behavior Retrieve the first record from users table ! Example : [code block] Steps to Reproduce 1. Create typeorm connection 2. Create user entity and migrate it to the database 3. Query the database using the user repository then call `findOneBy` method . Relevant Database Driver(s) | DB Type | Reproducible | |-------------------| --- | | `sqlite` | yes | For other database drivers , i have no idea :)
Unverified for your environment
Select your OS to check compatibility.
2 Fixes
Implement Input Validation for findOneBy Method
The `findOneBy` method in TypeORM does not handle null or undefined values for the query parameters correctly. When provided with an object containing `id: null` or `id: undefined`, it defaults to returning the first record in the table instead of returning null, which is not the expected behavior.
Awaiting Verification
Be the first to verify this fix
- 1
Add Input Validation
Before calling the `findOneBy` method, validate the input to ensure that the `id` is neither null nor undefined. If it is, return null or handle it appropriately.
typescriptconst userId = null; // or undefined if (userId == null) { return null; } const user = await userRepository.findOneBy({ id: userId }); - 2
Update Repository Call
Ensure that all calls to the `findOneBy` method across the application are updated to include the input validation step. This will prevent unintended queries from being executed.
typescriptconst user = await userRepository.findOneBy({ id: userId }); // Ensure userId is validated before this line - 3
Test Input Scenarios
Create unit tests to cover scenarios where the `id` is null, undefined, and valid values. Ensure that the tests confirm that null or undefined inputs return null as expected.
typescriptit('should return null for null id', async () => { const result = await userRepository.findOneBy({ id: null }); expect(result).toBeNull(); }); - 4
Review and Refactor Code
Review the codebase for any other instances where similar queries are made without validation. Refactor those instances to include the same validation logic.
typescriptconst userId = getUserId(); // Ensure this is validated if (userId == null) { return null; } const user = await userRepository.findOneBy({ id: userId }); - 5
Deploy Changes
Once all changes and tests are complete, deploy the updated code to the production environment. Monitor for any unexpected behavior.
Validation
To confirm the fix worked, run the application and call the `findOneBy` method with null and undefined as inputs. Ensure that it returns null in both cases. Additionally, run the unit tests created in step 3 to validate that all scenarios pass.
Sign in to verify this fix
1 low-confidence fix
Implement Input Validation for findOneBy Method
The `findOneBy` method in TypeORM does not handle null or undefined values for the `id` parameter properly. When passed null or undefined, it defaults to returning the first record in the table instead of returning null, which is not the expected behavior.
Awaiting Verification
Be the first to verify this fix
- 1
Add Input Validation
Before calling the `findOneBy` method, check if the `id` is null or undefined. If it is, return null or handle the case appropriately.
typescriptconst userId = null; // or undefined const user = userRepository.findOneBy({ id: userId }); if (userId == null) { return null; // or handle error } - 2
Update Repository Method Calls
Ensure that all calls to the user repository's `findOneBy` method are wrapped in the input validation check to prevent similar issues throughout the application.
typescriptconst getUserById = async (id: number | null | undefined) => { if (id == null) return null; return await userRepository.findOneBy({ id }); }; - 3
Test the Changes
Create unit tests that verify the behavior of the `getUserById` function, ensuring that it returns null when passed null or undefined and retrieves the correct user when a valid id is provided.
typescripttest('should return null for null id', async () => { const result = await getUserById(null); expect(result).toBeNull(); }); - 4
Review and Refactor Codebase
Conduct a code review to identify any other instances where similar input validation may be necessary and refactor accordingly to maintain consistency and prevent future issues.
Validation
Run the application and call the `getUserById` function with null and undefined values. Confirm that it returns null as expected. Additionally, verify that valid ids return the correct user records.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep