Repository navigation
Draft
[NEW QUERY] Swift CWE-20: unvalidated custom URL scheme parameters and unchecked substring ranges#350
Conversation
1 task done
…ked substring range Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
…quire Bool-valued checks Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add new query for CWE-20 in Swift
[NEW QUERY] Swift CWE-20: unvalidated custom URL scheme parameters and unchecked substring ranges
Oct 31, 2026
There was a problem hiding this comment.
🟡 Changes recommended
Validation barriers can suppress alerts for reassigned URL values and unchecked substring ranges, while replacement text is incorrectly treated as a range bound, causing both missed findings and false positives.
3 open issues
What changed in this PR
Adds a custom Swift CodeQL pack addressing the detection gaps described in #300: unvalidated deep-link parameters and unchecked NSString search ranges.
Changes:
- Adds two CWE-20 queries covering deep-link data reaching UI/actions and search results used as substring bounds.
- Includes Swift fixtures, inline expectations, and expected query results.
- Adds query help and source/test pack definitions.
| File | Description |
|---|---|
languages/swift/custom/test/UnvalidatedUrlSchemeParameter/UnvalidatedUrlSchemeParameter.qlref |
Connects deep-link tests to the query and expectation processor. |
languages/swift/custom/test/UnvalidatedUrlSchemeParameter/UnvalidatedUrlSchemeParameter.expected |
Records expected alerts and flow paths. |
languages/swift/custom/test/UnvalidatedUrlSchemeParameter/test.swift |
Covers delegate sources, sensitive sinks, and validation branches. |
languages/swift/custom/test/UncheckedSubstringRange/UncheckedSubstringRange.qlref |
Connects range tests to the query and expectation processor. |
languages/swift/custom/test/UncheckedSubstringRange/UncheckedSubstringRange.expected |
Records expected unchecked-range alerts. |
languages/swift/custom/test/UncheckedSubstringRange/test.swift |
Covers unchecked and checked delimiter searches. |
languages/swift/custom/test/qlpack.yml |
Defines the Swift test pack. |
languages/swift/custom/src/UnvalidatedUrlSchemeParameter/UnvalidatedUrlSchemeParameter.ql |
Implements deep-link taint tracking and validation barriers. |
languages/swift/custom/src/UnvalidatedUrlSchemeParameter/UnvalidatedUrlSchemeParameter.md |
Explains deep-link risks and parameter validation. |
languages/swift/custom/src/UncheckedSubstringRange/UncheckedSubstringRange.ql |
Detects unchecked search results used as substring bounds. |
languages/swift/custom/src/UncheckedSubstringRange/UncheckedSubstringRange.md |
Documents range checks and safer parsing. |
languages/swift/custom/src/qlpack.yml |
Defines the query pack and Swift library dependency. |
languages/swift/custom/.gitkeep |
Directory placeholder associated with the new pack. |
Review effort: Balanced
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

AI Scan found two CWE-20 issues in iGoat-Swift that CodeQL default setup misses. The first is deep-link query parameters driving an alert. The second is
NSString.range(of:)results used to slice a server response without checking that the delimiter was found.swift-allalready treats theapplication(_:open:options:)URL as untrusted input, but no Swift query flags it reaching UI or actions, and nothing covers unchecked range lookups. This PR adds two queries under a newlanguages/swift/custompack.📝 Query Information
swift/unvalidated-url-scheme-parameter,swift/unchecked-substring-range🎯 Description
What This Query Detects
swift/unvalidated-url-scheme-parameter(path-problem)UIApplicationDelegate:application(_:open:options:),application(_:handleOpen:),application(_:open:sourceApplication:annotation:)UISceneDelegate:scene(_:openURLContexts:), plus theoptionsparameter ofscene(_:willConnectTo:options:)UIAlertController(title:message:preferredStyle:)title and messageMFMessageComposeViewControllerrecipients,bodyandsubjectMFMailComposeViewController:setToRecipients,setCcRecipients,setBccRecipients,setSubject,setMessageBodyUIApplication.open/openURLURLComponentsinit, members ofURLComponents/URLQueryItem, andfor x in <tainted collection>.vis treated as safe only on the branch wherevpassed a check in anif/guard/whilecondition. Accepted checks are aBool-returning call that takesvas an argument or receiver, orv ==/!=/~=something. Checking onlyurl.schemedoes not count, so the iGoat case is still reported.swift/unchecked-substring-range(problem)NSString.range(of:…)when its result (or its.location/.length) flows into:substring(with:),substring(from:),substring(to:), orreplacingCharacters(in:with:)NSRange(location:length:)orNSMakeRange!= NSNotFoundorlength == 0.Example Vulnerable Code
Example Safe Code
🧪 Testing
guard let URLComponents(url:); a!=check only applies on its false branch; only some parameters are validated; scene delegates; nested optional rangesTests use inline expectations (
// $ Alert/// $ Source). They declare minimal stand-ins for the UIKit/Foundation types, so they extract without the Apple SDKs. The bundled 2.27.2 Swift extractor only works on macOS, so I extracted the test databases with CodeQL 2.17.6's Linux extractor and upgraded them with 2.27.2. On macOS, the path-step lines (edges/nodes) inUnvalidatedUrlSchemeParameter.expectedmay need re-accepting; the alerts themselves should not change.📋 Checklist
.mdand.qhelp):.mdquery help only, no.qhelp🔗 References
codeql/swift-allcodeql/swift/frameworks/StandardLibrary/CustomUrlSchemes.qllNote: This query was developed using Test-Driven Development methodology.