Skip to content

Commit 37d90fb

Browse files
author
Jackson Kearl
committed
1 parent d0fc46e commit 37d90fb

2 files changed

Lines changed: 40 additions & 12 deletions

File tree

src/vs/workbench/contrib/url/browser/trustedDomains.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ export async function configureOpenerTrustedDomainsHandler(
127127
return [];
128128
}
129129

130+
// Exported for testing.
131+
export function extractGitHubRemotesFromGitConfig(gitConfig: string): string[] {
132+
const domains = new Set<string>();
133+
let match: RegExpExecArray | null;
134+
135+
const RemoteMatcher = /^\s*url\s*=\s*(?:git@|https:\/\/)github\.com(?::|\/)(\S*)\s*$/mg;
136+
while (match = RemoteMatcher.exec(gitConfig)) {
137+
const repo = match[1].replace(/\.git$/, '');
138+
if (repo) {
139+
domains.add(`https://github.com/${repo}/`);
140+
}
141+
}
142+
return [...domains];
143+
}
144+
130145
async function getRemotes(fileService: IFileService, textFileService: ITextFileService, contextService: IWorkspaceContextService): Promise<string[]> {
131146
const workspaceUris = contextService.getWorkspace().folders.map(folder => folder.uri);
132147
const domains = await Promise.all<string[]>(workspaceUris.map(async workspaceUri => {
@@ -136,18 +151,8 @@ async function getRemotes(fileService: IFileService, textFileService: ITextFileS
136151
if (!exists) {
137152
return [];
138153
}
139-
const content = (await (textFileService.read(uri, { acceptTextOnly: true }).catch(() => ({ value: '' })))).value;
140-
const domains = new Set<string>();
141-
let match: RegExpExecArray | null;
142-
143-
const RemoteMatcher = /^\s*url\s*=\s*(?:git@|https:\/\/)github\.com(?::|\/)(\S*)(?:\.git)?\s*$/mg;
144-
while (match = RemoteMatcher.exec(content)) {
145-
const repo = match[1];
146-
if (repo) {
147-
domains.add(`https://github.com/${repo}/`);
148-
}
149-
}
150-
return [...domains];
154+
const gitConfig = (await (textFileService.read(uri, { acceptTextOnly: true }).catch(() => ({ value: '' })))).value;
155+
return extractGitHubRemotesFromGitConfig(gitConfig);
151156
}));
152157

153158
const set = domains.reduce((set, list) => list.reduce((set, item) => set.add(item), set), new Set<string>());

src/vs/workbench/contrib/url/test/browser/trustedDomains.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as assert from 'assert';
77

88
import { isURLDomainTrusted } from 'vs/workbench/contrib/url/browser/trustedDomainsValidator';
99
import { URI } from 'vs/base/common/uri';
10+
import { extractGitHubRemotesFromGitConfig } from 'vs/workbench/contrib/url/browser/trustedDomains';
1011

1112
function linkAllowedByRules(link: string, rules: string[]) {
1213
assert.ok(isURLDomainTrusted(URI.parse(link), rules), `Link\n${link}\n should be protected by rules\n${JSON.stringify(rules)}`);
@@ -15,6 +16,28 @@ function linkNotAllowedByRules(link: string, rules: string[]) {
1516
assert.ok(!isURLDomainTrusted(URI.parse(link), rules), `Link\n${link}\n should NOT be protected by rules\n${JSON.stringify(rules)}`);
1617
}
1718

19+
suite('GitHub remote extraction', () => {
20+
test('All known formats', () => {
21+
assert.deepEqual(
22+
extractGitHubRemotesFromGitConfig(
23+
`
24+
[remote "1"]
25+
url = git@github.com:sshgit/vscode.git
26+
[remote "2"]
27+
url = git@github.com:ssh/vscode
28+
[remote "3"]
29+
url = https://github.com/httpsgit/vscode.git
30+
[remote "4"]
31+
url = https://github.com/https/vscode`),
32+
[
33+
'https://github.com/sshgit/vscode/',
34+
'https://github.com/ssh/vscode/',
35+
'https://github.com/httpsgit/vscode/',
36+
'https://github.com/https/vscode/'
37+
]);
38+
});
39+
});
40+
1841
suite('Link protection domain matching', () => {
1942
test('simple', () => {
2043
linkNotAllowedByRules('https://x.org', []);

0 commit comments

Comments
 (0)