Skip to content

Commit

Permalink
ファイルサイズのハードリミット (misskey-dev#7760)
Browse files Browse the repository at this point in the history
* maxFileSize

* CHANGELOG
  • Loading branch information
mei23 authored Sep 4, 2021
1 parent da20675 commit e21ff91
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .config/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,6 @@ id: 'aid'
#allowedPrivateNetworks: [
# '127.0.0.1/32'
#]

# Upload or download file size limits (bytes)
#maxFileSize: 262144000
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- Proxy使用時には、この制限は適用されません。
Proxy使用時に同等の制限を行いたい場合は、Proxy側で設定を行う必要があります。
- `default.yml`にて`allowedPrivateNetworks`にCIDRを追加することにより、宛先ネットワークを指定してこの制限から除外することが出来ます。
- アップロード, ダウンロード出来るファイルサイズにハードリミットが適用されるようになりました。(約250MB)
- `default.yml`にて`maxFileSize`を変更することにより、制限値を変更することが出来ます。

### Bugfixes
- 管理者が最初にサインアップするページでログインされないのを修正
Expand Down
2 changes: 2 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export type Source = {

allowedPrivateNetworks?: string[];

maxFileSize?: number;

accesslog?: string;

clusterLimit?: number;
Expand Down
15 changes: 15 additions & 0 deletions src/misc/download-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export async function downloadUrl(url: string, path: string) {

const timeout = 30 * 1000;
const operationTimeout = 60 * 1000;
const maxSize = config.maxFileSize || 262144000;

const req = got.stream(url, {
headers: {
Expand All @@ -44,6 +45,20 @@ export async function downloadUrl(url: string, path: string) {
req.destroy();
}
}

const contentLength = res.headers['content-length'];
if (contentLength != null) {
const size = Number(contentLength);
if (size > maxSize) {
logger.warn(`maxSize exceeded (${size} > ${maxSize}) on response`);
req.destroy();
}
}
}).on('downloadProgress', (progress: Got.Progress) => {
if (progress.transferred > maxSize) {
logger.warn(`maxSize exceeded (${progress.transferred} > ${maxSize}) on downloadProgress`);
req.destroy();
}
}).on('error', (e: any) => {
if (e.name === 'HTTPError') {
const statusCode = e.response?.statusCode;
Expand Down
7 changes: 6 additions & 1 deletion src/server/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import discord from './service/discord';
import github from './service/github';
import twitter from './service/twitter';
import { Instances, AccessTokens, Users } from '@/models/index';
import config from '@/config';

// Init app
const app = new Koa();
Expand All @@ -37,7 +38,11 @@ app.use(bodyParser({

// Init multer instance
const upload = multer({
storage: multer.diskStorage({})
storage: multer.diskStorage({}),
limits: {
fileSize: config.maxFileSize || 262144000,
files: 1,
}
});

// Init router
Expand Down

0 comments on commit e21ff91

Please sign in to comment.