Skip to content

Unified: Support optional parameters in legacy MaD signatures - #23229

Merged
asgerf merged 5 commits into
mainfrom
asgerf-legacy-mad-optional-params
Nov 25, 2026
Merged

asgerf merged 5 commits into
mainfrom
asgerf-legacy-mad-optional-params

Conversation

@asgerf

@asgerf asgerf commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Makes it possible to mark parameters as optional in MaD models, by appending a ? after the name, but before the colon, e.g init(contentsOf:encoding?:) or positional(_:_?:).

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 be contentsOf:(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.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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"],

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Known temporary limitation; it will be fixed eventually but out of scope for this PR.

asgerf and others added 2 commits November 25, 2026 12:57
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>
@asgerf asgerf added the no-change-note-required This PR does not need a change note label Nov 25, 2026
@asgerf
asgerf marked this pull request as ready for review November 25, 2026 12:38
@asgerf
asgerf requested a review from a team as a code owner November 25, 2026 12:38

@hvitved hvitved left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

rawLabel = selector.getArgLabels().splitAt(":", n) and
rawLabel != ""
|
if rawLabel.matches("%?")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endswith

Comment on lines +309 to +311
if this.hasOptionalArgument()
then argLabels.regexpMatch(this.getArgLabelsRegexp())
else argLabels = this.getArgLabels()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    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>
@asgerf
asgerf merged commit 585ce95 into main Nov 25, 2026
16 checks passed
@asgerf
asgerf deleted the asgerf-legacy-mad-optional-params branch November 25, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants