-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
fix: Prevent TypeError when type is not a string #9848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: alpha
Are you sure you want to change the base?
fix: Prevent TypeError when type is not a string #9848
Conversation
🚀 Thanks for opening this pull request! |
📝 WalkthroughWalkthroughAdds a guard in mongoFieldToParseSchemaField to validate that the input Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller as Caller
participant MSC as MongoSchemaCollection.mongoFieldToParseSchemaField
Caller->>MSC: mongoFieldToParseSchemaField(type)
alt type is not a non-empty string
MSC-->>Caller: throw Parse.Error(INVALID_SCHEMA_OPERATION, message)
else type is a valid string
MSC-->>Caller: proceed with existing mapping logic (relation, pointer, etc.)
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Assessment against linked issues
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
🎉 Snyk checks have passed. No issues have been found so far.✅ security/snyk check is complete. No issues have been found. (View Details) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
src/Adapters/Storage/Mongo/MongoSchemaCollection.js (4)
54-55
: Optional: add field context to the error for easier debuggingIf we keep throwing inside
mongoFieldToParseSchemaField
, wrapping this call to appendclassName/fieldName
context improves operator UX when boot fails on bad schemas.- obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]); + try { + obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]); + } catch (e) { + if (e && e.code != null) { + // Preserve code; enrich message + e.message = `Invalid field type for "${fieldName}" in class "${schema._id}": ${e.message}`; + } + throw e; + }
5-11
: Add regression tests for non-string/whitespace input typesTo lock this down, please add tests covering:
- type: undefined, null, number, boolean, object, array, function
- type: '' and ' ' (whitespace)
- type: 'number ' (trailing space) → with the trim change, should map to Number; without it, it silently fails
If helpful, I can draft a spec in
spec/schemas.spec.js
that exercisesmongoSchemaFieldsToParseSchemaFields
with a mocked schema input.
25-47
: Add a default case inmongoFieldToParseSchemaField
to reject unsupported typesI’ve confirmed that the only call site for
mongoFieldToParseSchemaField
is withinmongoSchemaFieldsToParseSchemaFields
, and no code relies on it returningundefined
. Failing fast on unrecognized types will surface schema misconfigurations immediately without breaking existing behavior.• File:
src/Adapters/Storage/Mongo/MongoSchemaCollection.js
• Location: inside theswitch (type)
inmongoFieldToParseSchemaField
Recommended diff:
switch (type) { // …existing cases… case 'polygon': return { type: 'Polygon' }; + default: + throw new Parse.Error( + Parse.Error.INVALID_SCHEMA_OPERATION, + `Unsupported field type: "${type}".` + ); }
5-13
: Tighten type validation and normalize inputGreat addition to guard against
TypeError
. Two optional hardening tweaks:
- Reject empty or whitespace-only strings by using
type.trim()
.- Trim
type
before downstream checks so trailing (or leading) spaces can’t sneak through.Proposed diff in src/Adapters/Storage/Mongo/MongoSchemaCollection.js (lines 5–13):
// Add type validation to prevent TypeError - if (!type || typeof type !== 'string') { + if (typeof type !== 'string' || type.trim() === '') { throw new Parse.Error( Parse.Error.INVALID_SCHEMA_OPERATION, - `Invalid field type: ${type}. Expected a string. Field type must be one of: string, number, boolean, date, map, object, array, geopoint, file, bytes, polygon, or a valid relation/pointer format.` + `Invalid field type: ${String(type)}. Expected a non-empty string. Field type must be one of: string, number, boolean, date, map, object, array, geopoint, file, bytes, polygon, or a valid relation/pointer format.` ); } + + // Normalize input + type = type.trim();Follow-up question:
- All other “invalid field type” guards (e.g. in SchemaController) use
Parse.Error.INCORRECT_TYPE
. Should we switch fromINVALID_SCHEMA_OPERATION
toINCORRECT_TYPE
here for consistency?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
src/Adapters/Storage/Mongo/MongoSchemaCollection.js
(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/Adapters/Storage/Mongo/MongoSchemaCollection.js (7)
spec/schemas.spec.js (1)
Parse
(3-3)src/Controllers/SchemaController.js (1)
Parse
(18-18)src/Adapters/Auth/apple.js (1)
Parse
(48-48)src/Adapters/Auth/facebook.js (1)
Parse
(62-62)src/Adapters/Auth/ldap.js (1)
Parse
(77-77)src/SchemaMigrations/DefinedSchemas.js (1)
Parse
(3-3)spec/ReadPreferenceOption.spec.js (1)
Parse
(3-3)
Added a type check in mongoFieldToParseSchemaField to ensure `type` is a string before calling `startsWith`. This prevents crashes when Parse Server processes MongoDB schema fields with undefined, null, or unexpected type values. Closes parse-community#9847
8d90d30
to
1f9b6fe
Compare
Added a type check in mongoFieldToParseSchemaField to ensure
type
is a string before callingstartsWith
. This prevents crashes when Parse Server processes MongoDB schema fields with undefined, null, or unexpected type values.Closes #9847
Pull Request
Issue
Approach
Tasks
Summary by CodeRabbit
New Features
Bug Fixes
Chores