Skip to content

Commit

Permalink
Fix: drive/files 系の type の validater Fix #2386
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 authored and fs5m8 committed Jan 9, 2023
1 parent 22f9ea5 commit ea2bc3f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/misc/mime-type-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const typeFilterValidater = /^([a-z]+)[/]([*]|[0-9A-Za-z+.-]+)$/;
3 changes: 2 additions & 1 deletion src/server/api/endpoints/drive/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ID } from '../../../../misc/cafy-id';
import define from '../../define';
import { DriveFiles } from '../../../../models';
import { makePaginationQuery } from '../../common/make-pagination-query';
import { typeFilterValidater } from '../../../../misc/mime-type-filter';

export const meta = {
desc: {
Expand Down Expand Up @@ -36,7 +37,7 @@ export const meta = {
},

type: {
validator: $.optional.str.match(/^[a-zA-Z\/\-*]+$/)
validator: $.optional.str.match(typeFilterValidater)
}
},

Expand Down
3 changes: 2 additions & 1 deletion src/server/api/endpoints/drive/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ID } from '../../../../misc/cafy-id';
import define from '../../define';
import { DriveFiles } from '../../../../models';
import { makePaginationQuery } from '../../common/make-pagination-query';
import { typeFilterValidater } from '../../../../misc/mime-type-filter';

export const meta = {
tags: ['drive'],
Expand All @@ -26,7 +27,7 @@ export const meta = {
},

type: {
validator: $.optional.str.match(/^[a-zA-Z\/\-*]+$/)
validator: $.optional.str.match(typeFilterValidater)
}
},

Expand Down
56 changes: 56 additions & 0 deletions test/mime-type-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as assert from 'assert';
import { typeFilterValidater } from '../src/misc/mime-type-filter';

describe('typeFilterValidater', () => {
it('Allow image/jpeg', () => {
assert.strictEqual(typeFilterValidater.test('image/jpeg'), true);
});

it('Allow image/*', () => {
assert.strictEqual(typeFilterValidater.test('image/*'), true);
});

it('Reject image/a*', () => {
assert.strictEqual(typeFilterValidater.test('image/a*'), false);
});

it('Reject image/a*b*c', () => {
assert.strictEqual(typeFilterValidater.test('image/a*b*c'), false);
});

it('Reject image*/jpeg', () => {
assert.strictEqual(typeFilterValidater.test('image*/jpeg'), false);
});

it('Reject image/*/*', () => {
assert.strictEqual(typeFilterValidater.test('image/*/*'), false);
});

it('Allow video/mp4', () => {
assert.strictEqual(typeFilterValidater.test('video/mp4'), true);
});

it('Allow video/MP1S', () => {
assert.strictEqual(typeFilterValidater.test('video/MP1S'), true);
});

it('Allow image/x-icon', () => {
assert.strictEqual(typeFilterValidater.test('image/x-icon'), true);
});

it('Allow image/svg+xml', () => {
assert.strictEqual(typeFilterValidater.test('image/svg+xml'), true);
});

it('Allow audio/vnd.wave', () => {
assert.strictEqual(typeFilterValidater.test('audio/vnd.wave'), true);
});

it('Reject %', () => {
assert.strictEqual(typeFilterValidater.test('image/%'), false);
});

it('Reject _', () => {
assert.strictEqual(typeFilterValidater.test('image/_'), false);
});
});

0 comments on commit ea2bc3f

Please sign in to comment.