Skip to content

Commit 8bfe499

Browse files
edusperoniNathanWalker
authored andcommitted
fix(android): handle self-closing and single-quoted debug manifests
The dev cleartext change left an app's debug manifest with a self-closing `<manifest/>` root unchanged apart from the marker, so the permission was never added and later prepares skipped it. It also missed single-quoted `usesCleartextTraffic` and `tools:replace` attributes and added duplicates next to them. Expand a self-closing root before adding the application element, leave a manifest it can't complete unmarked, and match both quote styles, keeping the quote style of an existing `tools:replace`.
1 parent d125a81 commit 8bfe499

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎lib/services/android/dev-cleartext-manifest.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,44 @@ export function withDevCleartext(manifest: string): string {
5959

6060
const applicationTag = /<application\b[^>]*?(\/?)>/.exec(result);
6161
if (!applicationTag) {
62-
result = result.replace(
63-
/<\/manifest>/,
64-
`\t<application ${CLEARTEXT_ATTRIBUTE}="true" tools:replace="${CLEARTEXT_ATTRIBUTE}" />\n</manifest>`,
65-
);
62+
const application = `\t<application ${CLEARTEXT_ATTRIBUTE}="true" tools:replace="${CLEARTEXT_ATTRIBUTE}" />`;
63+
const root = /<manifest\b[^>]*>/.exec(result)[0];
64+
if (root.endsWith("/>")) {
65+
result = result.replace(
66+
root,
67+
`${root.slice(0, -2).trimEnd()}>\n${application}\n</manifest>`,
68+
);
69+
} else if (/<\/manifest>/.test(result)) {
70+
result = result.replace(/<\/manifest>/, `${application}\n</manifest>`);
71+
} else {
72+
// Without the marker, the next prepare tries again.
73+
return manifest;
74+
}
6675
} else {
6776
let tag = applicationTag[0];
68-
const value = new RegExp(`${CLEARTEXT_ATTRIBUTE}\\s*=\\s*"[^"]*"`);
77+
const value = /android:usesCleartextTraffic\s*=\s*(?:"[^"]*"|'[^']*')/;
6978
tag = value.test(tag)
7079
? tag.replace(value, `${CLEARTEXT_ATTRIBUTE}="true"`)
7180
: tag.replace(
7281
/^<application\b/,
7382
`<application ${CLEARTEXT_ATTRIBUTE}="true"`,
7483
);
75-
const replace = /tools:replace\s*=\s*"([^"]*)"/.exec(tag);
84+
const replace = /tools:replace\s*=\s*(["'])(.*?)\1/.exec(tag);
7685
if (!replace) {
7786
tag = tag.replace(
7887
/^<application\b/,
7988
`<application tools:replace="${CLEARTEXT_ATTRIBUTE}"`,
8089
);
8190
} else if (
82-
!replace[1]
91+
!replace[2]
8392
.split(",")
8493
.map((name) => name.trim())
8594
.includes(CLEARTEXT_ATTRIBUTE)
8695
) {
96+
const [, quote, names] = replace;
8797
tag = tag.replace(
8898
replace[0],
89-
`tools:replace="${replace[1]},${CLEARTEXT_ATTRIBUTE}"`,
99+
`tools:replace=${quote}${names},${CLEARTEXT_ATTRIBUTE}${quote}`,
90100
);
91101
}
92102
result = result.replace(applicationTag[0], tag);

‎test/services/android/dev-cleartext-manifest.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,35 @@ describe("dev-cleartext-manifest", () => {
9696
/<application android:usesCleartextTraffic="true" tools:replace="android:usesCleartextTraffic" \/>\n<\/manifest>/,
9797
);
9898
});
99+
100+
it("expands a self-closing manifest root to add the application element", () => {
101+
const result = withDevCleartext(
102+
'<manifest xmlns:android="http://schemas.android.com/apk/res/android" />',
103+
);
104+
assert.match(
105+
result,
106+
/<manifest xmlns:tools="http:\/\/schemas.android.com\/tools" xmlns:android="http:\/\/schemas.android.com\/apk\/res\/android">\n\t<application android:usesCleartextTraffic="true" tools:replace="android:usesCleartextTraffic" \/>\n<\/manifest>$/,
107+
);
108+
assert.isTrue(hasDevCleartext(result));
109+
});
110+
111+
it("leaves a manifest it can't complete unmarked", () => {
112+
const manifest =
113+
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">';
114+
assert.equal(withDevCleartext(manifest), manifest);
115+
});
116+
117+
it("rewrites single-quoted attributes instead of duplicating them", () => {
118+
const result = withDevCleartext(
119+
"<manifest xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools'><application android:usesCleartextTraffic='false' tools:replace='android:label'/></manifest>",
120+
);
121+
assert.equal(result.match(/usesCleartextTraffic=/g).length, 1);
122+
assert.include(result, 'android:usesCleartextTraffic="true"');
123+
assert.equal(result.match(/tools:replace=/g).length, 1);
124+
assert.include(
125+
result,
126+
"tools:replace='android:label,android:usesCleartextTraffic'",
127+
);
128+
});
99129
});
100130
});

0 commit comments

Comments
 (0)