FED-2156 Unsafe required props lint#896
Conversation
Security InsightsNo security relevant content was detected by automated scans. Action Items
Questions or Comments? Reach out on Slack: #support-infosec. |
tools/analyzer_plugin/lib/src/diagnostic/unsafe_required_prop_access.dart
Outdated
Show resolved
Hide resolved
tools/analyzer_plugin/test/unit/util/is_props_from_render_test.dart
Outdated
Show resolved
Hide resolved
| /// | ||
| /// For example, in `props.containsProp((p) => p.requiredProp)`, | ||
| /// returns `true` for the `p` that's followed by `.requiredProp`. | ||
| bool isPropsSafeUtilityMethodArg(Expression target) { |
There was a problem hiding this comment.
Is there a reason why the other functions like this are private, but this one isn't?
There was a problem hiding this comment.
Oh nope, must have just missed this one while cleaning up. Made private in 71431c7.
| }.contains(lifecycleMethod.name.lexeme); | ||
| } | ||
|
|
||
| // |
There was a problem hiding this comment.
Was there going to be a comment here?
There was a problem hiding this comment.
Oops, missed this one. Added in 71431c7.
There was a problem hiding this comment.
This is super awesome, Greg! You were so thorough!!!!
The only thing that came up in QA was that on my computer, // debug:over_react_metrics in the playground has consistently shown UnsafeRequiredPropAccessDiagnostic to be one of the two slowest - at first it was always the slowest at around 44%, but now whenever I restart my analysis server, it's consistently section slowest:
** UnsafeRequiredPropAccessDiagnostic: 3.483ms (24.9%)
** UnsafeRequiredPropAccessDiagnostic: 3.270ms (25.2%),
** UnsafeRequiredPropAccessDiagnostic: 3.488ms (25.1%),
** UnsafeRequiredPropAccessDiagnostic: 3.344ms (25.3%),
I don't know if that's much different from what you were seeing since you said it was around fifth slowest?
Edit: although I just tested it a few times in xbrl-module and it wasn't even in the top three so maybe it has more to do with how hard the other diagnostics are working in the playground relative to this one
QA steps:
- Test in playground example
- Test in a consumer that uses required props (e.g., FluxUiProps) and verify there aren't any unexpected errors - tested in xbrl-module
- Double-check with // debug: over_react_metrics that performance is acceptable (this lint has to process all property accesses to see if they're on props).
@sydneyjodon-wk Ah yup, I think I had been testing on larger files in other repos. Yeah that makes sense to me, since most of the playground examples don't have a lot of code for the diagnostics to process. So yeah as long as performance looks acceptable on larger, real-world code, I think we're good. |
Co-authored-by: sydneyjodon-wk <51122966+sydneyjodon-wk@users.noreply.github.com>
Motivation
While required late props are validated to be present when a component is rendered, there are cases where they won't always be present. For example, "partial" props objects that represent a subset of a component's props.
If required late props are accessed without being set, they'll throw at runtime, and there's currently no static analysis that prevents consumers from doing so, or notifies them that they're doing something unsafe.
So, we want to help prevent consumers from unsafely accessing required late props.
Changes
UnsafeRequiredPropAccessDiagnosticthat warns whenever required props are accessed in an unsafe manner.getPropKey((p) => p.requiredProp)memoareEqual functions, or top-level functions withareEqualin the name since consumers apparently split themout a fair bittest/directory, which typically usesgetPropsa lot (and if unsafe accesses happen, they'll just fail tests instead of crashing an app)typedPropsFactorySharedAnalysisContextto allow creation of files outside oflibRelease Notes
Review
See CONTRIBUTING.md for more details on review types (+1 / QA +1 / +10) and code review process.
Please review:
QA Checklist
// debug: over_react_metricsthat performance is acceptable (this lint has to process all property accesses to see if they're on props). From my testing it looked pretty efficient, all things considered, and was hovering around being our 5th slowest lint, which IMO isn't bad at all.Merge Checklist
While we perform many automated checks before auto-merging, some manual checks are needed: