diff --git a/src/tasks/apply-patches.ts b/src/tasks/apply-patches.ts index 9809e15..2617e3e 100644 --- a/src/tasks/apply-patches.ts +++ b/src/tasks/apply-patches.ts @@ -5,6 +5,7 @@ import modifyManifest from './modify-manifest' import createNetworkSecurityConfig from './create-netsec-config' import disableCertificatePinning from './disable-certificate-pinning' import copyCertificateFile from './copy-certificate-file' +import fixXmlRes from "./fix-xml-res"; export default function applyPatches( decodeDir: string, @@ -49,5 +50,9 @@ export default function applyPatches( title: 'Disabling certificate pinning', task: (_, task) => disableCertificatePinning(decodeDir, task), }, + { + title: 'Fix strings in XML res', + task: (_, task) => fixXmlRes(decodeDir, task), + }, ]) } diff --git a/src/tasks/fix-xml-res.ts b/src/tasks/fix-xml-res.ts new file mode 100644 index 0000000..b45f654 --- /dev/null +++ b/src/tasks/fix-xml-res.ts @@ -0,0 +1,42 @@ +import globby = require('globby') +import { ListrTaskWrapper } from 'listr' +import * as fs from '../utils/fs' + +import observeAsync from '../utils/observe-async' +import buildGlob from '../utils/build-glob' + +const escapeXmlTags = (value: string): string => { + return value.replace(//g, '>'); +}; + +const processXmlFile = async (filePath: string): Promise => { + const xml = await fs.readFile(filePath, 'utf8'); + let newXml = xml; + + const stringRegex = /(.*?)<\/string>/gs; + let match: RegExpExecArray | null; + while ((match = stringRegex.exec(xml)) !== null) { + const [, name, value] = match; + if (value.includes('>') || value.includes('<')) { + const escapedValue = escapeXmlTags(value); + newXml = newXml.replace(value, `${escapedValue}`); + } + } + + await fs.writeFile(filePath, newXml, 'utf8'); +}; + +export default async function fixXmlRes( + directoryPath: string, + task: ListrTaskWrapper, +) { + return observeAsync(async log => { + const resStringsGlob = buildGlob(directoryPath, 'res/*/strings.xml') + + log('Scanning strings in XML res...') + for await (const filePathChunk of globby.stream(resStringsGlob)) { + const filePath = filePathChunk as string + await processXmlFile(filePath); + } + }) +}