From 5ba7475c785d74918d6371c263524a6d55a9eb58 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:12:49 +0000 Subject: [PATCH 1/2] Initial plan From 0a70076bddc21d476f963fa7a690457b71fd6d44 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 16 Oct 2026 16:00:18 +0000 Subject: [PATCH 2/2] Add Swift path-injection-extended query with deep link URL components models Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> --- .github/workflows/codeql-qlt-unit-tests.yml | 3 +- .../PathInjectionExtended.ql | 28 +++ .../PathInjectionModels.qll | 74 ++++++++ languages/swift/custom/src/qlpack.yml | 5 + .../PathInjectionExtendedTest.expected | 0 .../PathInjectionExtendedTest.ql | 22 +++ .../test/PathInjectionExtended/deeplink.swift | 159 ++++++++++++++++++ languages/swift/custom/test/qlpack.yml | 5 + 8 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 languages/swift/custom/src/PathInjectionExtended/PathInjectionExtended.ql create mode 100644 languages/swift/custom/src/PathInjectionExtended/PathInjectionModels.qll create mode 100644 languages/swift/custom/src/qlpack.yml create mode 100644 languages/swift/custom/test/PathInjectionExtended/PathInjectionExtendedTest.expected create mode 100644 languages/swift/custom/test/PathInjectionExtended/PathInjectionExtendedTest.ql create mode 100644 languages/swift/custom/test/PathInjectionExtended/deeplink.swift create mode 100644 languages/swift/custom/test/qlpack.yml diff --git a/.github/workflows/codeql-qlt-unit-tests.yml b/.github/workflows/codeql-qlt-unit-tests.yml index 2a8b6ef7..2f7e1629 100644 --- a/.github/workflows/codeql-qlt-unit-tests.yml +++ b/.github/workflows/codeql-qlt-unit-tests.yml @@ -54,7 +54,8 @@ jobs: # The `ql` language (i.e. ql for ql) is always a special case. # The `actions` language is currently not supported by `qlt` but is supported by `codeql`. # TODO: Implement support for `actions` language in `qlt` - if [[ "$lang" == "ql" || "$lang" == "actions" ]]; then + # The `swift` extractor only runs on macOS, so Swift tests cannot be extracted on this Linux runner. + if [[ "$lang" == "ql" || "$lang" == "actions" || "$lang" == "swift" ]]; then echo "⚠️ qlt unit testing is not currently supported for language: $lang" continue fi diff --git a/languages/swift/custom/src/PathInjectionExtended/PathInjectionExtended.ql b/languages/swift/custom/src/PathInjectionExtended/PathInjectionExtended.ql new file mode 100644 index 00000000..b8e7b35e --- /dev/null +++ b/languages/swift/custom/src/PathInjectionExtended/PathInjectionExtended.ql @@ -0,0 +1,28 @@ +/** + * @name Uncontrolled data used in path expression (extended deep link models) + * @description Accessing paths influenced by users can allow an attacker to access unexpected resources. + * This variant of `swift/path-injection` adds models for parsing custom URL scheme + * (deep link) query parameters and for deep links stored on view controllers. + * @kind path-problem + * @problem.severity error + * @security-severity 7.5 + * @precision high + * @id swift/path-injection-extended + * @tags security + * external/cwe/cwe-022 + * external/cwe/cwe-023 + * external/cwe/cwe-036 + * external/cwe/cwe-073 + * external/cwe/cwe-099 + */ + +import swift +import codeql.swift.dataflow.DataFlow +import codeql.swift.security.PathInjectionQuery +import PathInjectionModels +import PathInjectionFlow::PathGraph + +from PathInjectionFlow::PathNode source, PathInjectionFlow::PathNode sink +where PathInjectionFlow::flowPath(source, sink) +select sink.getNode(), source, sink, "This path depends on a $@.", source.getNode(), + "user-provided value" diff --git a/languages/swift/custom/src/PathInjectionExtended/PathInjectionModels.qll b/languages/swift/custom/src/PathInjectionExtended/PathInjectionModels.qll new file mode 100644 index 00000000..e569738c --- /dev/null +++ b/languages/swift/custom/src/PathInjectionExtended/PathInjectionModels.qll @@ -0,0 +1,74 @@ +/** + * Provides additional models used by `swift/path-injection-extended` to track + * custom URL scheme (deep link) data into file system paths. + * + * Swift does not support data extension (`.model.yml`) files, so these models + * are written in QL using the `SummaryModelCsv` and `TaintInheritingContent` + * extension points of `codeql/swift-all`. They only take effect in queries that + * import this module. + */ + +import swift +private import codeql.swift.dataflow.DataFlow +private import codeql.swift.dataflow.ExternalFlow +private import codeql.swift.dataflow.FlowSteps +private import codeql.swift.security.PathInjectionExtensions + +/** + * A model for `URLComponents` / `NSURLComponents` and `Sequence` members that + * permit taint flow when parsing query parameters out of a URL. + */ +private class UrlComponentsSummaries extends SummaryModelCsv { + override predicate row(string row) { + row = + [ + ";URLComponents;true;init(url:resolvingAgainstBaseURL:);;;Argument[0];ReturnValue.OptionalSome;taint", + ";URLComponents;true;init(string:);;;Argument[0];ReturnValue.OptionalSome;taint", + ";NSURLComponents;true;init(url:resolvingAgainstBaseURL:);;;Argument[0];ReturnValue.OptionalSome;taint", + ";NSURLComponents;true;init(string:);;;Argument[0];ReturnValue.OptionalSome;taint", + ";Sequence;true;first(where:);;;Argument[-1];ReturnValue;taint" + ] + } +} + +/** + * A content implying that, if a `URLComponents`, `NSURLComponents`, + * `URLQueryItem` or `NSURLQueryItem` is tainted, then all its fields (for + * example `queryItems`, `path`, `name` and `value`) are tainted. + */ +private class UrlComponentsFieldsInheritTaint extends TaintInheritingContent, + DataFlow::Content::FieldContent +{ + UrlComponentsFieldsInheritTaint() { + this.getField().getEnclosingDecl().asNominalTypeDecl().getFullName() = + ["URLComponents", "NSURLComponents", "URLQueryItem", "NSURLQueryItem"] + } +} + +/** Holds if `f` is a stored property of a `UIViewController` subclass. */ +private predicate isViewControllerProperty(FieldDecl f) { + f.getEnclosingDecl().asNominalTypeDecl().getABaseTypeDecl*().getFullName() = "UIViewController" +} + +/** + * A flow step from a value assigned to a stored property of a + * `UIViewController` subclass to reads of that property through `self`. + * + * View controllers commonly store a deep link in a property and handle it later + * in a lifecycle method such as `viewDidAppear(_:)`, which is invoked by UIKit + * rather than by application code, so standard data flow does not connect the + * write to the read. + */ +private class ViewControllerStoredPropertyStep extends PathInjectionAdditionalFlowStep { + override predicate step(DataFlow::Node node1, DataFlow::Node node2) { + exists(FieldDecl f, AssignExpr assign, MemberRefExpr read | + isViewControllerProperty(f) and + assign.getDest().(MemberRefExpr).getMember() = f and + node1.asExpr() = assign.getSource() and + read.getMember() = f and + not read = any(AssignExpr a).getDest() and + read.getBase().(DeclRefExpr).getDecl() = any(Callable c).getSelfParam() and + node2.asExpr() = read + ) + } +} diff --git a/languages/swift/custom/src/qlpack.yml b/languages/swift/custom/src/qlpack.yml new file mode 100644 index 00000000..d780f223 --- /dev/null +++ b/languages/swift/custom/src/qlpack.yml @@ -0,0 +1,5 @@ +name: languages-swift-custom-src +version: 0.0.1 +library: false +dependencies: + codeql/swift-all: "*" diff --git a/languages/swift/custom/test/PathInjectionExtended/PathInjectionExtendedTest.expected b/languages/swift/custom/test/PathInjectionExtended/PathInjectionExtendedTest.expected new file mode 100644 index 00000000..e69de29b diff --git a/languages/swift/custom/test/PathInjectionExtended/PathInjectionExtendedTest.ql b/languages/swift/custom/test/PathInjectionExtended/PathInjectionExtendedTest.ql new file mode 100644 index 00000000..63a936da --- /dev/null +++ b/languages/swift/custom/test/PathInjectionExtended/PathInjectionExtendedTest.ql @@ -0,0 +1,22 @@ +import swift +import codeql.swift.dataflow.DataFlow +import codeql.swift.security.PathInjectionQuery +import PathInjectionExtended.PathInjectionModels +import utils.test.InlineExpectationsTest + +module PathInjectionExtendedTest implements TestSig { + string getARelevantTag() { result = "hasPathInjection" } + + predicate hasActualResult(Location location, string element, string tag, string value) { + exists(DataFlow::Node sink | + PathInjectionFlow::flowTo(sink) and + location = sink.getLocation() and + location.getFile().getBaseName() != "" and + element = sink.toString() and + tag = "hasPathInjection" and + value = "" + ) + } +} + +import MakeTest diff --git a/languages/swift/custom/test/PathInjectionExtended/deeplink.swift b/languages/swift/custom/test/PathInjectionExtended/deeplink.swift new file mode 100644 index 00000000..68a442ad --- /dev/null +++ b/languages/swift/custom/test/PathInjectionExtended/deeplink.swift @@ -0,0 +1,159 @@ +// --- stubs --- + +struct URL { + init?(string: String) {} + + var lastPathComponent: String { get { return "" } } + + func appendingPathComponent(_ pathComponent: String) -> URL { return self } +} + +struct URLQueryItem { + var name: String + var value: String? +} + +struct URLComponents { + init?(url: URL, resolvingAgainstBaseURL resolve: Bool) {} + + var queryItems: [URLQueryItem]? { get { return nil } set {} } +} + +class NSURLComponents { + init?(url: URL, resolvingAgainstBaseURL resolve: Bool) {} + + var queryItems: [URLQueryItem]? { get { return nil } set {} } +} + +class Data { + struct Base64DecodingOptions : OptionSet { let rawValue: Int } + struct WritingOptions : OptionSet { let rawValue: Int } + + init?(base64Encoded base64String: String, options: Data.Base64DecodingOptions = []) {} + + func write(to: URL, options: Data.WritingOptions = []) {} +} + +class FileManager { + enum SearchPathDirectory : UInt { case documentDirectory } + struct SearchPathDomainMask : OptionSet { + let rawValue: UInt + static let userDomainMask = SearchPathDomainMask(rawValue: 1) + } + + static let `default` = FileManager() + + func urls(for directory: FileManager.SearchPathDirectory, in domainMask: FileManager.SearchPathDomainMask) -> [URL] { return [] } +} + +class UIApplication { + struct OpenURLOptionsKey : Hashable { + static func == (lhs: OpenURLOptionsKey, rhs: OpenURLOptionsKey) -> Bool { + return true; + } + + func hash(into hasher: inout Hasher) {} + } + struct LaunchOptionsKey: Hashable { + init(rawValue: String) {} + public static let url: UIApplication.LaunchOptionsKey = UIApplication.LaunchOptionsKey(rawValue: "") + func hash(into hasher: inout Hasher) {} + } +} + +protocol UIApplicationDelegate { + func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool +} + +class UIViewController { + func viewDidAppear(_ animated: Bool) {} +} + +// --- app helpers --- + +extension FileManager { + class func documentsUrlForFile(withName name: String) -> URL { + let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] + return documents.appendingPathComponent(name) + } +} + +// --- tests --- + +class AppDelegate: UIApplicationDelegate { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool { + let mainViewController = MainViewController() + let deeplink = launchOptions?[.url] as? URL + mainViewController.set(deeplink: deeplink) + + let safeViewController = SafeViewController() + safeViewController.set(fileName: "report.bin") + return true + } + + func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool { + if let components = URLComponents(url: url, resolvingAgainstBaseURL: false), + let name = components.queryItems?.first(where: { $0.name == "name" })?.value, + let data = Data(base64Encoded: "") { + data.write(to: FileManager.documentsUrlForFile(withName: name)) // $ hasPathInjection + data.write(to: FileManager.documentsUrlForFile(withName: "export.bin")) // safe: fixed file name + } + return true + } +} + +// The deep link is stored on the view controller and handled later in a UIKit +// lifecycle method (`viewDidAppear(_:)`) that is not called from app code. +class MainViewController: UIViewController { + private var deeplink: URL? + + func set(deeplink: URL?) { + self.deeplink = deeplink + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + if let deeplink = deeplink { + handleDeeplink(deeplink) + } + } + + private func handleDeeplink(_ url: URL) { + let route = url.lastPathComponent + if route == "save", + let dataParam = getQueryParameter(url, "data"), + let data = Data(base64Encoded: dataParam), + let name = getQueryParameter(url, "name") { + data.write(to: FileManager.documentsUrlForFile(withName: name)) // $ hasPathInjection + data.write(to: FileManager.documentsUrlForFile(withName: "export.bin")) // safe: fixed file name + } + } + + func getQueryParameter(_ url: URL, _ name: String) -> String? { + if let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true), let params = components.queryItems { + if let value = params.first(where: { $0.name == name })?.value { + return value + } + } + return nil + } +} + +// The stored property is only ever assigned constant values. +class SafeViewController: UIViewController { + private var fileName: String = "default.bin" + + func set(fileName: String) { + self.fileName = fileName + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + if let data = Data(base64Encoded: "") { + data.write(to: FileManager.documentsUrlForFile(withName: fileName)) // safe: constant file name + } + } +} diff --git a/languages/swift/custom/test/qlpack.yml b/languages/swift/custom/test/qlpack.yml new file mode 100644 index 00000000..34034232 --- /dev/null +++ b/languages/swift/custom/test/qlpack.yml @@ -0,0 +1,5 @@ +name: languages-swift-custom-test +version: 0.0.1 +dependencies: + languages-swift-custom-src: ${workspace} +extractor: swift