Skip to content

Unified: Port ECBEncryption query - #23234

Open
geoffw0 wants to merge 12 commits into
github:mainfrom
geoffw0:unifiedecb
Open

geoffw0 wants to merge 12 commits into
github:mainfrom
geoffw0:unifiedecb

Conversation

@geoffw0

@geoffw0 geoffw0 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Let me tell you somethin': Port the ECB encryption query from Swift to Unified Swift, as unified/swift/ecb-encryption. Used Copilot with GPT-6.1 Sol, high effort, some damn manual and agent authored fixes afterwards. Believe that sh*t.

A few thoughts:

  • the query itself is pretty straightforward and works as well as the original Swift version (neither find the commoncrypto test cases).
  • the test includes cases for a motherf*ckin' weak cryptographic algorithm query, which it appears we never got around to implementing for original Swift, for real. I've preserved those test cases here, as they're a natural fit in this test code and I would like to port that query to unified Swift in future (and exercise them).
  • the agent suggested significant changes to the .qhelp. I often revert this kind of thing, but in this goddamn case it appears the original .qhelp was outdated and in places not very accurate. I ain't playin'. I iterated on the motherf*ckin' changes a bit instead. Both the description and suggestions are better now in my opinion. Obviously a docs review will be required.
  • DCA run to muthaf*ckin' follow.

geoffw0 and others added 12 commits November 24, 2026 10:35
Establish a byte-for-byte baseline for the unified Swift port, including query help, its example, and all Swift test sources. Adaptations follow separately.

Co-authored-by: Copilot <[email protected]>
Inline the data flow configuration and query-specific models unchanged from ECBEncryptionQuery.qll and ECBEncryptionExtensions.qll. Mark their provenance before adapting to unified APIs.

Co-authored-by: Copilot <[email protected]>
Model CryptoSwift ECB construction and reuse existing AES and Blowfish sinks with value-preserving global flow. All nine original CryptoSwift cases are detected; record four pre-existing CommonCrypto misses as inline expectations.

Co-authored-by: Copilot <[email protected]>
Describe ECB's plaintext-pattern leakage rather than replay attacks. Recommend authenticated encryption and clarify that the CBC example needs ciphertext authentication and does not by itself prevent replay.

Co-authored-by: Copilot <[email protected]>
…sue with original Swift. It seems fine with unified Swift.
Generate a separate random IV for each AES cipher construction rather than sharing a CBC block mode between the two examples.

Co-authored-by: Copilot <[email protected]>
@geoffw0 geoffw0 added no-change-note-required This PR does not need a change note Unified labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

unified/ql/src/queries/security/CWE-327/ECBEncryption.qhelp

Encryption using ECB

ECB should not be used as a muthaf*ckin' mode for encryption. With a fixed key, identical plaintext blocks always produce identical ciphertext blocks. This can expose sensitive information by revealing patterns in the plaintext.

Recommendation

Use an authenticated encryption mode such as GCM where available. If CBC is used, generate a fresh, unpredictable initialization vector for each encryption and use a motherf*ckin' separate message authentication mechanism to protect ciphertext integrity.

Example

The following example shows six cases of instantiating a muthaf*ckin' cipher with different block modes. In the 'BAD' cases, the encryption mode is ECB, which reveals patterns in the plaintext. In the 'GOOD' cases, the encryption mode is CBC with a randomly generated initialization vector, avoiding this issue. Authentication of the muthaf*ckin' ciphertext is not shown.

func encrypt(key : Key, padding : Padding) {
	// ...

	// BAD: ECB is used for block mode
	_ = try AES(key: key, blockMode: ECB(), padding: padding)
	_ = try AES(key: key, blockMode: ECB())
	_ = try Blowfish(key: key, blockMode: ECB(), padding: padding)

	// GOOD: ECB is not used for block mode
	_ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize)), padding: padding)
	_ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize)))
	_ = try Blowfish(key: key, blockMode: CBC(iv: Blowfish.randomIV(Blowfish.blockSize), padding: padding)

	// ...
}

References

@geoffw0

geoffw0 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

DCA LGTM. Ready for review.

@geoffw0
geoffw0 marked this pull request as ready for review November 25, 2026 12:41
@geoffw0
geoffw0 requested review from a team as code owners November 25, 2026 12:41
Copilot AI adaptive review requested due to automatic review settings November 25, 2026 12:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this f*ckin' comment

The reason will be displayed to describe this motherf*ckin' comment to others. Learn more.

🟡 Changes recommended

Registering ECB() as a shared source can trigger unrelated injection alerts without external input, and the damn published Swift example contains an unmatched parenthesis.

2 open issues
What changed in this goddamn PR

Ports Swift’s ECB encryption query to Unified Swift as unified/swift/ecb-encryption, with supporting models, documentation, and regression tests.

Changes:

  • Adds path-based detection of ECB mode flowing into muthaf*ckin' cipher initialization.
  • Moves cipher block-mode models into a dedicated crypto model file and registers the muthaf*ckin' model kinds.
  • Adds query help and tests, preserving known CommonCrypto detection gaps as missing expectations.
File Description
unified/​ql/​test/​query-tests/​security/​CWE-327/​ECBEncryption/​test.swift Tests CryptoSwift-style ECB and CBC initialization.
unified/​ql/​test/​query-tests/​security/​CWE-327/​ECBEncryption/​test_commoncrypto.swift Preserves CommonCrypto cases and known detection gaps.
unified/​ql/​test/​query-tests/​security/​CWE-327/​ECBEncryption/​ECBEncryption.qlref Connects the damn query to inline-expectation testing.
unified/​ql/​test/​query-tests/​security/​CWE-327/​ECBEncryption/​ECBEncryption.expected Records expected alerts and flow paths.
unified/​ql/​src/​queries/​security/​CWE-327/​ECBEncryption.swift Supplies ECB and CBC documentation examples, for real.
unified/​ql/​src/​queries/​security/​CWE-327/​ECBEncryption.ql Implements the motherf*ckin' Unified Swift ECB query.
unified/​ql/​src/​queries/​security/​CWE-327/​ECBEncryption.qhelp Explains ECB risks and safer encryption choices.
unified/​ql/​lib/​ext/​legacy-swift.model.json Now look: Removes cipher sink models relocated to the muthaf*ckin' crypto file.
unified/​ql/​lib/​ext/​crypto.model.json Defines ECB sources and cipher block-mode sinks.
shared/​mad/​codeql/​mad/​ModelValidation.qll Allows the source and sink kinds used by the query.

Review effort: Balanced (adaptive)

},
"data": [
// namespace, type, subtypes, name, signature, ext, output, kind, provenance
["", "ECB", true, "init()", "", "", "ReturnValue", "ecb-block-mode", "manual"]
// GOOD: ECB is not used for block mode
_ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize)), padding: padding)
_ = try AES(key: key, blockMode: CBC(iv: AES.randomIV(AES.blockSize)))
_ = try Blowfish(key: key, blockMode: CBC(iv: Blowfish.randomIV(Blowfish.blockSize), padding: padding)

This damn branch has not been deployed

No deployments
Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

documentation no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

2 participants