Skip to content

Commit

Permalink
fix(files): nsfw scan memory limit
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmcg committed Mar 31, 2022
1 parent 462c254 commit aa8398f
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 15 deletions.
4 changes: 0 additions & 4 deletions components/views/files/controls/Controls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ export default Vue.extend({
sameNameResults.map(async (file: File) => {
// convert heic to jpg for scan. return original heic if sfw
if (await isHeic(file)) {
// prevent crash in case of larger than 2GB heic files. could possibly be broken up into multiple buffers
if (file.size >= this.$Config.arrayBufferLimit) {
return { file, nsfw: false }
}
const buffer = new Uint8Array(await file.arrayBuffer())
const outputBuffer = await convert({
buffer,
Expand Down
2 changes: 1 addition & 1 deletion components/views/files/upload/Upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export default Vue.extend({
)
for (const uploadFile of filesToUpload) {
if (uploadFile.file.size <= Config.nsfwByteLimit) {
if (uploadFile.file.size <= Config.nsfwPictureLimit) {
uploadFile.nsfw.checking = true
try {
uploadFile.nsfw.status = await this.$Security.isNSFW(
Expand Down
5 changes: 2 additions & 3 deletions components/views/user/create/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ export default Vue.extend({
if (!files?.length) return
// stop upload if picture is too large for nsfw scan.
// Leaving this in place since nsfw profile pictures would be bad
if (files[0].size > this.$Config.nsfwByteLimit) {
// stop upload if picture is too large for nsfw scan
if (files[0].size > this.$Config.nsfwPictureLimit) {
this.error = this.$t('errors.accounts.file_too_large') as string
return
}
Expand Down
4 changes: 2 additions & 2 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ export const Config = {
routingMiddleware: {
prerequisitesCheckBypass: ['auth', 'setup'],
},
nsfwByteLimit: 1048576 * 8, // 8MB - arbitrary image limit for nsfw scan - binary
nsfwPictureLimit: 1048576 * 8, // 8MB - arbitrary image limit for nsfw scan - binary
personalFilesLimit: 1000000000 * 4, // 4GB - free tier limit - decimal because stoarge systems typically are
arrayBufferLimit: 1073741824 * 2, // 2GB - array buffers larger than this crash - binary
nsfwVideoLimit: 1073741824 * 2, // 2GB - array buffers larger than this crash - binary
regex: {
// identify if a file type is embeddable image
image: '^.*.(apng|avif|gif|jpg|jpeg|jfif|pjpeg|pjp|png|svg|webp)$',
Expand Down
4 changes: 0 additions & 4 deletions libraries/Files/TextileFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ export class TextileFileSystem extends FilSystem {
type: FILE_TYPE,
): Promise<string | undefined> {
if (await isHeic(file)) {
// prevent crash in case of larger than 2GB heic files. could possibly be broken up into multiple buffers
if (file.size >= Config.arrayBufferLimit) {
return
}
const buffer = new Uint8Array(await file.arrayBuffer())
const outputBuffer = await convert({
buffer,
Expand Down
6 changes: 5 additions & 1 deletion utilities/NSFW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default async function isNSFW(file: File): Promise<boolean> {

// if embeddable video
if (vidTypes.some((type) => file.type.includes(type))) {
// if too big to scan, allow upload
if (file.size > Config.nsfwVideoLimit) {
return false
}
const vid = document.createElement('video')
vid.src = URL.createObjectURL(file)
await (async () => {
Expand Down Expand Up @@ -71,7 +75,7 @@ export default async function isNSFW(file: File): Promise<boolean> {
}
// if image is somewhat large, scale down so we don't approach RAM limits
img.src = URL.createObjectURL(
file.size > Config.nsfwByteLimit
file.size > Config.nsfwPictureLimit
? await skaler(file, { width: 400 })
: file,
)
Expand Down

0 comments on commit aa8398f

Please sign in to comment.