Skip to content

Commit 600505a

Browse files
committed
cleanup github badge validation
1 parent 6adca53 commit 600505a

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/package.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,16 @@ const TrustedSVGSources = [
171171
'www.versioneye.com'
172172
];
173173

174-
function isHostTrusted(host: string): boolean {
175-
return TrustedSVGSources.indexOf(host.toLowerCase()) > -1;
176-
}
177-
178174
function isGitHubRepository(repository: string): boolean {
179175
return /^https:\/\/github\.com\/|^git@github\.com:/.test(repository || '');
180176
}
181177

182178
function isGitHubBadge(href: string): boolean {
183-
return isGitHubRepository(href) && /[A-Za-z0-9_-]{1,100}\/workflows\/[^<>:;,?"*|/]+\/badge\.svg$/.test(href || '');
179+
return /^https:\/\/github\.com\/[^/]+\/[^/]+\/workflows\/.*badge\.svg/.test(href || '');
180+
}
181+
182+
function isHostTrusted(url: url.UrlWithStringQuery): boolean {
183+
return TrustedSVGSources.indexOf(url.host.toLowerCase()) > -1 || isGitHubBadge(url.href);
184184
}
185185

186186
class ManifestProcessor extends BaseProcessor {
@@ -472,7 +472,7 @@ export class MarkdownProcessor extends BaseProcessor {
472472
throw new Error(`Images in ${this.name} must come from an HTTPS source: ${src}`);
473473
}
474474

475-
if (/\.svg$/i.test(srcUrl.pathname) && (!isHostTrusted(srcUrl.host) && !isGitHubBadge(srcUrl.href))) {
475+
if (/\.svg$/i.test(srcUrl.pathname) && (!isHostTrusted(srcUrl))) {
476476
throw new Error(`SVGs are restricted in ${this.name}; please use other file image formats, such as PNG: ${src}`);
477477
}
478478
});
@@ -717,7 +717,7 @@ export function validateManifest(manifest: Manifest): Manifest {
717717
throw new Error(`Badge URLs must come from an HTTPS source: ${badge.url}`);
718718
}
719719

720-
if (/\.svg$/i.test(srcUrl.pathname) && (!isHostTrusted(srcUrl.host) && !isGitHubBadge(srcUrl.href))) {
720+
if (/\.svg$/i.test(srcUrl.pathname) && (!isHostTrusted(srcUrl))) {
721721
throw new Error(`Badge SVGs are restricted. Please use other file image formats, such as PNG: ${badge.url}`);
722722
}
723723
});

src/test/package.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ describe('MarkdownProcessor', () => {
16421642
assert(file);
16431643
});
16441644

1645-
it('should allow SVG from GitHub actions in image tag', async() => {
1645+
it('should allow SVG from GitHub actions in image tag', async () => {
16461646
const manifest = { name: 'test', publisher: 'mocha', version: '0.0.1', engines: Object.create(null), repository: 'https://github.com/username/repository' };
16471647
const contents = `![title](https://github.com/fakeuser/fakerepo/workflows/fakeworkflowname/badge.svg)`;
16481648
const processor = new ReadmeProcessor(manifest, {});
@@ -1652,7 +1652,7 @@ describe('MarkdownProcessor', () => {
16521652
assert(file);
16531653
});
16541654

1655-
it('should prevent SVG from a GitHub repo in image tag', async() => {
1655+
it('should prevent SVG from a GitHub repo in image tag', async () => {
16561656
const manifest = { name: 'test', publisher: 'mocha', version: '0.0.1', engines: Object.create(null), repository: 'https://github.com/username/repository' };
16571657
const contents = `![title](https://github.com/eviluser/evilrepo/blob/master/malicious.svg)`;
16581658
const processor = new ReadmeProcessor(manifest, {});

0 commit comments

Comments
 (0)