|
| 1 | +# Customizing Library Models for Swift |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +Customize data-flow and taint analysis for Swift by modeling frameworks and libraries via data extensions (YAML) and model packs. This enables accurate flow tracking through third-party libraries not included in CodeQL databases. |
| 6 | + |
| 7 | +For common guidance on data extensions (YAML structure, model packs, development workflow), see `codeql://learning/data-extensions`. |
| 8 | + |
| 9 | +## Data Extensions Overview |
| 10 | + |
| 11 | +### Structure |
| 12 | + |
| 13 | +Data extensions use YAML format to extend CodeQL's knowledge of library behavior: |
| 14 | + |
| 15 | +```yaml |
| 16 | +extensions: |
| 17 | + - addsTo: |
| 18 | + pack: codeql/swift-all |
| 19 | + extensible: <extensible-predicate> |
| 20 | + data: |
| 21 | + - <tuple1> |
| 22 | + - <tuple2> |
| 23 | +``` |
| 24 | +
|
| 25 | +### Union Semantics |
| 26 | +
|
| 27 | +- Multiple YAML files are combined |
| 28 | +- Rows are merged across files |
| 29 | +- Duplicates are automatically removed |
| 30 | +- Order of files doesn't matter |
| 31 | +
|
| 32 | +## Model Format |
| 33 | +
|
| 34 | +Swift uses a **MaD (Models as Data)** format with multi-column tuples that identify callables by module/type/name/signature — the same structural family as Java/Kotlin, C#, C/C++, and Go. Methods are keyed on Swift's module-qualified type and method names (e.g. `Foundation.URLRequest.init(url:)`). |
| 35 | + |
| 36 | +## Extensible Predicates for Swift |
| 37 | + |
| 38 | +| Predicate | Purpose | |
| 39 | +| -------------- | --------------------------------------------------------------------- | |
| 40 | +| `sourceModel` | Model sources of tainted data | |
| 41 | +| `sinkModel` | Model sinks where tainted data is used unsafely | |
| 42 | +| `summaryModel` | Model flow through opaque library functions/methods | |
| 43 | +| `barrierModel` | Model barriers (sanitizers) that stop taint flow | |
| 44 | +| `neutralModel` | Mark callables as having no dataflow impact (suppress generated rows) | |
| 45 | + |
| 46 | +Refer to `codeql/swift-all` (the `ext/*.model.yml` files under `swift/ql/lib/ext/` in the upstream `codeql` repository) for the canonical column layout used by the current CodeQL CLI release. Authoring a tuple with the wrong column count will fail to load (often silently). |
| 47 | + |
| 48 | +## Identifier Columns |
| 49 | + |
| 50 | +Swift models typically identify a callable by: |
| 51 | + |
| 52 | +- **module** — Swift module name (e.g. `Foundation`, `UIKit`, the package/target name for third-party code) |
| 53 | +- **type** — Type name (`""` for module-level free functions) |
| 54 | +- **subtypes** — Whether to apply to subtypes (`true`/`false`) |
| 55 | +- **name** — Method or function name (e.g. `init(url:)`, `data(using:)`) |
| 56 | +- **signature** — Parameter signature (`""` for any) |
| 57 | + |
| 58 | +The exact column count and order is defined by the `codeql/swift-all` pack — always cross-check before authoring rows. |
| 59 | + |
| 60 | +## Access Paths |
| 61 | + |
| 62 | +Swift access paths follow the same conventions as the other MaD-tuple languages: |
| 63 | + |
| 64 | +| Component | Description | |
| 65 | +| ---------------- | ----------------------------------------------- | |
| 66 | +| `Argument[n]` | Argument at index n (0-based, excluding `self`) | |
| 67 | +| `Argument[self]` | The receiver of a method call | |
| 68 | +| `Parameter[n]` | Parameter at index n (used by `summaryModel`) | |
| 69 | +| `ReturnValue` | Return value of a call | |
| 70 | + |
| 71 | +## Common Sink Kinds |
| 72 | + |
| 73 | +`command-injection`, `path-injection`, `sql-injection`, `request-forgery`, `url-redirection`, `code-injection`, `predicate-injection` |
| 74 | + |
| 75 | +## Sample Model |
| 76 | + |
| 77 | +```yaml |
| 78 | +extensions: |
| 79 | + - addsTo: |
| 80 | + pack: codeql/swift-all |
| 81 | + extensible: sinkModel |
| 82 | + data: |
| 83 | + - [ |
| 84 | + 'Foundation', |
| 85 | + 'NSPredicate', |
| 86 | + false, |
| 87 | + 'init(format:argumentArray:)', |
| 88 | + '', |
| 89 | + '', |
| 90 | + 'Argument[0]', |
| 91 | + 'predicate-injection', |
| 92 | + 'manual' |
| 93 | + ] |
| 94 | +``` |
| 95 | + |
| 96 | +> The exact column count above is **illustrative**; verify against the `codeql/swift-all` pack shipped with the CodeQL CLI version recorded in `.codeql-version`. |
| 97 | + |
| 98 | +## Validation Workflow |
| 99 | + |
| 100 | +1. Place `*.model.yml` files in your model-pack directory (or under `.github/codeql/extensions/` for the single-repo path). |
| 101 | +2. Run `codeql_query_run` against a database that exercises the modelled APIs and confirm new findings appear (sources/sinks) or expected findings disappear (barriers/neutrals). |
| 102 | +3. Add a unit test that exercises the new chain end-to-end using `codeql_test_run`. |
| 103 | + |
| 104 | +## Related Resources |
| 105 | + |
| 106 | +- `codeql://learning/data-extensions` — Common data extensions overview (both model formats) |
| 107 | +- [CodeQL for Swift](https://codeql.github.com/docs/codeql-language-guides/codeql-for-swift/) — Official Swift language guide |
0 commit comments