Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Swift rule for nil assignment to implicilty unwrapped optional #66

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion rules_table_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import yaml
import sys

LANGUAGES = ['go', 'python', 'rs', 'javascript']
LANGUAGES = ['go', 'python', 'rs', 'javascript', 'swift']
IMPACT_MAP = {
'LOW': "🟩",
'MEDIUM': "🟧",
Expand Down
41 changes: 41 additions & 0 deletions swift/AssignNilToImplicitlyUnwrappedOptional.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

func LuhnCheck(_ pan: Int, check: Int?=nil) -> (Bool, Int?) {
var num: Int!
num = pan
var cd: Int! = nil
var sum = 0, digit = 0
var shouldDouble: Bool? = false
if let ch = check {
cd = ch
} else {
cd = num % 10
num /= 10
}
while num > 0 {
digit = num % 10
num /= 10
if let d = shouldDouble {
if d {
digit *= 2
}
shouldDouble!.toggle()
}
sum += digit
}
let correct_check = 10 - (sum % 10)
// ok: assign-nil-to-implicitly-unwrapped-optional
shouldDouble = nil
if cd == correct_check {
// ruleid: assign-nil-to-implicitly-unwrapped-optional
num = nil
return (true, cd)
} else {
// ruleid: assign-nil-to-implicitly-unwrapped-optional
cd = nil
return (false, nil)
}
}

print("Hello, world!")
print(LuhnCheck(4429000011112224))
print(LuhnCheck(4429000011112223))
33 changes: 33 additions & 0 deletions swift/AssignNilToImplicitlyUnwrappedOptional.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
rules:
- id: assign-nil-to-implicitly-unwrapped-optional
message: >
An implicitly unwrapped optional was assigned the value nil. If the
variable is dereferenced before it is assigned a non-nil value, a fatal
error will occur.
languages: [swift]
severity: WARNING
metadata:
category: security
subcategory:
- audit
technology:
- shell
cwe: "CWE-250: Execution with Unnecessary Privileges"
confidence: HIGH
likelihood: HIGH
impact: HIGH
references:
- https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/#Implicitly-Unwrapped-Optionals
description: Implicitly unwrapped optionals are always unwrapped whenever they are dereferenced. Such a variable should never be assigned the value nil after initialization.
patterns:
- pattern-inside:
pattern-either:
- pattern: |
var $VAR: Int! = nil
(...)
- pattern: |
var $VAR: Int!
(...)
Comment on lines +23 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I'm kinda surprised this runs. AFAIK, pattern-inside must take a string, not a child YAML object. IMO a better way to structure this would be to have the pattern-either as the parent pattern, and have two pattern-insides underneath.

I think taint mode is another great option here. It perfectly matches what you're trying to achieve here. The patterns above would be the sources, the pattern-nots would be sanitizers, and the pattern below would be the sink.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. Given CodeQL's inability to richly express variable types, I'm going to chuck this for a CodeQL version at some point in the future. Thanks!

- pattern: $VAR = nil
- pattern-not: "var $VAR: Int! = nil"
- pattern-not-inside: "var $VAR: Int! = nil"
Loading