String obfuscation for Ionic Apps


#1

Hi!
In some apps I’m involved there are some string constants that contain some information that’s not supposed to be shared in plain sight (I know, decompiling apps is quite easy and I’m only making things a little more complicated to whoever does this). So as parts of our requirements, these strings have to be obfuscated.

I’ve solved this previously on a Ionic-1 app using Gnirts and a Gulp task to be run before compiling and minimizing all js and css files.
Now I’m trying to do the same with a Ionic app. I copied and edited the webpack.config.js file and added the gnirts-loader with no successful results.

This is the important part in the webpack.config.js file:

    {
        test: /\.js$/,
        loader: [ 
          'gnirts-loader',
          process.env.IONIC_WEBPACK_TRANSPILE_LOADER
           ]
      }

I guess there’s something I’m doing wrong or not understanding in the compilation flow, because my strings still plain between the @mangle tags.
Anybody has tried something like this? I’ll make further tests and share anything I can find.

Thanks, best regards,
José Ignacio


#2

Not precisely the solution but a piece of code that does the job. Don’t know where to place it:

var fs = require('fs'),
path = require('path'),
gnirts = require('gnirts'),
_ = require('lodash');

const walkSync = (dir, filelist = []) => fs.readdirSync(dir)
                                      .map(file => fs.statSync(path.join(dir, file)).isDirectory()
                                                    ? walkSync(path.join(dir, file), filelist) 
                                                    : filelist.concat(path.join(dir, file))[0]);
var fileList = _.chain(walkSync('{{BUILD}}'))
            .flattenDeep()
            .filter((v) => (/\.js$/).test(v))
            .value();

if(!fs.existsSync('{{BUILD}}/mangle')){
     _.each(fileList, (v) => {
          let js = fs.readFileSync(v, { encoding: 'utf8' });
          js = gnirts.mangle(js);
          fs.writeFileSync(v, js);
     });

     fs.writeFileSync('{{BUILD}}/mangle', '');
}

Requires gnirts and lodash.