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

fix: xml parser error not well-formed (invalid token) #133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/tasks/apply-patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
},
])
}
42 changes: 42 additions & 0 deletions src/tasks/fix-xml-res.ts
Original file line number Diff line number Diff line change
@@ -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, '&lt;').replace(/>/g, '&gt;');
};

const processXmlFile = async (filePath: string): Promise<void> => {
const xml = await fs.readFile(filePath, 'utf8');
let newXml = xml;

const stringRegex = /<string name="(.*?)">(.*?)<\/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, `<string name="${name}">${escapedValue}</string>`);
}
}

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);
}
})
}