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

Exclude files or code from obfuscation #31

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
Exclude publics from obfuscation
  • Loading branch information
hadiidbouk committed Nov 12, 2018
commit ec9c5c7f5f451174407be922ade3c3dc7a744c6e
69 changes: 39 additions & 30 deletions swiftshield-Sources/AutomaticSwiftShield.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ class AutomaticSwiftShield: Protector {
let projectToBuild: String
let schemeToBuild: String
let modulesToIgnore: Set<String>
let filesToIgnore: Set<String>
let excludedPrefixTag: String
let excludedSuffixTag: String
var publicProtocols: Set<String>!
Copy link
Owner

Choose a reason for hiding this comment

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

Do not use force unwrap - If storing the protocols here are needed, we can pre-init it for safety reasons 🙂
var publicProtocols: Set<String> = []

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

let excludePublic: Bool

var isWorkspace: Bool {
return projectToBuild.hasSuffix(".xcworkspace")
Expand All @@ -17,16 +16,12 @@ class AutomaticSwiftShield: Protector {
projectToBuild: String,
schemeToBuild: String,
modulesToIgnore: Set<String>,
classesToIgnore: Set<String>,
protectedClassNameSize: Int,
excludedPrefixTag: String,
excludedSuffixTag: String) {
excludePublic: Bool) {
self.projectToBuild = projectToBuild
self.schemeToBuild = schemeToBuild
self.modulesToIgnore = modulesToIgnore
self.filesToIgnore = classesToIgnore
self.excludedPrefixTag = excludedPrefixTag
self.excludedSuffixTag = excludedSuffixTag
self.excludePublic = excludePublic
super.init(basePath: basePath, protectedClassNameSize: protectedClassNameSize)
if self.schemeToBuild.isEmpty || self.projectToBuild.isEmpty {
Logger.log(.helpText)
Expand Down Expand Up @@ -68,6 +63,9 @@ class AutomaticSwiftShield: Protector {
Logger.log(.indexing(file: file))
let resp = index(sourceKit: sourceKit, file: file, args: compilerArgs)
let dict = SKApi.sourcekitd_response_get_value(resp)

publicProtocols = Set<String>()

sourceKit.recurseOver(childID: sourceKit.entitiesID, resp: dict) { [unowned self] dict in
guard let data = self.getNameData(from: dict,
obfuscationData: obfuscationData,
Expand Down Expand Up @@ -128,6 +126,7 @@ extension AutomaticSwiftShield {
obfuscationData: ObfuscationData,
sourceKit: SourceKit,
shouldRemoveSuffixTags: Bool) -> (name: String, usr: String, obfuscatedName: String)? {

let kind = dict.getUUIDString(key: sourceKit.kindID)
guard sourceKit.declarationType(for: kind) != nil else {
return nil
Expand All @@ -136,33 +135,43 @@ extension AutomaticSwiftShield {
return nil
}

if self.filesToIgnore.contains(name) {
return nil
}

if self.excludedPrefixTag != "" && name.hasPrefix(self.excludedPrefixTag) && shouldRemoveSuffixTags == false {
return nil
}

if self.excludedSuffixTag != "" && name.hasSuffix(excludedSuffixTag) && shouldRemoveSuffixTags == false {
return nil
if excludePublic {
Copy link
Owner

Choose a reason for hiding this comment

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

To avoid making getNameData get super big, we can move this logic to a separate shouldIgnoreSymbol()-like method, makes sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

let attributesDict = SKApi.sourcekitd_variant_dictionary_get_value(dict, sourceKit.attributesID)
let attributesData = dict.getAttributes(dict: attributesDict, subKey: sourceKit.attributeID)


//Check if variant is public
Copy link
Owner

Choose a reason for hiding this comment

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

Let's avoid comments unless explaining an intent is needed - what is happening should be clear enough through the name of the properties we're dealing with.

let isPublic = attributesData.filter { item in
return item.contains("public")
Copy link
Owner

Choose a reason for hiding this comment

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

I would avoid dealing with strings directly. We can make an enum to handle visibility identifiers. I already did that for kinds, so we can follow the same pattern 🙂

}.count != 0

//Add to publicProtocols array
if kind == "source.lang.swift.decl.protocol" && isPublic {
Copy link
Owner

Choose a reason for hiding this comment

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

Same thing here (not dealing with strings directly), SKAPI already has a method to return what an identifier is - I think we just need to add the protocol case.

publicProtocols.insert(name)
}

//Don't Obfuscate public methods/properties..
if isPublic {
return nil
}

//Handle public protocol's functions
for protocolName in publicProtocols {
if usr.contains(protocolName) {
Copy link
Owner

Choose a reason for hiding this comment

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

If I understood this correctly, shouldn't this be simply publicProtocols.contains(usr) instead of a loop? This is running in O(nˆ2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because the receiver_usr isn't returning just a name, It's something like that
s:17ObfuscationSource14ViewControllerC5countSivp

Copy link
Owner

Choose a reason for hiding this comment

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

I see now. The problem is that using contains inside an usr will likely not work in a bigger app because nothing stops other modules from having private protocols with the same name as your public one. I think you need to store the protocol usrs in publicProtocols instead of their names to avoid this loop.

But I'm getting some bad vibes, is this deterministic? I'm having the impression that a public protocol's method would be wrongly obfuscated if the order of the files changed. If you look at isReferencingInternal(), I had to even make it recursive to make it work. Things are slightly more difficult here because it happens before everything, but I think it's gonna work nicely in the end.

return nil
}
}
}

guard let protected = obfuscationData.obfuscationDict[name] else {
if !shouldRemoveSuffixTags {
let newName = String.random(length: self.protectedClassNameSize, excluding: obfuscationData.allObfuscatedNames)
obfuscationData.obfuscationDict[name] = newName
return (name, usr, newName)
} else {
let newName = name.components(separatedBy: self.excludedSuffixTag).first!
obfuscationData.obfuscationDict[name] = newName
return (name, usr, newName)
}
//if !shouldRemoveSuffixTags {
Copy link
Owner

Choose a reason for hiding this comment

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

We can remove these leftovers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will remove it.

let newName = String.random(length: self.protectedClassNameSize, excluding: obfuscationData.allObfuscatedNames)
obfuscationData.obfuscationDict[name] = newName
return (name, usr, newName)
}

return (name, usr, protected)
}

func findReferencesInIndexed(obfuscationData: AutomaticObfuscationData) {
let SK = SourceKit()
Logger.log(.searchingReferencesOfUsr)
Expand Down
14 changes: 14 additions & 0 deletions swiftshield-Sources/SourceKit+Variant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,18 @@ extension sourcekitd_variant_t {
func getDictionary(key: sourcekitd_uid_t) -> sourcekitd_variant_t {
return SKApi.sourcekitd_variant_dictionary_get_value(self, key)
}

func getAttributes(dict: sourcekitd_variant_t, subKey: sourcekitd_uid_t) -> [String] {

var data = [String]()

let _ = SKApi.sourcekitd_variant_array_apply(dict) { (_, attributesDict) in
Copy link

Choose a reason for hiding this comment

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

Is this safe? Wouldn't it crash on none-array dict?


let accessControl = attributesDict.getUUIDString(key: subKey)
data.append(accessControl)
return true
}

return data
}
}
4 changes: 3 additions & 1 deletion swiftshield-Sources/SourceKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ final class SourceKit {
lazy var lineID = SKApi.sourcekitd_uid_get_from_cstr("key.line")!
lazy var colID = SKApi.sourcekitd_uid_get_from_cstr("key.column")!
lazy var usrID = SKApi.sourcekitd_uid_get_from_cstr("key.usr")!

lazy var attributesID = SKApi.sourcekitd_uid_get_from_cstr("key.attributes")!
lazy var attributeID = SKApi.sourcekitd_uid_get_from_cstr("key.attribute")!

func array(argv: [String]) -> sourcekitd_object_t {
let objects = argv.map { SKApi.sourcekitd_request_string_create($0) }
return SKApi.sourcekitd_request_array_create(objects, objects.count)!
Expand Down
6 changes: 2 additions & 4 deletions swiftshield-Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ if automatic {
let schemeToBuild = UserDefaults.standard.string(forKey: "automatic-project-scheme") ?? ""
let projectToBuild = UserDefaults.standard.string(forKey: "automatic-project-file") ?? ""
let modulesToIgnore = UserDefaults.standard.string(forKey: "ignore-modules")?.components(separatedBy: ",") ?? []
let excludedSuffixTag = UserDefaults.standard.string(forKey: "excluded-suffix-tag") ?? ""
let excludedPrefixTag = UserDefaults.standard.string(forKey: "excluded-prefix-tag") ?? ""
let filesToIgnore = UserDefaults.standard.string(forKey: "ignore-files")?.components(separatedBy: ",") ?? []
let excludePublic = CommandLine.arguments.contains("-exclude-public")

protector = AutomaticSwiftShield(basePath: basePath, projectToBuild: projectToBuild, schemeToBuild: schemeToBuild, modulesToIgnore: Set(modulesToIgnore), classesToIgnore: Set(filesToIgnore), protectedClassNameSize: protectedClassNameSize, excludedPrefixTag: excludedPrefixTag, excludedSuffixTag: excludedSuffixTag)
protector = AutomaticSwiftShield(basePath: basePath, projectToBuild: projectToBuild, schemeToBuild: schemeToBuild, modulesToIgnore: Set(modulesToIgnore), protectedClassNameSize: protectedClassNameSize, excludePublic: excludePublic)
} else {
let tag = UserDefaults.standard.string(forKey: "tag") ?? "__s"
protector = ManualSwiftShield(basePath: basePath, tag: tag, protectedClassNameSize: protectedClassNameSize)
Expand Down