Repository navigation
Conversation
asgerf
force-pushed
the
asgerf-swift-subscript-support
branch
4 times, most recently
from
November 16, 2026 10:44
8a57ce8 to
b4ac4b1
Compare
| } | ||
|
|
||
| /** Gets the node corresponding to the field `type_parameter`. */ | ||
| final F::TypeParameter getTypeParameter(int i) { |
| * The new value is passed as an extra positional argument after the explicit ones. | ||
| */ | ||
| pragma[nomagic] | ||
| private predicate setterCallSelector(CallExpr call, string name, string argLabels) { |
asgerf
force-pushed
the
asgerf-swift-subscript-support
branch
2 times, most recently
from
November 18, 2026 13:07
ff2cdda to
7ecb52d
Compare
Extract subscript accesses as calls to a 'subscript' member, and subscript declarations as a 'subscript' getter method followed by a 'subscript' accessor of kind 'setter_method'. The setter clones the subscript parameters and takes the new value as an extra trailing positional parameter. 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 as 'name:set', with the value as the last argument label, e.g. 'subscript:set(_:_:)'. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
asgerf
force-pushed
the
asgerf-swift-subscript-support
branch
from
November 18, 2026 13:52
7ecb52d to
eeab6e5
Compare
asgerf
marked this pull request as ready for review
November 18, 2026 19:50
Contributor
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)
Comment on lines
+34
to
36
| node1.isReceiverPostUpdateEx(call) and | ||
| step.value() and | ||
| node2.isPostUpdate(receiverExpr) |
Comment on lines
+116
to
+119
| pos.asPositional() = | ||
| count(Argument arg | | ||
| arg = call.asSetterCall().(CallExpr).getAnArgument() and arg.isPositional() | ||
| ) |
| CachedStage::ref() and | ||
| exists(MemberAccessExpr callee, ClassLikeDeclaration cls, NamespaceNode ns | | ||
| callee = ce.getCallee() and | ||
| ns.getMember(callee.getMemberName()).isIdentifier(result.getNameNode()) |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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 is extracted as a
FunctionDeclarationnamedsubscript, and the setter becomes anAccessorDeclarationwith the accessor-kindsetter_method. We clone the 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 just let the call resolve as normal (resolves to the getter).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.