Skip to content

Commit 551e2a2

Browse files
author
Bryan Clark
committed
sanitzie XML characters
1 parent 9d56a3b commit 551e2a2

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

__tests__/auth.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,22 @@ describe('auth tests', () => {
8282
expect(fs.existsSync(m2Dir)).toBe(false);
8383
expect(fs.existsSync(settingsFile)).toBe(false);
8484
}, 100000);
85+
86+
it('escapes invalid XML inputs', () => {
87+
const id = 'packages';
88+
const username = 'bluebottle';
89+
const password = '&<>"\'\'"><&';
90+
91+
expect(auth.generate(id, username, password)).toEqual(`
92+
<settings>
93+
<servers>
94+
<server>
95+
<id>${id}</id>
96+
<username>${username}</username>
97+
<password>&amp;&lt;&gt;&quot;&apos;&apos;&quot;&gt;&lt;&amp;</password>
98+
</server>
99+
</servers>
100+
</settings>
101+
`);
102+
});
85103
});

dist/index.js

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/auth.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,24 @@ export async function configAuthentication(
2727
}
2828
}
2929

30+
function escapeXML(value: string) {
31+
return value
32+
.replace(/&/g, '&amp;')
33+
.replace(/</g, '&lt;')
34+
.replace(/>/g, '&gt;')
35+
.replace(/"/g, '&quot;')
36+
.replace(/'/g, '&apos;');
37+
}
38+
3039
// only exported for testing purposes
3140
export function generate(id: string, username: string, password: string) {
3241
return `
3342
<settings>
3443
<servers>
3544
<server>
36-
<id>${id}</id>
37-
<username>${username}</username>
38-
<password>${password}</password>
45+
<id>${escapeXML(id)}</id>
46+
<username>${escapeXML(username)}</username>
47+
<password>${escapeXML(password)}</password>
3948
</server>
4049
</servers>
4150
</settings>

0 commit comments

Comments
 (0)