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

HEICファイルのアップロードを拒否する #4760

Merged
merged 1 commit into from
Dec 6, 2023
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/client/app/common/views/components/uploader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,22 @@ export default Vue.extend({
xhr.onload = (e: any) => {
if (xhr.status !== 200) {
this.uploads = (this.uploads as any[]).filter(x => x.id !== id);

let errMsg = `${xhr.status} ${xhr.statusText}`;
if (xhr.status === 413) {
errMsg = 'File too large';
} else {
try {
const errObj = JSON.parse(xhr.responseText);
if (errObj.error?.message) errMsg = errObj.error.message;
} catch (err) {
console.error(err);
}
}

this.$root.dialog({
type: 'error',
text: xhr.status === 413 ? `File to large` : `${xhr.status} ${xhr.statusText}`
text: errMsg
});
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/misc/get-file-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const FILE_TYPE_DETECTS = [
'audio/aac',
'audio/x-flac',
'audio/vnd.wave',

'image/heif', 'image/heif-sequence', 'image/heic', 'image/heic-sequence',
];

export const FILE_TYPE_BROWSERSAFE = [
Expand Down
13 changes: 11 additions & 2 deletions src/server/api/endpoints/drive/files/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ms from 'ms';
import $ from 'cafy';
import ID, { transform } from '../../../../../misc/cafy-id';
import { validateFileName, pack } from '../../../../../models/drive-file';
import { addFile } from '../../../../../services/drive/add-file';
import { FileTypeError, addFile } from '../../../../../services/drive/add-file';
import define from '../../../define';
import { apiLogger } from '../../../logger';
import { ApiError } from '../../../error';
Expand Down Expand Up @@ -71,7 +71,13 @@ export const meta = {
message: 'Invalid file name.',
code: 'INVALID_FILE_NAME',
id: 'f449b209-0c60-4e51-84d5-29486263bfd4'
}
},

unsupportedFileTypeHeic: {
message: 'Unsupported file type. HEIC',
code: 'UNSUPPORTED_FILE_TYPE_HEIC',
id: 'c5384862-bc35-4d0f-9493-ba45ec96115d'
},
}
};

Expand All @@ -95,6 +101,9 @@ export default define(meta, async (ps, user, app, file, cleanup) => {
const driveFile = await addFile({ user, path: file.path, name, folderId: ps.folderId, force: ps.force, sensitive: ps.isSensitive });
return pack(driveFile, { detail: true, self: true });
} catch (e) {
if (e instanceof FileTypeError) {
if (e.type === 'denyHeic') throw new ApiError(meta.errors.unsupportedFileTypeHeic);
}
apiLogger.error(e);
throw new ApiError();
} finally {
Expand Down
16 changes: 16 additions & 0 deletions src/services/drive/add-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ import { InternalStorage } from './internal-storage';

const logger = driveLogger.createSubLogger('register', 'yellow');

type FileTypeErrorType = 'denyHeic';

export class FileTypeError extends Error {
public type?: FileTypeErrorType;
constructor(type?: FileTypeErrorType) {
super('file type error');
this.name = 'FileTypeError';
this.type = type;
}
}


/***
* Save file
* @param path Path for original
Expand All @@ -41,6 +53,10 @@ async function save(path: string, name: string, info: FileInfo, metadata: IMetad
// thunbnail, webpublic を必要なら生成
let animation = info.type.mime === 'image/apng' ? 'yes' : info.type.mime === 'image/png' ? 'no' : undefined;

if (!metadata.uri && ['image/heif', 'image/heif-sequence', 'image/heic', 'image/heic-sequence'].includes(info.type.mime)) {
throw new FileTypeError('denyHeic');
}

const alts = await generateAlts(path, info.type.mime, !metadata.uri).catch(err => {
if (err === 'ANIMATED') {
animation = 'yes';
Expand Down