-
Notifications
You must be signed in to change notification settings - Fork 33
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
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)) |
This file contains 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
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! | ||
(...) | ||
- pattern: $VAR = nil | ||
- pattern-not: "var $VAR: Int! = nil" | ||
- pattern-not-inside: "var $VAR: Int! = nil" |
Oops, something went wrong.
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.
There was a problem hiding this comment.
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 thepattern-either
as the parent pattern, and have twopattern-inside
s underneath.I think taint mode is another great option here. It perfectly matches what you're trying to achieve here. The
pattern
s above would be the sources, thepattern-not
s would be sanitizers, and thepattern
below would be the sink.There was a problem hiding this comment.
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!