Search code examples
javascriptiosswiftobfuscation

Can Swift Scripts Execute in a Swift iOS App?


We have an iOS app (Obj-C) that grabs a JavaScript file from AWS S3 to run our proprietary algorithm on a bunch of raw image data right on the iPhone (Lambda functions produced some latency I guess). For the sake of security, the JavaScript is obfuscated and entirely unreadable, and even harder to debug.

Well, the JavaScript is spitting out NaN result values back to the iPhone, and I am seriously struggling to even find where this NaN pollution starts. Very frustrating.

Behind the scenes I am working on refactoring this entire project to leverage Swift instead. It's much easier to dev/maintain than Objective-C (IMHO). Also, Swift can be used for scripting, so I'm thinking I can just reduce my tech stack by removing the JavaScript entirely.

Question: Can a Swift script execute in a Swift iPhone app the same way JavaScript can? If yes, how?

The Objective-C code snippet (modified for brevity)

functionCall = [NSString stringWithFormat: @"some JS function call"];
[jsContext evaluateScript: functionCall];
JSValue* jsv = [jsContext objectForKeyedSubscript: @"result"];
NSDictionary* result = [jsv toDictionary];
// result is != nil, but the values are all NaN
// Can't find the error when debugging JavaScript in Chrome
// C# utility app that does the same thing produces good results

I am aware that Apple will reject an app for the App Store if using an obfuscation tool on the source code is detected, or I'd consider purchasing a license for one. Also, we have an identical Android app, so if I can do the same with Kotlin scripts then I'll be a happy camper.

Another concern I have is if the obfuscated JavaScript code is even increasing the security. I understand that a jailbroken device can access device memory and get a hold of code at runtime, which makes me think I'm performing an exercise in futility. Phrased differently, is the obfuscated JavaScript effectively more secure than manually/primitively obfuscated Swift source code?


Solution

  • As user jvnpdx stated, it is not possible to dynamically run a Swift script within the app.