Repository navigation
Conversation
8a57ce8 to
b4ac4b1
Compare
7ecb52d to
eeab6e5
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Swift subscript setter handling has shadowing collisions, incorrect argument mapping when defaults are omitted, and premature receiver updates, causing missed or false data-flow results.
3 open issues
What changed in this PR
Adds Swift subscript support to the unified extractor and analysis libraries, representing reads as subscript method calls and writes as setter-method calls.
Changes:
- Extracts subscript getters and setters with separate parameters and generic type parameters.
- Extends call resolution, data flow, and model matching to support setters and basic array-element flow.
- Adds extraction and analysis tests, including a newly detected path-injection flow.
| File | Description |
|---|---|
| unified/ql/test/query-tests/security/CWE-022/PathInjection/testPathInjection.swift | Expects the newly detected subscript flow. |
| unified/ql/test/query-tests/security/CWE-022/PathInjection/PathInjectionTest.expected | Records the additional alert and flow path. |
| unified/ql/test/library-tests/type-inference/type-inference.expected | Updates subscript expression labels. |
| unified/ql/test/library-tests/mad/test.swift | Tests subscript source and sink models. |
| unified/ql/test/library-tests/mad/test.ext.json | Defines getter and setter models. |
| unified/ql/test/library-tests/mad/test.expected | Records model-matching results. |
| unified/ql/test/library-tests/dataflow/test.ext.json | Adds subscript flow summaries. |
| unified/ql/test/library-tests/dataflow/test.expected | Records subscript data-flow results. |
| unified/ql/test/library-tests/dataflow/subscripts.swift | Tests getters, setters, and write-back flows. |
| unified/ql/test/library-tests/controlflow/basicblock-slices.expected | Updates subscript control-flow snapshots. |
| unified/ql/test/library-tests/BasicTest/test.expected | Records synthetic subscript identifiers. |
| unified/ql/lib/unified.dbscheme | Adds accessor type-parameter storage. |
| unified/ql/lib/codeql/unified/internal/typeinference/TypeInference.qll | Resolves setter methods and accessor generics. |
| unified/ql/lib/codeql/unified/internal/typeinference/Type.qll | Recognizes accessor-owned type parameters. |
| unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll | Matches setter models and their arguments. |
| unified/ql/lib/codeql/unified/internal/FacadeAst.qll | Defines SetterMethodDeclaration. |
| unified/ql/lib/codeql/unified/internal/dataflow/FlowSummary.qll | Includes setter calls in summary matching. |
| unified/ql/lib/codeql/unified/internal/dataflow/DataFlowPluginSwift.qll | Adds subscript element flow and write-back propagation. |
| unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll | Maps setter arguments into data flow. |
| unified/ql/lib/codeql/unified/internal/dataflow/DataFlowGraph.qll | Connects setter receivers and updates. |
| unified/ql/lib/codeql/unified/internal/dataflow/DataFlowCall.qll | Represents assignment-target calls as setters. |
| unified/ql/lib/codeql/unified/internal/dataflow/CallGraph.qll | Adds setter targets to the call graph. |
| unified/ql/lib/codeql/unified/internal/Ast.qll | Exposes accessor type parameters. |
| unified/ql/consistency-queries/DataFlowConsistency.ql | Allows arguments shared by getter and setter calls. |
| unified/extractor/tests/corpus/swift/subscripts/subscript-protocol-requirement.swift | Adds a protocol subscript fixture. |
| unified/extractor/tests/corpus/swift/subscripts/subscript-protocol-requirement.output | Records protocol accessor extraction. |
| unified/extractor/tests/corpus/swift/subscripts/subscript-getter-only.swift | Adds an implicit-getter fixture. |
| unified/extractor/tests/corpus/swift/subscripts/subscript-getter-only.output | Records getter-only extraction. |
| unified/extractor/tests/corpus/swift/subscripts/subscript-get-set.swift | Covers labels, modifiers, and generic accessors. |
| unified/extractor/tests/corpus/swift/subscripts/subscript-get-set.output | Records getter and setter extraction. |
| unified/extractor/tests/corpus/swift/subscripts/subscript-assignment.swift | Adds assignment and nested-subscript fixtures. |
| unified/extractor/tests/corpus/swift/subscripts/subscript-assignment.output | Records assignment extraction. |
| unified/extractor/tests/corpus/swift/expressions/subscript-call.output | Updates subscript-call extraction snapshots. |
| unified/extractor/tests/corpus/swift/collections/subscript-access.output | Records array access as a member call. |
| unified/extractor/tests/corpus/swift/collections/dictionary-subscript.output | Records dictionary access as a member call. |
| unified/extractor/src/languages/swift/swift.rs | Implements subscript declaration and call translation. |
| unified/extractor/ast_types.yml | Extends accessor schema and documents setter methods. |
Review effort: Balanced (adaptive)
9aee525 to
5ae017e
Compare
Extract subscript accesses as calls to a 'subscript' member, and subscript declarations as a 'subscript' getter function followed by a 'subscript' setter function. The setter clones the subscript parameters and takes the new value as an extra parameter with the reserved external name '#newValue', which identifies it as a setter method. A call appearing as an assignment target, or written back after an in-place update, now invokes the corresponding setter method, generalizing the existing property setter call logic. MaD models refer to setter methods by their argument labels, including '#newValue:', e.g. 'subscript(_:#newValue:)'. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
872da71 to
5ebb68a
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The PR description still needs final-revision DCA results and a summary of changed results or unexpected regressions for the Swift extraction and unified data-flow changes.
1 open issue
3 resolved since last review
Review effort: Balanced (adaptive)
| exists(MemberAccessExpr callee, ClassLikeDeclaration cls, NamespaceNode ns | | ||
| callee = ce.getCallee() and | ||
| ns.getMember(callee.getMemberName()).isIdentifier(result.getNameNode()) | ||
| | | ||
| cls = inferType(callee.getBase()).asClass() and | ||
| ns.isInstanceMemberNamespace(cls) | ||
| or | ||
| cls = callee.getBase().(TypeMention).getType().asClass() and | ||
| ns.isStaticMemberNamespace(cls) | ||
| ) |
There was a problem hiding this comment.
Could this instead be
exists(Input3::Callable c |
result = c.getCallable() and
c = [lookupInstanceMember(this.getCallee(), this.getTargetViaStaticNameBinding()]
)?
There was a problem hiding this comment.
Done in 3b704ae. Copilot decided this was worth factoring into a member predicate, which seems fine to me, but let me know if you disagree.
paldepind
left a comment
There was a problem hiding this comment.
Review of a the Rust changes.
| return_type: ret, | ||
| }); | ||
| let mut result = Vec::new(); | ||
| for acc in accessors.into_iter() { |
There was a problem hiding this comment.
The into_iter() here is not needed.
cargo clippy --package codeql-extractor-unified --all-targets --all-features finds this and a few other things (unfortunately it's not running in CI yet 😭).
| struct SubscriptAccessor { | ||
| /// Modifiers written on the accessor itself, such as `nonmutating`. | ||
| modifiers: Vec<yeast::OutputId>, | ||
| /// For a setter, the explicit name of its new-value parameter (`set(v)`), |
There was a problem hiding this comment.
Might just be me, but the content of the parenthesis was a bit confusing. It made sense after seeing where we use set(v) in the tests, but maybe this'll make it a bit clearer?
| /// For a setter, the explicit name of its new-value parameter (`set(v)`), | |
| /// For a setter, the explicit name of its new-value parameter (the `newValue` in `set(newValue) { ... }`), |
There was a problem hiding this comment.
Clarified the comment, but kept the parameter name as v to avoid confusing it with the default name which is newValue.
| let mut result = Vec::new(); | ||
| for acc in accessors.into_iter() { | ||
| ctx.is_chained = !result.is_empty(); | ||
| result.extend(ctx.translate(acc)); | ||
| } | ||
| result |
There was a problem hiding this comment.
We can do this without mutation by using flat_map:
| let mut result = Vec::new(); | |
| for acc in accessors.into_iter() { | |
| ctx.is_chained = !result.is_empty(); | |
| result.extend(ctx.translate(acc)); | |
| } | |
| result | |
| accessors.into_iter().enumerate().flat_map(|(i, acc)| { | |
| ctx.is_chained = i > 0; | |
| ctx.translate(acc) | |
| }).collect::<Vec<_>>() |
| let ty = ctx.translate(ty).into_iter().next(); | ||
| let val = ctx.translate(val).into_iter().next(); |
There was a problem hiding this comment.
My understanding is that these two lines can just be deleted. YEAST will do the translation if the untranslated ty and val end up in the tree.
| /// True while translating the parameters of a subscript. Unlike function | ||
| /// parameters, a subscript parameter with a single name has no argument | ||
| /// label. | ||
| in_subscript_parameters: bool, |
There was a problem hiding this comment.
Maybe this wont work for some reason, but could we get away with using ctx.subscript.is_some() as a substitute for this Boolean? (We might have to set it to None when translating bodies of subscript getters and setters).
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Reuse lookupInstanceMember and static name binding in resolveSetterCallTarget, rather than duplicating the namespace lookup. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Also add a corpus test for a subscript parameter with a default value. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Replace the separate in_subscript_parameters flag with a check of ctx.subscript, which is now also set while translating subscript parameters. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>


xs[i]is now seen as a call to a member namedsubscript, i.e.xs.subscript(i).A subscript declaration can contain a getter and a setter, with a shared parameter list:
The getter and setter are extracted as a pair of
FunctionDeclarations both namedsubscript, with the setter having an extra parameter with external name#newValue(a "reserved" name that cannot clash with a user identifier). The use of a named parameter plays nicely with overload resolution and overriding. We clone the shared parameter list so both callables get their ownParameternodes (crude, but gets the job done). The setter is recognised on the QL side as aSetterMethodDeclaration, and is meant as a language-agnostic concept.When a call appears as an assignment target (
foo() = x), we use the existingTSetterCallbranch ofDataFlowCallto represent the call to aSetterMethodDeclaration. For type inference, we internally let the call resolve as normal (resolves to the getter) but expose a predicate specifically for resolving setter calls to their setter method.The PR also contains a very basic model of flow through subscript calls, where they are always seen as accesses to the "array element" content.
Relationship to Ruby
My thinking is that perhaps Ruby's
foo=methods can also be handled by being mapped to "setter methods". I've been going back and forth on the idea of fully adopting Ruby's convention, and representing the subscript-setter as a method namedsubscript=, but eventually decided against it as it lead to the proliferation of ad-hoc string manipulation in the codebase.DCA results
DCA results look good. Note that some of the "per node" stats decrease because we extract mode code now, due to subscript declarations having their body extracted, but the overall effect is a win. We get some new alerts and the percentage of resolved calls goes from 36% -> 40% due to subscript calls being resolved.