Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 💩 Move to ESM as much as I can #14619

Merged
merged 10 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
8 changes: 0 additions & 8 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,8 @@ updates:
- dependency-name: jsrsasign
versions: ['>=11.0.0'] # no longer includes KJUR.crypto.Cipher for RSA
# ESM only packages
- dependency-name: fanfou-sdk
versions: ['>=5.0.0']
- dependency-name: got
versions: ['>=12.0.0']
- dependency-name: ip-regex
versions: ['>=5.0.0']
- dependency-name: query-string
versions: ['>=8.0.0']
- dependency-name: rand-user-agent
versions: ['>=2.0.1']
- dependency-name: remark-parse
versions: ['>=10.0.0']
- dependency-name: remark-preset-prettier
Expand Down
202 changes: 131 additions & 71 deletions lib/middleware/access-control.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { describe, expect, it, jest, afterEach } from '@jest/globals';
import supertest from 'supertest';
import md5 from '@/utils/md5';
import type { serve } from '@hono/node-server';

let server: ReturnType<typeof serve>;

process.env.NODE_NAME = 'mock';

function checkBlock(response) {
async function checkBlock(response) {
expect(response.status).toBe(403);
expect(response.text).toMatch(/Access denied\./);
expect(await response.text()).toMatch(/Access denied\./);
}

afterEach(() => {
delete process.env.ACCESS_KEY;
delete process.env.DENYLIST;
delete process.env.ALLOWLIST;
jest.resetModules();
server.close();
});

describe('access-control', () => {
Expand All @@ -26,46 +21,77 @@ describe('access-control', () => {
const code = md5('/test/2' + key);
process.env.DENYLIST = 'est/1,233.233.233.,black';
process.env.ACCESS_KEY = key;
server = (await import('@/index')).default;
const request = supertest(server);

const response11 = await request.get('/test/1');
checkBlock(response11);

const response12 = await request.get('/test/1').set('X-Mock-IP', '233.233.233.233');
checkBlock(response12);

const response13 = await request.get('/test/1').set('user-agent', 'blackua');
checkBlock(response13);

const response21 = await request.get('/test/2');
const app = (await import('@/app')).default;

const response11 = await app.request('/test/1');
await checkBlock(response11);

const response12 = await app.request('/test/1', {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
await checkBlock(response12);

const response13 = await app.request('/test/1', {
headers: {
'user-agent': 'blackua',
},
});
await checkBlock(response13);

const response21 = await app.request('/test/2');
expect(response21.status).toBe(200);

const response22 = await request.get('/test/2').set('X-Mock-IP', '233.233.233.233');
checkBlock(response22);
const response22 = await app.request('/test/2', {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
await checkBlock(response22);

const response23 = await request.get('/test/2').set('user-agent', 'blackua');
checkBlock(response23);
const response23 = await app.request('/test/2', {
headers: {
'user-agent': 'blackua',
},
});
await checkBlock(response23);

// wrong key/code, not on denylist
const response311 = await request.get(`/test/2?key=wrong+${key}`);
const response311 = await app.request(`/test/2?key=wrong+${key}`);
expect(response311.status).toBe(200);

const response312 = await request.get(`/test/2?code=wrong+${code}`);
const response312 = await app.request(`/test/2?code=wrong+${code}`);
expect(response312.status).toBe(200);

// wrong key/code, on denylist
const response321 = await request.get(`/test/2?key=wrong+${key}`).set('X-Mock-IP', '233.233.233.233');
checkBlock(response321);

const response322 = await request.get(`/test/2?code=wrong+${code}`).set('X-Mock-IP', '233.233.233.233');
checkBlock(response322);
const response321 = await app.request(`/test/2?key=wrong+${key}`, {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
await checkBlock(response321);

const response322 = await app.request(`/test/2?code=wrong+${code}`, {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
await checkBlock(response322);

// right key/code, on denylist
const response331 = await request.get(`/test/2?key=${key}`).set('X-Mock-IP', '233.233.233.233');
const response331 = await app.request(`/test/2?key=${key}`, {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
expect(response331.status).toBe(200);

const response332 = await request.get(`/test/2?code=${code}`).set('X-Mock-IP', '233.233.233.233');
const response332 = await app.request(`/test/2?code=${code}`, {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
expect(response332.status).toBe(200);
});

Expand All @@ -74,95 +100,129 @@ describe('access-control', () => {
const code = md5('/test/2' + key);
process.env.ALLOWLIST = 'est/1,233.233.233.,103.31.4.0/22,white';
process.env.ACCESS_KEY = key;
server = (await import('@/index')).default;
const request = supertest(server);
const app = (await import('@/app')).default;

const response01 = await request.get('/');
const response01 = await app.request('/');
expect(response01.status).toBe(200);

const response02 = await request.get('/robots.txt');
const response02 = await app.request('/robots.txt');
expect(response02.status).toBe(404);

const response11 = await request.get('/test/1');
const response11 = await app.request('/test/1');
expect(response11.status).toBe(200);

const response12 = await request.get('/test/1').set('X-Mock-IP', '233.233.233.233');
const response12 = await app.request('/test/1', {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
expect(response12.status).toBe(200);

const response13 = await request.get('/test/1').set('user-agent', 'whiteua');
const response13 = await app.request('/test/1', {
headers: {
'user-agent': 'whiteua',
},
});
expect(response13.status).toBe(200);

const response21 = await request.get('/test/2');
checkBlock(response21);
const response21 = await app.request('/test/2');
await checkBlock(response21);

const response22 = await request.get('/test/2').set('X-Mock-IP', '233.233.233.233');
const response22 = await app.request('/test/2', {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
expect(response22.status).toBe(200);

const response221 = await request.get('/test/2').set('X-Mock-IP', '103.31.4.0');
const response221 = await app.request('/test/2', {
headers: {
'X-Mock-IP': '103.31.4.0',
},
});
expect(response221.status).toBe(200);

const response222 = await request.get('/test/2').set('X-Mock-IP', '103.31.7.255');
const response222 = await app.request('/test/2', {
headers: {
'X-Mock-IP': '103.31.7.255',
},
});
expect(response222.status).toBe(200);

const response223 = await request.get('/test/2').set('X-Mock-IP', '103.31.8.0');
checkBlock(response223);

const response23 = await request.get('/test/2').set('user-agent', 'whiteua');
const response223 = await app.request('/test/2', {
headers: {
'X-Mock-IP': '103.31.8.0',
},
});
await checkBlock(response223);

const response23 = await app.request('/test/2', {
headers: {
'user-agent': 'whiteua',
},
});
expect(response23.status).toBe(200);

// wrong key/code, not on allowlist
const response311 = await request.get(`/test/2?code=wrong+${code}`);
checkBlock(response311);
const response311 = await app.request(`/test/2?code=wrong+${code}`);
await checkBlock(response311);

const response312 = await request.get(`/test/2?key=wrong+${key}`);
checkBlock(response312);
const response312 = await app.request(`/test/2?key=wrong+${key}`);
await checkBlock(response312);

// wrong key/code, on allowlist
const response321 = await request.get(`/test/2?code=wrong+${code}`).set('X-Mock-IP', '233.233.233.233');
const response321 = await app.request(`/test/2?code=wrong+${code}`, {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
expect(response321.status).toBe(200);

const response322 = await request.get(`/test/2?key=wrong+${key}`).set('X-Mock-IP', '233.233.233.233');
const response322 = await app.request(`/test/2?key=wrong+${key}`, {
headers: {
'X-Mock-IP': '233.233.233.233',
},
});
expect(response322.status).toBe(200);

// right key/code
const response331 = await request.get(`/test/2?code=${code}`);
const response331 = await app.request(`/test/2?code=${code}`);
expect(response331.status).toBe(200);

const response332 = await request.get(`/test/2?key=${key}`);
const response332 = await app.request(`/test/2?key=${key}`);
expect(response332.status).toBe(200);
});

it(`no list`, async () => {
const key = '1L0veRSSHub';
const code = md5('/test/2' + key);
process.env.ACCESS_KEY = key;
server = (await import('@/index')).default;
const request = supertest(server);
const app = (await import('@/app')).default;

const response01 = await request.get('/');
const response01 = await app.request('/');
expect(response01.status).toBe(200);

const response02 = await request.get('/robots.txt');
const response02 = await app.request('/robots.txt');
expect(response02.status).toBe(404);

const response11 = await request.get('/test/1');
checkBlock(response11);
const response11 = await app.request('/test/1');
await checkBlock(response11);

const response21 = await request.get('/test/2');
checkBlock(response21);
const response21 = await app.request('/test/2');
await checkBlock(response21);

// wrong key/code
const response321 = await request.get(`/test/2?key=wrong+${key}`);
checkBlock(response321);
const response321 = await app.request(`/test/2?key=wrong+${key}`);
await checkBlock(response321);

const response322 = await request.get(`/test/2?code=wrong+${code}`);
checkBlock(response322);
const response322 = await app.request(`/test/2?code=wrong+${code}`);
await checkBlock(response322);

// right key/code
const response331 = await request.get(`/test/2?key=${key}`);
const response331 = await app.request(`/test/2?key=${key}`);
expect(response331.status).toBe(200);

const response332 = await request.get(`/test/2?code=${code}`);
const response332 = await app.request(`/test/2?code=${code}`);
expect(response332.status).toBe(200);
});
});
26 changes: 12 additions & 14 deletions lib/middleware/anti-hotlink.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { describe, expect, it, jest, afterEach, afterAll } from '@jest/globals';
import supertest from 'supertest';
import Parser from 'rss-parser';
import querystring from 'query-string';
import type { serve } from '@hono/node-server';

const parser = new Parser();
jest.setTimeout(50000);
let server: ReturnType<typeof serve>;

afterAll(() => {
delete process.env.HOTLINK_TEMPLATE;
Expand All @@ -20,7 +16,6 @@ afterEach(() => {
delete process.env.HOTLINK_INCLUDE_PATHS;
delete process.env.HOTLINK_EXCLUDE_PATHS;
delete process.env.ALLOW_USER_HOTLINK_TEMPLATE;
server.close();
jest.resetModules();
});

Expand Down Expand Up @@ -122,17 +117,21 @@ const expects = {
};

const testAntiHotlink = async (path, expectObj, query?: string | Record<string, any>) => {
server = (await import('@/index')).default;
const request = supertest(server);
const app = (await import('@/app')).default;

let queryStr;
if (query) {
queryStr = typeof query === 'string' ? query : querystring.stringify(query);
queryStr =
typeof query === 'string'
? query
: Object.entries(query)
.map(([key, value]) => `${key}=${value}`)
.join('&');
}
path = path + (queryStr ? `?${queryStr}` : '');

const response = await request.get(path);
const parsed = await parser.parseString(response.text);
const response = await app.request(path);
const parsed = await parser.parseString(await response.text());
expect({
items: parsed.items.slice(0, expectObj.items.length).map((i) => i.content),
desc: parsed.description,
Expand Down Expand Up @@ -247,9 +246,8 @@ describe('anti-hotlink', () => {

it('invalid-property', async () => {
process.env.HOTLINK_TEMPLATE = 'https://i3.wp.com/${createObjectURL}';
server = (await import('@/index')).default;
const request = supertest(server);
const response = await request.get('/test/complicated');
expect(response.text).toContain('Error: Invalid URL property: createObjectURL');
const app = (await import('@/app')).default;
const response = await app.request('/test/complicated');
expect(await response.text()).toContain('Error: Invalid URL property: createObjectURL');
});
});
Loading
Loading