Skip to content

Commit 4221d0b

Browse files
committed
add general facility for admins to block certain urls from proxying on the share server
1 parent 6b10369 commit 4221d0b

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/packages/next/lib/share/proxy/api.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const RAW_MAX_SIZE_BYTES = 10000000; // 10MB
4343
export async function rawText(
4444
githubOrg: string,
4545
githubRepo: string,
46-
segments: string[]
46+
segments: string[],
4747
): Promise<string> {
4848
const url = rawURL(githubOrg, githubRepo, segments);
4949
//console.log("raw:", { url });
@@ -53,10 +53,10 @@ export async function rawText(
5353
function rawURL(
5454
githubOrg: string,
5555
githubRepo: string,
56-
segments: string[]
56+
segments: string[],
5757
): string {
5858
return `https://raw.githubusercontent.com/${githubOrg}/${githubRepo}/${join(
59-
...segments.slice(1)
59+
...segments.slice(1),
6060
)}`;
6161
}
6262

@@ -77,22 +77,46 @@ interface GithubFile {
7777
async function credentials(): Promise<{
7878
github_username?: string;
7979
github_token?: string;
80+
github_block?: string;
8081
}> {
8182
const pool = getPool("long");
8283
const { rows } = await pool.query(
83-
"SELECT name, value FROM server_settings WHERE name='github_username' OR name='github_token'"
84+
"SELECT name, value FROM server_settings WHERE name='github_username' OR name='github_token' OR name='github_block'",
8485
);
85-
let result: { github_username?: string; github_token?: string } = {};
86+
let result: {
87+
github_username?: string;
88+
github_token?: string;
89+
github_block?: string;
90+
} = {};
8691
for (const row of rows) {
8792
result[row.name] = row.value;
8893
}
8994
return result;
9095
}
9196

97+
function isBlocked(path: string, github_block?: string) {
98+
if (!github_block) {
99+
return false;
100+
}
101+
const path1 = path.toLowerCase();
102+
for (const x of github_block.split(",")) {
103+
const y = x.trim().toLowerCase();
104+
if (path1.includes(y)) {
105+
return true;
106+
}
107+
}
108+
return false;
109+
}
110+
92111
export async function api(path: string): Promise<any> {
93112
const url = `https://api.github.com/${path}`;
94113
const options: any = {};
95-
const { github_username, github_token } = await credentials();
114+
const { github_username, github_token, github_block } = await credentials();
115+
if (isBlocked(path, github_block)) {
116+
throw Error(
117+
`Path '${path}' is blocked by the site admins. If you think this is a mistake, please contact support.`,
118+
);
119+
}
96120
if (github_username && github_token) {
97121
options.headers = new Headers({
98122
Authorization: "Basic " + encode(`${github_username}:${github_token}`),
@@ -120,7 +144,7 @@ export async function api(path: string): Promise<any> {
120144
export async function contents(
121145
githubOrg: string,
122146
githubRepo: string,
123-
segments: string[]
147+
segments: string[],
124148
): Promise<GithubFile[]> {
125149
let ref, path;
126150
if (segments.length == 0) {
@@ -134,19 +158,19 @@ export async function contents(
134158
const result = await api(
135159
`repos/${githubOrg}/${githubRepo}/contents/${path}${
136160
ref ? "?ref=" + ref : ""
137-
}`
161+
}`,
138162
);
139163
if (result.name != null) {
140164
throw Error(
141-
"only use contents to get directory listing, not to get file contents"
165+
"only use contents to get directory listing, not to get file contents",
142166
);
143167
}
144168
return result;
145169
}
146170

147171
export async function defaultBranch(
148172
githubOrg: string,
149-
githubRepo: string
173+
githubRepo: string,
150174
): Promise<string> {
151175
return (await api(`repos/${githubOrg}/${githubRepo}`)).default_branch;
152176
}

src/packages/util/db-schema/site-settings-extras.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ export type SiteSettingsExtrasKeys =
230230
| "github_project_id"
231231
| "github_username"
232232
| "github_token"
233+
| "github_block"
233234
| "prometheus_metrics"
234235
| "pay_as_you_go_section"
235236
| "pay_as_you_go_spending_limit"
@@ -527,6 +528,13 @@ export const EXTRAS: SettingsExtras = {
527528
show: () => true,
528529
tags: ["GitHub"],
529530
},
531+
github_block: {
532+
name: "GitHub Abuse Block",
533+
desc: "In case of **abuse**, you can block proxying of any GitHub URL that contains any string in this comma separated list.",
534+
default: "",
535+
show: () => true,
536+
tags: ["GitHub"],
537+
},
530538
email_section: {
531539
name: "Email Configuration",
532540
desc: "",

0 commit comments

Comments
 (0)