From b8a90768e9bdbaa267e980151d41cf7de641b4bf Mon Sep 17 00:00:00 2001 From: Asger F Date: Wed, 25 Nov 2026 09:19:03 +0100 Subject: [PATCH 1/6] Unified: Add tests for qualified model types and imports Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../mad-qualified-types/dotted-import.swift | 12 ++++++++++++ .../mad-qualified-types/module-import.swift | 12 ++++++++++++ .../mad-qualified-types/no-import.swift | 14 ++++++++++++++ .../mad-qualified-types/other-module.swift | 10 ++++++++++ .../mad-qualified-types/scoped-import.swift | 12 ++++++++++++ .../mad-qualified-types/test.expected | 11 +++++++++++ .../test/library-tests/mad-qualified-types/test.ql | 10 ++++++++++ .../library-tests/mad-qualified-types/test.qlref | 2 ++ 8 files changed, 83 insertions(+) create mode 100644 unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift create mode 100644 unified/ql/test/library-tests/mad-qualified-types/module-import.swift create mode 100644 unified/ql/test/library-tests/mad-qualified-types/no-import.swift create mode 100644 unified/ql/test/library-tests/mad-qualified-types/other-module.swift create mode 100644 unified/ql/test/library-tests/mad-qualified-types/scoped-import.swift create mode 100644 unified/ql/test/library-tests/mad-qualified-types/test.expected create mode 100644 unified/ql/test/library-tests/mad-qualified-types/test.ql create mode 100644 unified/ql/test/library-tests/mad-qualified-types/test.qlref diff --git a/unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift b/unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift new file mode 100644 index 000000000000..479a73315bf2 --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift @@ -0,0 +1,12 @@ +import Foundation.URL + +func withDottedImport(url: URL, qualifiedURL: Foundation.URL, path: String) { + _ = url.lines // $ MISSING: isSource=remote + _ = qualifiedURL.lines // $ MISSING: isSource=remote + + let unqualified = URL(fileURLWithPath: path) // $ MISSING: constructor=URL + _ = unqualified.lines // $ MISSING: isSource=remote + let qualified = Foundation.URL(fileURLWithPath: path) // $ MISSING: constructor=URL + _ = qualified.lines // $ MISSING: isSource=remote + _ = Foundation.URL.init(fileURLWithPath: path) // $ MISSING: constructor=URL +} diff --git a/unified/ql/test/library-tests/mad-qualified-types/module-import.swift b/unified/ql/test/library-tests/mad-qualified-types/module-import.swift new file mode 100644 index 000000000000..2fe9bd0dbf1e --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/module-import.swift @@ -0,0 +1,12 @@ +import Foundation + +func withModuleImport(url: URL, qualifiedURL: Foundation.URL, path: String) { + _ = url.lines // $ isSource=remote + _ = qualifiedURL.lines // $ MISSING: isSource=remote + + let unqualified = URL(fileURLWithPath: path) // $ constructor=URL + _ = unqualified.lines // $ isSource=remote + let qualified = Foundation.URL(fileURLWithPath: path) // $ MISSING: constructor=URL + _ = qualified.lines // $ MISSING: isSource=remote + _ = Foundation.URL.init(fileURLWithPath: path) // $ MISSING: constructor=URL +} diff --git a/unified/ql/test/library-tests/mad-qualified-types/no-import.swift b/unified/ql/test/library-tests/mad-qualified-types/no-import.swift new file mode 100644 index 000000000000..2acc119fe400 --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/no-import.swift @@ -0,0 +1,14 @@ +func withoutImports(url: URL, qualifiedURL: Foundation.URL, path: String) { + _ = url.lines // $ isSource=remote + _ = qualifiedURL.lines // $ MISSING: isSource=remote + + let unqualified = URL(fileURLWithPath: path) // $ constructor=URL + _ = unqualified.lines // $ isSource=remote + let qualified = Foundation.URL(fileURLWithPath: path) // $ MISSING: constructor=URL + _ = qualified.lines // $ MISSING: isSource=remote + _ = Foundation.URL.init(fileURLWithPath: path) // $ MISSING: constructor=URL + + // Keep nested model types distinct from module qualifiers. + _ = String.Index(encodedOffset: 0) // $ constructor=String.Index isSink=string-length + _ = Foundation.String.Index(encodedOffset: 0) // $ MISSING: constructor=String.Index isSink=string-length +} diff --git a/unified/ql/test/library-tests/mad-qualified-types/other-module.swift b/unified/ql/test/library-tests/mad-qualified-types/other-module.swift new file mode 100644 index 000000000000..1453c9387997 --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/other-module.swift @@ -0,0 +1,10 @@ +import Other +import Other.URL + +// Types named `URL` in unrelated modules must not match the Foundation models. +func withOtherModule(url: Other.URL, path: String) { + _ = url.lines + let qualified = Other.URL(fileURLWithPath: path) + _ = qualified.lines + _ = Other.URL.init(fileURLWithPath: path) +} diff --git a/unified/ql/test/library-tests/mad-qualified-types/scoped-import.swift b/unified/ql/test/library-tests/mad-qualified-types/scoped-import.swift new file mode 100644 index 000000000000..022401f5142f --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/scoped-import.swift @@ -0,0 +1,12 @@ +import struct Foundation.URL + +func withScopedImport(url: URL, qualifiedURL: Foundation.URL, path: String) { + _ = url.lines // $ MISSING: isSource=remote + _ = qualifiedURL.lines // $ MISSING: isSource=remote + + let unqualified = URL(fileURLWithPath: path) // $ MISSING: constructor=URL + _ = unqualified.lines // $ MISSING: isSource=remote + let qualified = Foundation.URL(fileURLWithPath: path) // $ MISSING: constructor=URL + _ = qualified.lines // $ MISSING: isSource=remote + _ = Foundation.URL.init(fileURLWithPath: path) // $ MISSING: constructor=URL +} diff --git a/unified/ql/test/library-tests/mad-qualified-types/test.expected b/unified/ql/test/library-tests/mad-qualified-types/test.expected new file mode 100644 index 000000000000..f4573ec39e4e --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/test.expected @@ -0,0 +1,11 @@ +constructor +| module-import.swift:7:23:7:48 | URL(...) | URL | +| no-import.swift:5:23:5:48 | URL(...) | URL | +| no-import.swift:12:9:12:38 | ... .Index(...) | String.Index | +isSink +| no-import.swift:12:37:12:37 | 0 | string-length | +isSource +| module-import.swift:4:9:4:17 | ... .lines | remote | +| module-import.swift:8:9:8:25 | ... .lines | remote | +| no-import.swift:2:9:2:17 | ... .lines | remote | +| no-import.swift:6:9:6:25 | ... .lines | remote | diff --git a/unified/ql/test/library-tests/mad-qualified-types/test.ql b/unified/ql/test/library-tests/mad-qualified-types/test.ql new file mode 100644 index 000000000000..d3a698748529 --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/test.ql @@ -0,0 +1,10 @@ +private import unified +private import codeql.unified.internal.mad.LegacyMaD + +query predicate isSource = Models::isSource/2; + +query predicate isSink = Models::isSink/2; + +query predicate constructor(CallExpr call, string type) { + call = any(TypeSelector selector | type = selector).getAConstructorCall() +} diff --git a/unified/ql/test/library-tests/mad-qualified-types/test.qlref b/unified/ql/test/library-tests/mad-qualified-types/test.qlref new file mode 100644 index 000000000000..ab6773f15f90 --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/test.qlref @@ -0,0 +1,2 @@ +query: test.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql From df5309efe70c89efb420c8e1cc71d2c679b84337 Mon Sep 17 00:00:00 2001 From: Asger F Date: Wed, 25 Nov 2026 09:22:45 +0100 Subject: [PATCH 2/6] Unified: Fix model matching through module qualifiers Expose the legacy model namespace under the Swift and Foundation module names so qualified references and selective import aliases resolve to the existing type selectors. Unrelated modules are not mapped, so types such as `Other.URL` do not match Foundation models. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../unified/internal/StaticNameBinding.qll | 10 ++++++++ .../mad-qualified-types/dotted-import.swift | 14 +++++------ .../mad-qualified-types/module-import.swift | 8 +++---- .../mad-qualified-types/no-import.swift | 10 ++++---- .../mad-qualified-types/scoped-import.swift | 14 +++++------ .../mad-qualified-types/test.expected | 24 +++++++++++++++++++ 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll index e89af066eb09..4bede69f317d 100644 --- a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll +++ b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll @@ -237,6 +237,16 @@ predicate storeStep(NameBindingNode node1, string name, NameBindingNode node2) { node1.isTypeSelector(type.append(name)) and node2.isTypeSelector(type) ) + or + // Legacy Swift models omit module names. Expose their root namespace through + // module qualifiers as well as directly in the top-level scope. + node1.isTypeSelector("") and + name = ["Swift", "Foundation"] and + ( + node2.isModuleRoot() + or + node2.isLocalNamespace(any(TopLevel top).getBody()) + ) } predicate valueStep(NameBindingNode node1, NameBindingNode node2) { diff --git a/unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift b/unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift index 479a73315bf2..59548ec93632 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift +++ b/unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift @@ -1,12 +1,12 @@ import Foundation.URL func withDottedImport(url: URL, qualifiedURL: Foundation.URL, path: String) { - _ = url.lines // $ MISSING: isSource=remote - _ = qualifiedURL.lines // $ MISSING: isSource=remote + _ = url.lines // $ isSource=remote + _ = qualifiedURL.lines // $ isSource=remote - let unqualified = URL(fileURLWithPath: path) // $ MISSING: constructor=URL - _ = unqualified.lines // $ MISSING: isSource=remote - let qualified = Foundation.URL(fileURLWithPath: path) // $ MISSING: constructor=URL - _ = qualified.lines // $ MISSING: isSource=remote - _ = Foundation.URL.init(fileURLWithPath: path) // $ MISSING: constructor=URL + let unqualified = URL(fileURLWithPath: path) // $ constructor=URL + _ = unqualified.lines // $ isSource=remote + let qualified = Foundation.URL(fileURLWithPath: path) // $ constructor=URL + _ = qualified.lines // $ isSource=remote + _ = Foundation.URL.init(fileURLWithPath: path) // $ constructor=URL } diff --git a/unified/ql/test/library-tests/mad-qualified-types/module-import.swift b/unified/ql/test/library-tests/mad-qualified-types/module-import.swift index 2fe9bd0dbf1e..874aceb83e95 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/module-import.swift +++ b/unified/ql/test/library-tests/mad-qualified-types/module-import.swift @@ -2,11 +2,11 @@ import Foundation func withModuleImport(url: URL, qualifiedURL: Foundation.URL, path: String) { _ = url.lines // $ isSource=remote - _ = qualifiedURL.lines // $ MISSING: isSource=remote + _ = qualifiedURL.lines // $ isSource=remote let unqualified = URL(fileURLWithPath: path) // $ constructor=URL _ = unqualified.lines // $ isSource=remote - let qualified = Foundation.URL(fileURLWithPath: path) // $ MISSING: constructor=URL - _ = qualified.lines // $ MISSING: isSource=remote - _ = Foundation.URL.init(fileURLWithPath: path) // $ MISSING: constructor=URL + let qualified = Foundation.URL(fileURLWithPath: path) // $ constructor=URL + _ = qualified.lines // $ isSource=remote + _ = Foundation.URL.init(fileURLWithPath: path) // $ constructor=URL } diff --git a/unified/ql/test/library-tests/mad-qualified-types/no-import.swift b/unified/ql/test/library-tests/mad-qualified-types/no-import.swift index 2acc119fe400..3ae1f7172d2f 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/no-import.swift +++ b/unified/ql/test/library-tests/mad-qualified-types/no-import.swift @@ -1,14 +1,14 @@ func withoutImports(url: URL, qualifiedURL: Foundation.URL, path: String) { _ = url.lines // $ isSource=remote - _ = qualifiedURL.lines // $ MISSING: isSource=remote + _ = qualifiedURL.lines // $ isSource=remote let unqualified = URL(fileURLWithPath: path) // $ constructor=URL _ = unqualified.lines // $ isSource=remote - let qualified = Foundation.URL(fileURLWithPath: path) // $ MISSING: constructor=URL - _ = qualified.lines // $ MISSING: isSource=remote - _ = Foundation.URL.init(fileURLWithPath: path) // $ MISSING: constructor=URL + let qualified = Foundation.URL(fileURLWithPath: path) // $ constructor=URL + _ = qualified.lines // $ isSource=remote + _ = Foundation.URL.init(fileURLWithPath: path) // $ constructor=URL // Keep nested model types distinct from module qualifiers. _ = String.Index(encodedOffset: 0) // $ constructor=String.Index isSink=string-length - _ = Foundation.String.Index(encodedOffset: 0) // $ MISSING: constructor=String.Index isSink=string-length + _ = Foundation.String.Index(encodedOffset: 0) // $ constructor=String.Index isSink=string-length } diff --git a/unified/ql/test/library-tests/mad-qualified-types/scoped-import.swift b/unified/ql/test/library-tests/mad-qualified-types/scoped-import.swift index 022401f5142f..6b5bae909591 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/scoped-import.swift +++ b/unified/ql/test/library-tests/mad-qualified-types/scoped-import.swift @@ -1,12 +1,12 @@ import struct Foundation.URL func withScopedImport(url: URL, qualifiedURL: Foundation.URL, path: String) { - _ = url.lines // $ MISSING: isSource=remote - _ = qualifiedURL.lines // $ MISSING: isSource=remote + _ = url.lines // $ isSource=remote + _ = qualifiedURL.lines // $ isSource=remote - let unqualified = URL(fileURLWithPath: path) // $ MISSING: constructor=URL - _ = unqualified.lines // $ MISSING: isSource=remote - let qualified = Foundation.URL(fileURLWithPath: path) // $ MISSING: constructor=URL - _ = qualified.lines // $ MISSING: isSource=remote - _ = Foundation.URL.init(fileURLWithPath: path) // $ MISSING: constructor=URL + let unqualified = URL(fileURLWithPath: path) // $ constructor=URL + _ = unqualified.lines // $ isSource=remote + let qualified = Foundation.URL(fileURLWithPath: path) // $ constructor=URL + _ = qualified.lines // $ isSource=remote + _ = Foundation.URL.init(fileURLWithPath: path) // $ constructor=URL } diff --git a/unified/ql/test/library-tests/mad-qualified-types/test.expected b/unified/ql/test/library-tests/mad-qualified-types/test.expected index f4573ec39e4e..acaa8d91e1e5 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/test.expected +++ b/unified/ql/test/library-tests/mad-qualified-types/test.expected @@ -1,11 +1,35 @@ constructor +| dotted-import.swift:7:23:7:48 | URL(...) | URL | +| dotted-import.swift:9:21:9:57 | ... .URL(...) | URL | +| dotted-import.swift:11:9:11:50 | ... .init(...) | URL | | module-import.swift:7:23:7:48 | URL(...) | URL | +| module-import.swift:9:21:9:57 | ... .URL(...) | URL | +| module-import.swift:11:9:11:50 | ... .init(...) | URL | | no-import.swift:5:23:5:48 | URL(...) | URL | +| no-import.swift:7:21:7:57 | ... .URL(...) | URL | +| no-import.swift:9:9:9:50 | ... .init(...) | URL | | no-import.swift:12:9:12:38 | ... .Index(...) | String.Index | +| no-import.swift:13:9:13:49 | ... .Index(...) | String.Index | +| scoped-import.swift:7:23:7:48 | URL(...) | URL | +| scoped-import.swift:9:21:9:57 | ... .URL(...) | URL | +| scoped-import.swift:11:9:11:50 | ... .init(...) | URL | isSink | no-import.swift:12:37:12:37 | 0 | string-length | +| no-import.swift:13:48:13:48 | 0 | string-length | isSource +| dotted-import.swift:4:9:4:17 | ... .lines | remote | +| dotted-import.swift:5:9:5:26 | ... .lines | remote | +| dotted-import.swift:8:9:8:25 | ... .lines | remote | +| dotted-import.swift:10:9:10:23 | ... .lines | remote | | module-import.swift:4:9:4:17 | ... .lines | remote | +| module-import.swift:5:9:5:26 | ... .lines | remote | | module-import.swift:8:9:8:25 | ... .lines | remote | +| module-import.swift:10:9:10:23 | ... .lines | remote | | no-import.swift:2:9:2:17 | ... .lines | remote | +| no-import.swift:3:9:3:26 | ... .lines | remote | | no-import.swift:6:9:6:25 | ... .lines | remote | +| no-import.swift:8:9:8:23 | ... .lines | remote | +| scoped-import.swift:4:9:4:17 | ... .lines | remote | +| scoped-import.swift:5:9:5:26 | ... .lines | remote | +| scoped-import.swift:8:9:8:25 | ... .lines | remote | +| scoped-import.swift:10:9:10:23 | ... .lines | remote | From 9728ab40c19d8bc66af79f8056f082143ba3b2c9 Mon Sep 17 00:00:00 2001 From: Asger F Date: Wed, 25 Nov 2026 15:27:30 +0100 Subject: [PATCH 3/6] Unified: Add TODO for model module names Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll | 1 + 1 file changed, 1 insertion(+) diff --git a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll index 4bede69f317d..5298d7c707b4 100644 --- a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll +++ b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll @@ -240,6 +240,7 @@ predicate storeStep(NameBindingNode node1, string name, NameBindingNode node2) { or // Legacy Swift models omit module names. Expose their root namespace through // module qualifiers as well as directly in the top-level scope. + // TODO: MaD models should include module names to avoid this special case. node1.isTypeSelector("") and name = ["Swift", "Foundation"] and ( From 72c8362b9183c6b0d16d72aa81a5a6dbcb7ef0e8 Mon Sep 17 00:00:00 2001 From: Asger F Date: Thu, 26 Nov 2026 08:31:21 +0100 Subject: [PATCH 4/6] Unified: Add model module qualifiers Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../codeql/unified/internal/StaticNameBinding.qll | 9 +++++++-- .../mad-qualified-types/other-modules.swift | 15 +++++++++++++++ .../mad-qualified-types/test.expected | 4 ++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 unified/ql/test/library-tests/mad-qualified-types/other-modules.swift diff --git a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll index 5298d7c707b4..9c7cafe1619b 100644 --- a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll +++ b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll @@ -239,10 +239,15 @@ predicate storeStep(NameBindingNode node1, string name, NameBindingNode node2) { ) or // Legacy Swift models omit module names. Expose their root namespace through - // module qualifiers as well as directly in the top-level scope. + // known module qualifiers as well as directly in the top-level scope. // TODO: MaD models should include module names to avoid this special case. node1.isTypeSelector("") and - name = ["Swift", "Foundation"] and + name = + [ + "Swift", "Foundation", "FoundationNetworking", "Alamofire", "AppleArchive", "Crypto", + "CryptoKit", "CryptoSwift", "JavaScriptCore", "Network", "NIOPosix", "OSLog", "SQLite", + "System", "UIKit", "WebKit", "Zip", "os" + ] and ( node2.isModuleRoot() or diff --git a/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift b/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift new file mode 100644 index 000000000000..60e3d3f6ec08 --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift @@ -0,0 +1,15 @@ +import Alamofire +import UIKit + +func withOtherModelModules( + response: DataResponse, + qualifiedResponse: Alamofire.DataResponse, + textField: UITextField, + qualifiedTextField: UIKit.UITextField +) { + _ = response.value // $ isSource=remote + _ = qualifiedResponse.value // $ isSource=remote + + _ = textField.text // $ isSource=local + _ = qualifiedTextField.text // $ isSource=local +} diff --git a/unified/ql/test/library-tests/mad-qualified-types/test.expected b/unified/ql/test/library-tests/mad-qualified-types/test.expected index acaa8d91e1e5..617d8d5cbdc3 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/test.expected +++ b/unified/ql/test/library-tests/mad-qualified-types/test.expected @@ -29,6 +29,10 @@ isSource | no-import.swift:3:9:3:26 | ... .lines | remote | | no-import.swift:6:9:6:25 | ... .lines | remote | | no-import.swift:8:9:8:23 | ... .lines | remote | +| other-modules.swift:10:9:10:22 | ... .value | remote | +| other-modules.swift:11:9:11:31 | ... .value | remote | +| other-modules.swift:13:9:13:22 | ... .text | local | +| other-modules.swift:14:9:14:31 | ... .text | local | | scoped-import.swift:4:9:4:17 | ... .lines | remote | | scoped-import.swift:5:9:5:26 | ... .lines | remote | | scoped-import.swift:8:9:8:25 | ... .lines | remote | From bb49f9d86149088e30d583d9111d5f197b75e830 Mon Sep 17 00:00:00 2001 From: Asger F Date: Thu, 26 Nov 2026 11:06:01 +0100 Subject: [PATCH 5/6] Unified: Simplify constructor model test cast Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- unified/ql/test/library-tests/mad-qualified-types/test.ql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unified/ql/test/library-tests/mad-qualified-types/test.ql b/unified/ql/test/library-tests/mad-qualified-types/test.ql index d3a698748529..2cfc5f84e2be 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/test.ql +++ b/unified/ql/test/library-tests/mad-qualified-types/test.ql @@ -6,5 +6,5 @@ query predicate isSource = Models::isSource/2; query predicate isSink = Models::isSink/2; query predicate constructor(CallExpr call, string type) { - call = any(TypeSelector selector | type = selector).getAConstructorCall() + call = type.(TypeSelector).getAConstructorCall() } From 40d244f3864eae245a3147201b45776961a02b38 Mon Sep 17 00:00:00 2001 From: Asger F Date: Thu, 26 Nov 2026 11:24:23 +0100 Subject: [PATCH 6/6] Unified: Match models for qualified Swift types Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../unified/internal/StaticNameBinding.qll | 31 ++++-- .../codeql/unified/internal/mad/LegacyMaD.qll | 97 ++++++++++++++++--- .../mad-qualified-types/other-modules.swift | 4 - .../mad-qualified-types/test.expected | 6 +- .../library-tests/mad-qualified-types/test.ql | 10 +- 5 files changed, 113 insertions(+), 35 deletions(-) diff --git a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll index 9c7cafe1619b..afbe0d3d62b7 100644 --- a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll +++ b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll @@ -235,24 +235,37 @@ predicate storeStep(NameBindingNode node1, string name, NameBindingNode node2) { or exists(MaD::TypeSelector type | node1.isTypeSelector(type.append(name)) and - node2.isTypeSelector(type) + node2.isTypeSelector(type) and + not ( + type = "" and + name = + [ + "Swift", "Foundation", "FoundationNetworking", "Alamofire", "AppleArchive", "Crypto", + "CryptoKit", "CryptoSwift", "JavaScriptCore", "Network", "NIOPosix", "OSLog", "SQLite", + "System", "UIKit", "WebKit", "Zip" + ] + ) ) or - // Legacy Swift models omit module names. Expose their root namespace through - // known module qualifiers as well as directly in the top-level scope. + // Legacy Swift models omit module names. Add module qualifiers to model types + // without connecting them to the shared root used by global function models. // TODO: MaD models should include module names to avoid this special case. node1.isTypeSelector("") and - name = - [ - "Swift", "Foundation", "FoundationNetworking", "Alamofire", "AppleArchive", "Crypto", - "CryptoKit", "CryptoSwift", "JavaScriptCore", "Network", "NIOPosix", "OSLog", "SQLite", - "System", "UIKit", "WebKit", "Zip", "os" - ] and + name = ["Swift", "Foundation"] and ( node2.isModuleRoot() or node2.isLocalNamespace(any(TopLevel top).getBody()) ) + or + node1.isTypeSelector(name) and + name = + [ + "Swift", "Foundation", "FoundationNetworking", "Alamofire", "AppleArchive", "Crypto", + "CryptoKit", "CryptoSwift", "JavaScriptCore", "Network", "NIOPosix", "OSLog", "SQLite", + "System", "UIKit", "WebKit", "Zip" + ] and + node2.isModuleRoot() } predicate valueStep(NameBindingNode node1, NameBindingNode node2) { diff --git a/unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll b/unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll index 80bf5e4783e8..f131f7f5ca17 100644 --- a/unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll +++ b/unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll @@ -42,6 +42,61 @@ private predicate parsedRawMethodName(string rawName, string name, string argLab ) } +bindingset[type] +private predicate legacyTypeModule(string type, string moduleName) { + type = ["DataResponse", "DownloadResponse"] and moduleName = "Alamofire" + or + type = "ArchiveByteStream" and moduleName = "AppleArchive" + or + type = + [ + "AES", "Blowfish", "Digest", "Insecure.MD5", "Insecure.SHA1", "MD5", "SHA1", "SHA2", "SHA3", + "SHA256", "SHA384", "SHA512" + ] and + moduleName = "CryptoSwift" + or + type = ["Insecure.MD5", "Insecure.SHA1", "SHA256", "SHA384", "SHA512"] and + moduleName = "CryptoKit" + or + type = ["Insecure.MD5", "Insecure.SHA1", "SHA256", "SHA384", "SHA512"] and + moduleName = "Crypto" + or + type = ["JSContext", "JSValue"] and moduleName = "JavaScriptCore" + or + type = "NWConnection" and moduleName = "Network" + or + type = "NIOFileHandle" and moduleName = "NIOPosix" + or + type = "Logger" and moduleName = "OSLog" + or + type = ["Blob", "Expression", "ExpressionType"] and moduleName = "SQLite" + or + type = "FilePath" and moduleName = "System" + or + type = + [ + "UIApplicationDelegate", "UISceneDelegate", "UITextField", "UITextFieldDelegate", + "UITextInput", "UITextViewDelegate", "UIWebView" + ] and + moduleName = "UIKit" + or + type = + ["WKScriptMessageHandler", "WKUserContentController", "WKUserScript", "WKWebView", "WebView"] and + moduleName = "WebKit" + or + type = "Zip" and moduleName = "Zip" + or + type = ["URLSession", "URLSessionConfiguration"] and moduleName = "FoundationNetworking" +} + +bindingset[rawType] +private string getModelTypeName(string rawType) { + result = rawType + or + result = + any(string moduleName | legacyTypeModule(rawType, moduleName) | moduleName + "." + rawType) +} + private string getSimpleNameFromExpr(Expr e) { result = e.(Identifier).getValue() or @@ -183,21 +238,27 @@ private predicate propertyAccessSelector(MemberAccess access, T::Type baseType, private newtype TSelector = MkCallSelector(string type, boolean subtypes, string name, string argLabels, string argTypes) { - exists(string rawName | - legacySourceModel(_, type, subtypes, rawName, argTypes, _, _, _, _) - or - legacySinkModel(_, type, subtypes, rawName, argTypes, _, _, _, _) - or - legacySummaryModel(_, type, subtypes, rawName, argTypes, _, _, _, _, _) + exists(string rawType, string rawName | + ( + legacySourceModel(_, rawType, subtypes, rawName, argTypes, _, _, _, _) + or + legacySinkModel(_, rawType, subtypes, rawName, argTypes, _, _, _, _) + or + legacySummaryModel(_, rawType, subtypes, rawName, argTypes, _, _, _, _, _) + ) and + type = getModelTypeName(rawType) | parsedRawMethodName(rawName, name, argLabels) ) } or MkPropertySelector(string type, boolean subtypes, string name) { - ( - legacySourceModel(_, type, subtypes, name, _, _, _, _, _) - or - legacySinkModel(_, type, subtypes, name, _, _, _, _, _) + exists(string rawType | + ( + legacySourceModel(_, rawType, subtypes, name, _, _, _, _, _) + or + legacySinkModel(_, rawType, subtypes, name, _, _, _, _, _) + ) and + type = getModelTypeName(rawType) ) and isRawPropertyName(name) } @@ -555,8 +616,9 @@ private DataFlow::Node getSinkFromPropertyAccess(MemberAccess access, AccessPath private predicate normalizedSourceModel( Selector selector, AccessPath output, string kind, string provenance ) { - exists(string type, boolean subtypes, string rawName, string argTypes | - legacySourceModel(_, type, subtypes, rawName, argTypes, _, output, kind, provenance) + exists(string rawType, string type, boolean subtypes, string rawName, string argTypes | + legacySourceModel(_, rawType, subtypes, rawName, argTypes, _, output, kind, provenance) and + type = getModelTypeName(rawType) | exists(string name, string argLabels | parsedRawMethodName(rawName, name, argLabels) and @@ -570,8 +632,9 @@ private predicate normalizedSourceModel( private predicate normalizedSinkModel( Selector selector, AccessPath output, string kind, string provenance ) { - exists(string type, boolean subtypes, string rawName, string argTypes | - legacySinkModel(_, type, subtypes, rawName, argTypes, _, output, kind, provenance) + exists(string rawType, string type, boolean subtypes, string rawName, string argTypes | + legacySinkModel(_, rawType, subtypes, rawName, argTypes, _, output, kind, provenance) and + type = getModelTypeName(rawType) | exists(string name, string argLabels | parsedRawMethodName(rawName, name, argLabels) and @@ -586,9 +649,11 @@ predicate normalizedSummaryModel( Selector selector, string input, string output, string kind, string provenance ) { exists( - string type, boolean subtypes, string rawName, string name, string argLabels, string argTypes + string rawType, string type, boolean subtypes, string rawName, string name, string argLabels, + string argTypes | - legacySummaryModel(_, type, subtypes, rawName, argTypes, _, input, output, kind, provenance) and + legacySummaryModel(_, rawType, subtypes, rawName, argTypes, _, input, output, kind, provenance) and + type = getModelTypeName(rawType) and parsedRawMethodName(rawName, name, argLabels) and selector = MkCallSelector(type, subtypes, name, argLabels, argTypes) ) diff --git a/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift b/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift index 60e3d3f6ec08..f2f3aa4adea6 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift +++ b/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift @@ -2,14 +2,10 @@ import Alamofire import UIKit func withOtherModelModules( - response: DataResponse, qualifiedResponse: Alamofire.DataResponse, - textField: UITextField, qualifiedTextField: UIKit.UITextField ) { - _ = response.value // $ isSource=remote _ = qualifiedResponse.value // $ isSource=remote - _ = textField.text // $ isSource=local _ = qualifiedTextField.text // $ isSource=local } diff --git a/unified/ql/test/library-tests/mad-qualified-types/test.expected b/unified/ql/test/library-tests/mad-qualified-types/test.expected index 617d8d5cbdc3..6ed94bbce643 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/test.expected +++ b/unified/ql/test/library-tests/mad-qualified-types/test.expected @@ -29,10 +29,8 @@ isSource | no-import.swift:3:9:3:26 | ... .lines | remote | | no-import.swift:6:9:6:25 | ... .lines | remote | | no-import.swift:8:9:8:23 | ... .lines | remote | -| other-modules.swift:10:9:10:22 | ... .value | remote | -| other-modules.swift:11:9:11:31 | ... .value | remote | -| other-modules.swift:13:9:13:22 | ... .text | local | -| other-modules.swift:14:9:14:31 | ... .text | local | +| other-modules.swift:8:9:8:31 | ... .value | remote | +| other-modules.swift:10:9:10:31 | ... .text | local | | scoped-import.swift:4:9:4:17 | ... .lines | remote | | scoped-import.swift:5:9:5:26 | ... .lines | remote | | scoped-import.swift:8:9:8:25 | ... .lines | remote | diff --git a/unified/ql/test/library-tests/mad-qualified-types/test.ql b/unified/ql/test/library-tests/mad-qualified-types/test.ql index 2cfc5f84e2be..85088566130b 100644 --- a/unified/ql/test/library-tests/mad-qualified-types/test.ql +++ b/unified/ql/test/library-tests/mad-qualified-types/test.ql @@ -1,9 +1,15 @@ private import unified private import codeql.unified.internal.mad.LegacyMaD -query predicate isSource = Models::isSource/2; +query predicate isSource(DataFlow::Node node, string kind) { + Models::isSource(node, kind) and + node.getLocation().getFile().fromSource() +} -query predicate isSink = Models::isSink/2; +query predicate isSink(DataFlow::Node node, string kind) { + Models::isSink(node, kind) and + node.getLocation().getFile().fromSource() +} query predicate constructor(CallExpr call, string type) { call = type.(TypeSelector).getAConstructorCall()