Skip to content

Commit

Permalink
fix(gateway): content-type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Nov 27, 2019
1 parent e2e8645 commit 9b73c03
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/gateway/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,11 @@ export class Server {
const contentType: string = content.headers['content-type'];

if (
!contentType.includes('text/plain') ||
!contentType.includes('application/javascript')
!contentType ||
(
!contentType.includes('text/plain') &&
!contentType.includes('application/javascript')
)
) {
ctx.throw(400, '该文件不是一个可转换的脚本文件');
return;
Expand Down Expand Up @@ -204,6 +207,7 @@ export class Server {
const contentType: string = content.headers['content-type'];

if (
!contentType ||
!contentType.includes('text/plain')
) {
ctx.throw(400, '该文件不是一个可转换的文件');
Expand Down
2 changes: 1 addition & 1 deletion test/gateway/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ test('qx-rewrite-remote with binary', async t => {
const surgioServer = gateway.createSurgioServer(fixture);
const app = gateway.createKoaApp(surgioServer);

const res = await request(app.callback())
await request(app.callback())
.get('/qx-rewrite-remote?url=https://github.com/crossutility/Quantumult-X/blob/master/sample.conf')
.expect(400);

Expand Down
122 changes: 122 additions & 0 deletions test/gateway/snapshots/index.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,125 @@ Generated by [AVA](https://ava.li).
`🇺🇸US 1 = custom, us.example.com, 443, chacha20-ietf-poly1305, password, https://raw.githubusercontent.com/ConnersHua/SSEncrypt/master/SSEncrypt.module, obfs=tls, obfs-host=gateway-carry.icloud.com␊
🇺🇸US 2 = custom, us.example.com, 443, chacha20-ietf-poly1305, password, https://raw.githubusercontent.com/ConnersHua/SSEncrypt/master/SSEncrypt.module␊
🇺🇲 US = custom, us.example.com, 443, chacha20-ietf-poly1305, password, https://raw.githubusercontent.com/ConnersHua/SSEncrypt/master/SSEncrypt.module, udp-relay=true, obfs=tls, obfs-host=gateway-carry.icloud.com, tfo=true`

## qx-script

> Snapshot 1
`/**␊
* @supported 55BE3B10F8A1␊
* THIS COMMENT IS GENERATED BY SURGIO␊
*/␊
/**␊
* @supported 23AD6B11CD4B 55BE3B10F8A1␊
* The above random generated device ID can be found at the bottom of Quantumult X additional menu, and may be changed when system restored.␊
* Indicate what device are supported by the file. This is necessary when the file is not loaded from local("On My iPhone - Quantumult X - Scripts").␊
*/␊
// $request, $response, $notify(title, subtitle, message), console.log(message)␊
// $request.scheme, $request.method, $request.url, $request.path, $request.headers␊
// $response.statusCode, $response.headers, $response.body␊
// You can optional change the response headers at the same time by using $done({body: modifiedBody, headers: modifiedHeaders}); only change the response headers is not allowed for script-response-body. The modifiedHeaders can be copied and modified from $response.headers, please do not change the content length, type and encoding field.␊
// Response status can also be optional changed by using $done({body: modifiedBody, headers: modifiedHeaders, status: modifiedStatus}), the modifiedStatus should be like "HTTP/1.1 200 OK"␊
var body = $response.body;␊
var obj = JSON.parse(body);␊
obj['result'] = 0;␊
body = JSON.stringify(obj);␊
console.log(body);␊
$done(body);␊
`

> Snapshot 2
`/**␊
* @supported abcdef␊
* THIS COMMENT IS GENERATED BY SURGIO␊
*/␊
/**␊
* @supported 23AD6B11CD4B 55BE3B10F8A1␊
* The above random generated device ID can be found at the bottom of Quantumult X additional menu, and may be changed when system restored.␊
* Indicate what device are supported by the file. This is necessary when the file is not loaded from local("On My iPhone - Quantumult X - Scripts").␊
*/␊
// $request, $response, $notify(title, subtitle, message), console.log(message)␊
// $request.scheme, $request.method, $request.url, $request.path, $request.headers␊
// $response.statusCode, $response.headers, $response.body␊
// You can optional change the response headers at the same time by using $done({body: modifiedBody, headers: modifiedHeaders}); only change the response headers is not allowed for script-response-body. The modifiedHeaders can be copied and modified from $response.headers, please do not change the content length, type and encoding field.␊
// Response status can also be optional changed by using $done({body: modifiedBody, headers: modifiedHeaders, status: modifiedStatus}), the modifiedStatus should be like "HTTP/1.1 200 OK"␊
var body = $response.body;␊
var obj = JSON.parse(body);␊
obj['result'] = 0;␊
body = JSON.stringify(obj);␊
console.log(body);␊
$done(body);␊
`

> Snapshot 3
`/**␊
* @supported abcdef bcdefg␊
* THIS COMMENT IS GENERATED BY SURGIO␊
*/␊
/**␊
* @supported 23AD6B11CD4B 55BE3B10F8A1␊
* The above random generated device ID can be found at the bottom of Quantumult X additional menu, and may be changed when system restored.␊
* Indicate what device are supported by the file. This is necessary when the file is not loaded from local("On My iPhone - Quantumult X - Scripts").␊
*/␊
// $request, $response, $notify(title, subtitle, message), console.log(message)␊
// $request.scheme, $request.method, $request.url, $request.path, $request.headers␊
// $response.statusCode, $response.headers, $response.body␊
// You can optional change the response headers at the same time by using $done({body: modifiedBody, headers: modifiedHeaders}); only change the response headers is not allowed for script-response-body. The modifiedHeaders can be copied and modified from $response.headers, please do not change the content length, type and encoding field.␊
// Response status can also be optional changed by using $done({body: modifiedBody, headers: modifiedHeaders, status: modifiedStatus}), the modifiedStatus should be like "HTTP/1.1 200 OK"␊
var body = $response.body;␊
var obj = JSON.parse(body);␊
obj['result'] = 0;␊
body = JSON.stringify(obj);␊
console.log(body);␊
$done(body);␊
`

> Snapshot 4
`/**␊
* @supported abcdef␊
* THIS COMMENT IS GENERATED BY SURGIO␊
*/␊
/**␊
* @supported 23AD6B11CD4B 55BE3B10F8A1␊
* The above random generated device ID can be found at the bottom of Quantumult X additional menu, and may be changed when system restored.␊
* Indicate what device are supported by the file. This is necessary when the file is not loaded from local("On My iPhone - Quantumult X - Scripts").␊
*/␊
// $request, $response, $notify(title, subtitle, message), console.log(message)␊
// $request.scheme, $request.method, $request.url, $request.path, $request.headers␊
// $response.statusCode, $response.headers, $response.body␊
// You can optional change the response headers at the same time by using $done({body: modifiedBody, headers: modifiedHeaders}); only change the response headers is not allowed for script-response-body. The modifiedHeaders can be copied and modified from $response.headers, please do not change the content length, type and encoding field.␊
// Response status can also be optional changed by using $done({body: modifiedBody, headers: modifiedHeaders, status: modifiedStatus}), the modifiedStatus should be like "HTTP/1.1 200 OK"␊
var body = $response.body;␊
var obj = JSON.parse(body);␊
obj['result'] = 0;␊
body = JSON.stringify(obj);␊
console.log(body);␊
$done(body);␊
`
Binary file modified test/gateway/snapshots/index.test.ts.snap
Binary file not shown.

0 comments on commit 9b73c03

Please sign in to comment.