mirror of
https://github.com/swiftbar/SwiftBar.git
synced 2024-08-16 19:40:28 +03:00
Improve shell escaping heuristics #402
Signed-off-by: Alex Mazanov <alexandr.mazanov@gmail.com>
This commit is contained in:
parent
28ef89ff82
commit
d81efbc3dc
@ -34,17 +34,23 @@ extension String {
|
|||||||
|
|
||||||
extension String {
|
extension String {
|
||||||
var isEnclosedInQuotes: Bool {
|
var isEnclosedInQuotes: Bool {
|
||||||
let regex = #"^(?:'.*'|".*")$"#
|
hasPrefix("'") && hasSuffix("'")
|
||||||
return range(of: regex, options: .regularExpression) != nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var needsShellQuoting: Bool {
|
var needsShellQuoting: Bool {
|
||||||
let specialCharacters = " \t\n\"'`$\\|&;()<>[]*?{}!^~#%"
|
let specialCharacters = " \t\n\"'`$\\|&;()<>[]*?{}!^~#%"
|
||||||
|
let shellOperators = ["&&", "||", ";", "|", "<", ">"]
|
||||||
|
|
||||||
|
// Check if the string is exactly a logical operator
|
||||||
|
if shellOperators.contains(self) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
return rangeOfCharacter(from: CharacterSet(charactersIn: specialCharacters)) != nil
|
return rangeOfCharacter(from: CharacterSet(charactersIn: specialCharacters)) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func quoteIfNeeded() -> String {
|
func quoteIfNeeded() -> String {
|
||||||
guard needsShellQuoting else { return self }
|
guard needsShellQuoting else { return self }
|
||||||
return isEnclosedInQuotes ? self : "\'\(self)\'"
|
return isEnclosedInQuotes ? self : "\'\(replacingOccurrences(of: "'", with: "'\\''"))\'"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user