@@ -43,7 +43,7 @@ export const RAW_MAX_SIZE_BYTES = 10000000; // 10MB
43
43
export async function rawText (
44
44
githubOrg : string ,
45
45
githubRepo : string ,
46
- segments : string [ ]
46
+ segments : string [ ] ,
47
47
) : Promise < string > {
48
48
const url = rawURL ( githubOrg , githubRepo , segments ) ;
49
49
//console.log("raw:", { url });
@@ -53,10 +53,10 @@ export async function rawText(
53
53
function rawURL (
54
54
githubOrg : string ,
55
55
githubRepo : string ,
56
- segments : string [ ]
56
+ segments : string [ ] ,
57
57
) : string {
58
58
return `https://raw.githubusercontent.com/${ githubOrg } /${ githubRepo } /${ join (
59
- ...segments . slice ( 1 )
59
+ ...segments . slice ( 1 ) ,
60
60
) } `;
61
61
}
62
62
@@ -77,22 +77,46 @@ interface GithubFile {
77
77
async function credentials ( ) : Promise < {
78
78
github_username ?: string ;
79
79
github_token ?: string ;
80
+ github_block ?: string ;
80
81
} > {
81
82
const pool = getPool ( "long" ) ;
82
83
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'" ,
84
85
) ;
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
+ } = { } ;
86
91
for ( const row of rows ) {
87
92
result [ row . name ] = row . value ;
88
93
}
89
94
return result ;
90
95
}
91
96
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
+
92
111
export async function api ( path : string ) : Promise < any > {
93
112
const url = `https://api.github.com/${ path } ` ;
94
113
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
+ }
96
120
if ( github_username && github_token ) {
97
121
options . headers = new Headers ( {
98
122
Authorization : "Basic " + encode ( `${ github_username } :${ github_token } ` ) ,
@@ -120,7 +144,7 @@ export async function api(path: string): Promise<any> {
120
144
export async function contents (
121
145
githubOrg : string ,
122
146
githubRepo : string ,
123
- segments : string [ ]
147
+ segments : string [ ] ,
124
148
) : Promise < GithubFile [ ] > {
125
149
let ref , path ;
126
150
if ( segments . length == 0 ) {
@@ -134,19 +158,19 @@ export async function contents(
134
158
const result = await api (
135
159
`repos/${ githubOrg } /${ githubRepo } /contents/${ path } ${
136
160
ref ? "?ref=" + ref : ""
137
- } `
161
+ } `,
138
162
) ;
139
163
if ( result . name != null ) {
140
164
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" ,
142
166
) ;
143
167
}
144
168
return result ;
145
169
}
146
170
147
171
export async function defaultBranch (
148
172
githubOrg : string ,
149
- githubRepo : string
173
+ githubRepo : string ,
150
174
) : Promise < string > {
151
175
return ( await api ( `repos/${ githubOrg } /${ githubRepo } ` ) ) . default_branch ;
152
176
}
0 commit comments