diff --git a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll index e89af066eb09..afbe0d3d62b7 100644 --- a/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll +++ b/unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll @@ -235,8 +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. 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"] 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/dotted-import.swift b/unified/ql/test/library-tests/mad-qualified-types/dotted-import.swift new file mode 100644 index 000000000000..59548ec93632 --- /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 // $ isSource=remote + _ = qualifiedURL.lines // $ isSource=remote + + 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 new file mode 100644 index 000000000000..874aceb83e95 --- /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 // $ isSource=remote + + 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/no-import.swift b/unified/ql/test/library-tests/mad-qualified-types/no-import.swift new file mode 100644 index 000000000000..3ae1f7172d2f --- /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 // $ isSource=remote + + 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 + + // Keep nested model types distinct from module qualifiers. + _ = String.Index(encodedOffset: 0) // $ 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/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/other-modules.swift b/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift new file mode 100644 index 000000000000..f2f3aa4adea6 --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/other-modules.swift @@ -0,0 +1,11 @@ +import Alamofire +import UIKit + +func withOtherModelModules( + qualifiedResponse: Alamofire.DataResponse, + qualifiedTextField: UIKit.UITextField +) { + _ = qualifiedResponse.value // $ isSource=remote + + _ = qualifiedTextField.text // $ isSource=local +} 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..6b5bae909591 --- /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 // $ isSource=remote + _ = qualifiedURL.lines // $ isSource=remote + + 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 new file mode 100644 index 000000000000..6ed94bbce643 --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/test.expected @@ -0,0 +1,37 @@ +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 | +| 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 | +| scoped-import.swift:10:9:10:23 | ... .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..85088566130b --- /dev/null +++ b/unified/ql/test/library-tests/mad-qualified-types/test.ql @@ -0,0 +1,16 @@ +private import unified +private import codeql.unified.internal.mad.LegacyMaD + +query predicate isSource(DataFlow::Node node, string kind) { + Models::isSource(node, kind) and + node.getLocation().getFile().fromSource() +} + +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() +} 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