Repository navigation
Unified: Support optional parameters in legacy MaD signatures - #23229
Merged
Merged
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved Swift model defects miss valid logging and JavaScript injection sinks and incorrectly classify String.write(to:) output streams as path-injection sinks.
5 open issues
What changed in this PR
Adds optional-parameter support to the unified library’s legacy Swift Models-as-Data signatures, allowing modeled calls to omit defaulted arguments.
Changes:
- Adds
?label parsing, optional call matching, and exact declaration matching. - Updates existing Swift and database models to recognize omitted defaults.
- Adds matching regression cases and updates path-injection expectations.
| File | Description |
|---|---|
unified/ql/test/query-tests/security/CWE-022/PathInjection/testPathInjection.swift |
Updates inline expectations for newly detected alerts. |
unified/ql/test/query-tests/security/CWE-022/PathInjection/PathInjectionTest.expected |
Records additional path-injection results. |
unified/ql/test/library-tests/mad/test.swift |
Adds optional-parameter and negative matching cases. |
unified/ql/test/library-tests/mad/test.ext.json |
Defines optional and required test signatures. |
unified/ql/test/library-tests/mad/test.expected |
Records expected source and sink matches. |
unified/ql/lib/ext/legacy-swift.model.json |
Marks defaulted parameters in existing Swift models. |
unified/ql/lib/ext/database.model.json |
Marks defaulted parameters in database models. |
unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll |
Implements optional-label parsing and matching. |
Review effort: Max
| ["", "Data", true, "sha384()", "", "", "Argument[self]", "weak-password-hash-input-SHA384", "manual"], | ||
| ["", "Data", true, "sha512()", "", "", "Argument[self]", "weak-password-hash-input-SHA512", "manual"], | ||
| ["", "Data", true, "write(to:options:)", "", "", "Argument[to:]", "path-injection", "manual"], | ||
| ["", "Data", true, "write(to:options?:)", "", "", "Argument[to:]", "path-injection", "manual"], |
Contributor
Author
There was a problem hiding this comment.
Known temporary limitation; it will be fixed eventually but out of scope for this PR.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
hvitved
reviewed
Nov 25, 2026
| rawLabel = selector.getArgLabels().splitAt(":", n) and | ||
| rawLabel != "" | ||
| | | ||
| if rawLabel.matches("%?") |
Comment on lines
+309
to
+311
| if this.hasOptionalArgument() | ||
| then argLabels.regexpMatch(this.getArgLabelsRegexp()) | ||
| else argLabels = this.getArgLabels() |
Contributor
There was a problem hiding this comment.
argLabels.regexpMatch(this.getArgLabelsRegexp())
or
not this.hasOptionalArgument() and
argLabels = this.getArgLabels()Co-authored-by: hvitved <hvitved@github.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
hvitved
approved these changes
Nov 25, 2026
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.

Makes it possible to mark parameters as optional in MaD models, by appending a
?after the name, but before the colon, e.ginit(contentsOf:encoding?:)orpositional(_:_?:).For callables with optional parameters, we now compile a regex to match the sequence of arg labels at call sites. For example, for
init(contentsOf:encoding?:)the regex would becontentsOf:(encoding:)?. This is currently only implemented for MaD - as discussed with @hvitved we'll eventually want to share the approach with call resolution in type inference, but it was left out of this PR.Regexes don't allow for efficient joins, and we want to limit how many regex tests we perform. Previously we'd do a join on the the entire arg-label string to quickly relate callables and call sites, but this of course fails when the callable has optional parameters that are omitted by the call site. We now compute a "join key" based on only the argument labels that are never optional for that method name. As before, we also join on the method name itself. We'll also join on the receiver type in the future, but this is explicitly omitted at this stage.
The MaD models are then updated. I relied on Copilot here.
DCA shows we detect 25% more sinks, one new reasonable-looking data flow alert, and a few minor gains on other metrics.