Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion unified/ql/lib/codeql/unified/internal/StaticNameBinding.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
97 changes: 81 additions & 16 deletions unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Alamofire
import UIKit

func withOtherModelModules(
qualifiedResponse: Alamofire.DataResponse,
qualifiedTextField: UIKit.UITextField
) {
_ = qualifiedResponse.value // $ isSource=remote

_ = qualifiedTextField.text // $ isSource=local
}
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 37 additions & 0 deletions unified/ql/test/library-tests/mad-qualified-types/test.expected
Original file line number Diff line number Diff line change
@@ -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 |
16 changes: 16 additions & 0 deletions unified/ql/test/library-tests/mad-qualified-types/test.ql
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
query: test.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Loading