cmvr-wms/mobile-app/tests/production-network-security.test.mjs

30 lines
2.2 KiB
JavaScript
Raw Normal View History

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {createRequire} from 'node:module';
import vm from 'node:vm';
const require=createRequire(import.meta.url);
const appConfig=require('../app.config.js');
const source=readFileSync(new URL('../plugins/with-internal-network.cjs',import.meta.url),'utf8');
async function generate(env,hosts){
let xml='',manifest;
const plugins={withAndroidManifest:(config,fn)=>{const result=fn({modResults:{manifest:{application:[{$:{}}]}}});manifest=result.modResults.manifest.application[0].$;return config},withDangerousMod:(config,args)=>({config,handler:args[1]})};
const module={exports:{}};
vm.runInNewContext(source,{module,exports:module.exports,require:name=>name==='expo/config-plugins'?plugins:name==='node:fs/promises'?{mkdir:async()=>{},writeFile:async(_path,data)=>{xml=data}}:name==='node:path'?require('node:path'):name==='../config/environment.cjs'?{environment:()=>env}:require(name)});
const result=module.exports({}, {hosts});await result.handler({modRequest:{platformProjectRoot:'dummy'}});return {manifest,xml};
}
test('production APK permits cleartext only for its fixed private endpoint',async()=>{
const original={...process.env};
try{
process.env.APP_ENV='prod';process.env.APP_API_URL='https://wms.svc.cmvr-iot.icu/prod-api';process.env.APP_UPDATE_ORIGIN='https://wms.svc.cmvr-iot.icu';
const config=appConfig(),plugin=config.plugins.find(x=>Array.isArray(x)&&x[0]==='./plugins/with-internal-network.cjs');
assert.deepEqual(plugin[1].hosts,['192.168.28.10']);
const {xml,manifest}=await generate({variant:'prod',apiUrl:process.env.APP_API_URL,updateOrigin:process.env.APP_UPDATE_ORIGIN},plugin[1].hosts);
assert.equal(manifest['android:usesCleartextTraffic'],'false');
assert.match(xml,/<base-config cleartextTrafficPermitted="false"\/>/);
assert.match(xml,/<domain includeSubdomains="false">192\.168\.28\.10<\/domain>/);
assert.equal((xml.match(/<domain /g)||[]).length,1);
await assert.rejects(generate({variant:'prod',apiUrl:process.env.APP_API_URL,updateOrigin:process.env.APP_UPDATE_ORIGIN},['evil.example']));
}finally{process.env=original;}
});