mirror of
https://github.com/github/codeql.git
synced 2025-12-20 02:44:30 +01:00
Swift: Separate out a FormatString library as well.
This commit is contained in:
74
swift/ql/lib/codeql/swift/FormatString.qll
Normal file
74
swift/ql/lib/codeql/swift/FormatString.qll
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Provides classes and predicates for reasoning about string formatting.
|
||||
*/
|
||||
|
||||
import swift
|
||||
|
||||
/**
|
||||
* A function that takes a `printf` style format argument.
|
||||
*/
|
||||
abstract class FormattingFunction extends AbstractFunctionDecl {
|
||||
/**
|
||||
* Gets the position of the format argument.
|
||||
*/
|
||||
abstract int getFormatParameterIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
* A call to a function that takes a `printf` style format argument.
|
||||
*/
|
||||
class FormattingFunctionCall extends CallExpr {
|
||||
FormattingFunction target;
|
||||
|
||||
FormattingFunctionCall() { target = this.getStaticTarget() }
|
||||
|
||||
/**
|
||||
* Gets the format expression used in this call.
|
||||
*/
|
||||
Expr getFormat() { result = this.getArgument(target.getFormatParameterIndex()).getExpr() }
|
||||
}
|
||||
|
||||
/**
|
||||
* An initializer for `String`, `NSString` or `NSMutableString` that takes a
|
||||
* `printf` style format argument.
|
||||
*/
|
||||
class StringInitWithFormat extends FormattingFunction, MethodDecl {
|
||||
StringInitWithFormat() {
|
||||
exists(string fName |
|
||||
this.hasQualifiedName(["String", "NSString", "NSMutableString"], fName) and
|
||||
fName.matches("init(format:%")
|
||||
)
|
||||
}
|
||||
|
||||
override int getFormatParameterIndex() { result = 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* The `localizedStringWithFormat` method of `String`, `NSString` and `NSMutableString`.
|
||||
*/
|
||||
class LocalizedStringWithFormat extends FormattingFunction, MethodDecl {
|
||||
LocalizedStringWithFormat() {
|
||||
this.hasQualifiedName(["String", "NSString", "NSMutableString"],
|
||||
"localizedStringWithFormat(_:_:)")
|
||||
}
|
||||
|
||||
override int getFormatParameterIndex() { result = 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* The functions `NSLog` and `NSLogv`.
|
||||
*/
|
||||
class NsLog extends FormattingFunction, FreeFunctionDecl {
|
||||
NsLog() { this.getName() = ["NSLog(_:_:)", "NSLogv(_:_:)"] }
|
||||
|
||||
override int getFormatParameterIndex() { result = 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* The `NSException.raise` method.
|
||||
*/
|
||||
class NsExceptionRaise extends FormattingFunction, MethodDecl {
|
||||
NsExceptionRaise() { this.hasQualifiedName("NSException", "raise(_:format:arguments:)") }
|
||||
|
||||
override int getFormatParameterIndex() { result = 1 }
|
||||
}
|
||||
@@ -4,79 +4,11 @@
|
||||
*/
|
||||
|
||||
import swift
|
||||
import codeql.swift.StringFormat
|
||||
import codeql.swift.dataflow.DataFlow
|
||||
import codeql.swift.dataflow.TaintTracking
|
||||
import codeql.swift.dataflow.FlowSources
|
||||
|
||||
/**
|
||||
* A function that takes a `printf` style format argument.
|
||||
*/
|
||||
abstract class FormattingFunction extends AbstractFunctionDecl {
|
||||
/**
|
||||
* Gets the position of the format argument.
|
||||
*/
|
||||
abstract int getFormatParameterIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
* An initializer for `String`, `NSString` or `NSMutableString` that takes a
|
||||
* `printf` style format argument.
|
||||
*/
|
||||
class StringInitWithFormat extends FormattingFunction, MethodDecl {
|
||||
StringInitWithFormat() {
|
||||
exists(string fName |
|
||||
this.hasQualifiedName(["String", "NSString", "NSMutableString"], fName) and
|
||||
fName.matches("init(format:%")
|
||||
)
|
||||
}
|
||||
|
||||
override int getFormatParameterIndex() { result = 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* The `localizedStringWithFormat` method of `String`, `NSString` and `NSMutableString`.
|
||||
*/
|
||||
class LocalizedStringWithFormat extends FormattingFunction, MethodDecl {
|
||||
LocalizedStringWithFormat() {
|
||||
this.hasQualifiedName(["String", "NSString", "NSMutableString"],
|
||||
"localizedStringWithFormat(_:_:)")
|
||||
}
|
||||
|
||||
override int getFormatParameterIndex() { result = 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* The functions `NSLog` and `NSLogv`.
|
||||
*/
|
||||
class NsLog extends FormattingFunction, FreeFunctionDecl {
|
||||
NsLog() { this.getName() = ["NSLog(_:_:)", "NSLogv(_:_:)"] }
|
||||
|
||||
override int getFormatParameterIndex() { result = 0 }
|
||||
}
|
||||
|
||||
/**
|
||||
* The `NSException.raise` method.
|
||||
*/
|
||||
class NsExceptionRaise extends FormattingFunction, MethodDecl {
|
||||
NsExceptionRaise() { this.hasQualifiedName("NSException", "raise(_:format:arguments:)") }
|
||||
|
||||
override int getFormatParameterIndex() { result = 1 }
|
||||
}
|
||||
|
||||
/**
|
||||
* A call to a function that takes a `printf` style format argument.
|
||||
*/
|
||||
class FormattingFunctionCall extends CallExpr {
|
||||
FormattingFunction target;
|
||||
|
||||
FormattingFunctionCall() { target = this.getStaticTarget() }
|
||||
|
||||
/**
|
||||
* Gets the format expression used in this call.
|
||||
*/
|
||||
Expr getFormat() { result = this.getArgument(target.getFormatParameterIndex()).getExpr() }
|
||||
}
|
||||
|
||||
/**
|
||||
* A taint configuration for tainted data that reaches a format string.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user