-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new file validation options and improve security validations pu…
…blish 2.1.0v
- Loading branch information
1 parent
1d32b0a
commit 995e236
Showing
7 changed files
with
252 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,37 @@ | ||
export default function validateMp4(fileBuffer: Buffer): boolean { | ||
const isMp4: boolean = | ||
fileBuffer[0] === 0x00 && | ||
fileBuffer[1] === 0x00 && | ||
fileBuffer[2] === 0x00 && | ||
fileBuffer[3] === 0x20 && | ||
fileBuffer[4] === 0x66 && | ||
fileBuffer[5] === 0x74 && | ||
fileBuffer[6] === 0x79 && | ||
fileBuffer[7] === 0x70 && | ||
fileBuffer[8] === 0x6d && | ||
fileBuffer[9] === 0x70 && | ||
fileBuffer[10] === 0x34 && | ||
fileBuffer[11] === 0x32; | ||
(fileBuffer[0] === 0x00 && | ||
fileBuffer[1] === 0x00 && | ||
fileBuffer[2] === 0x00 && | ||
fileBuffer[3] === 0x20 && | ||
fileBuffer[4] === 0x66 && | ||
fileBuffer[5] === 0x74 && | ||
fileBuffer[6] === 0x79 && | ||
fileBuffer[7] === 0x70 && | ||
fileBuffer[8] === 0x6d && | ||
fileBuffer[9] === 0x70 && | ||
fileBuffer[10] === 0x34 && | ||
fileBuffer[11] === 0x32) || | ||
(fileBuffer[0] === 0x00 && | ||
fileBuffer[1] === 0x00 && | ||
fileBuffer[2] === 0x00 && | ||
fileBuffer[3] === 0x18 && | ||
fileBuffer[4] === 0x66 && | ||
fileBuffer[5] === 0x74 && | ||
fileBuffer[6] === 0x79 && | ||
fileBuffer[7] === 0x70) || | ||
(fileBuffer[0] === 0x00 && | ||
fileBuffer[1] === 0x00 && | ||
fileBuffer[2] === 0x00 && | ||
fileBuffer[3] === 0x1c && | ||
fileBuffer[4] === 0x66 && | ||
fileBuffer[5] === 0x74 && | ||
fileBuffer[6] === 0x79 && | ||
fileBuffer[7] === 0x70 && | ||
fileBuffer[8] === 0x69 && | ||
fileBuffer[9] === 0x73 && | ||
fileBuffer[10] === 0x6f && | ||
fileBuffer[11] === 0x6d); | ||
|
||
return isMp4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,81 @@ | ||
import isValidAudio from '../../src/isValidAudio'; | ||
import path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
describe('isValidAudio', () => { | ||
it('should return true for valid MP3 file', () => { | ||
const fileBuffer = Buffer.from([0x49, 0x44, 0x33]); | ||
expect(isValidAudio(fileBuffer)).toBe(true); | ||
let fileBuffer1: Buffer, fileBuffer2: Buffer, fileBuffer3: Buffer, fileBuffer4: Buffer; | ||
|
||
beforeAll(() => { | ||
fileBuffer1 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidAudio', 'invalid', 'invalid.mp3')); // Invalid MP3 image | ||
fileBuffer2 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidAudio', 'invalid', 'invalid.wav')); // Invalid WAV image | ||
fileBuffer3 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidAudio', 'valid', 'valid.mp3')); // valid MP3 image | ||
fileBuffer4 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidAudio', 'valid', 'valid.wav')); // valid WAV image | ||
}); | ||
|
||
it('should return false for an empty buffer', () => { | ||
const fileBuffer = Buffer.from([]); | ||
const result = isValidAudio(fileBuffer); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false for an invalid MP3 audio', () => { | ||
const result = isValidAudio(fileBuffer1); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false for an invalid WAV audio', () => { | ||
const result = isValidAudio(fileBuffer2); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true for a valid MP3 audio', () => { | ||
const result = isValidAudio(fileBuffer3); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true for a valid WAV audio', () => { | ||
const result = isValidAudio(fileBuffer4); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when excluding WAV files', () => { | ||
const result = isValidAudio(fileBuffer4, { exclude: ['wav'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false when excluding MP3 files', () => { | ||
const result = isValidAudio(fileBuffer3, { exclude: ['mp3'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true when excluding WAV files', () => { | ||
const result = isValidAudio(fileBuffer3, { exclude: ['wav'] }); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true when excluding MP3 files', () => { | ||
const result = isValidAudio(fileBuffer4, { exclude: ['mp3'] }); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when excluding WAV files and MP3 files', () => { | ||
const result = isValidAudio(fileBuffer4, { exclude: ['wav', 'mp3'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false when excluding WAV files and MP3 files', () => { | ||
const result = isValidAudio(fileBuffer3, { exclude: ['wav', 'mp3'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true for valid WAV file', () => { | ||
const fileBuffer = Buffer.from([0x52, 0x49, 0x46, 0x46]); | ||
expect(isValidAudio(fileBuffer)).toBe(true); | ||
it('should return false when excluding WAV files and MP3 files', () => { | ||
const result = isValidAudio(fileBuffer2, { exclude: ['wav', 'mp3'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false for invalid file', () => { | ||
const fileBuffer = Buffer.from([0x00, 0x00, 0x00, 0x00]); | ||
expect(isValidAudio(fileBuffer)).toBe(false); | ||
it('should return false when excluding WAV files and MP3 files', () => { | ||
const result = isValidAudio(fileBuffer1, { exclude: ['wav', 'mp3'] }); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
export default isValidAudio; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,106 @@ | ||
import isValidVideo from '../../src/isValidVideo'; | ||
import path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
describe('isValidVideo', () => { | ||
it('should return true for valid MP4 file', () => { | ||
const fileBuffer = Buffer.from([ | ||
0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, 0x6d, 0x70, 0x34, 0x32 | ||
]); | ||
expect(isValidVideo(fileBuffer)).toBe(true); | ||
let fileBuffer1: Buffer, fileBuffer2: Buffer, fileBuffer3: Buffer, fileBuffer4: Buffer, fileBuffer5: Buffer, fileBuffer6: Buffer; | ||
|
||
beforeAll(() => { | ||
fileBuffer1 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'invalid', 'invalid.mkv')); // Invalid MKV image | ||
fileBuffer2 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'invalid', 'invalid.mp4')); // Invalid MP4 image | ||
fileBuffer3 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'invalid', 'invalid.mov')); // Invalid MOV image | ||
fileBuffer4 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'valid', 'valid.mkv')); // valid MKV image | ||
fileBuffer5 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'valid', 'valid.mp4')); // valid MP4 image | ||
fileBuffer6 = fs.readFileSync(path.join(__dirname, '..', 'assets', 'isValidVideo', 'valid', 'valid.mov')); // valid MOV image | ||
}); | ||
|
||
it('should return false for an empty buffer', () => { | ||
const fileBuffer = Buffer.from([]); | ||
const result = isValidVideo(fileBuffer); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false for an invalid MKV video', () => { | ||
const result = isValidVideo(fileBuffer1); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false for an invalid MP4 video', () => { | ||
const result = isValidVideo(fileBuffer2); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false for an invalid MOV video', () => { | ||
const result = isValidVideo(fileBuffer3); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true for a valid MKV video', () => { | ||
const result = isValidVideo(fileBuffer4); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true for a valid MP4 video', () => { | ||
const result = isValidVideo(fileBuffer5); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true for a valid MOV video', () => { | ||
const result = isValidVideo(fileBuffer6); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when excluding MP4 files', () => { | ||
const result = isValidVideo(fileBuffer5, { exclude: ['mp4'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false when excluding MOV files', () => { | ||
const result = isValidVideo(fileBuffer6, { exclude: ['mov'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false when excluding MKV files', () => { | ||
const result = isValidVideo(fileBuffer4, { exclude: ['mkv'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true when excluding MP4 files', () => { | ||
const result = isValidVideo(fileBuffer6, { exclude: ['mp4'] }); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true when excluding MOV files', () => { | ||
const result = isValidVideo(fileBuffer4, { exclude: ['mov'] }); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true when excluding MKV files', () => { | ||
const result = isValidVideo(fileBuffer5, { exclude: ['mkv'] }); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when excluding MP4 files and MOV files', () => { | ||
const result = isValidVideo(fileBuffer5, { exclude: ['mp4', 'mov'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true for valid MOV file', () => { | ||
const fileBuffer = Buffer.from([ | ||
0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20, 0x20 | ||
]); | ||
expect(isValidVideo(fileBuffer)).toBe(true); | ||
it('should return false when excluding MKV files and MP4 files', () => { | ||
const result = isValidVideo(fileBuffer5, { exclude: ['mkv', 'mp4'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true for valid MKV file', () => { | ||
const fileBuffer = Buffer.from([ | ||
0x1a, 0x45, 0xdf, 0xa3 | ||
]); | ||
expect(isValidVideo(fileBuffer)).toBe(true); | ||
it('should return false when excluding MKV files, MP4 files, and MOV files', () => { | ||
const result = isValidVideo(fileBuffer5, { exclude: ['mkv', 'mp4', 'mov'] }); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false for invalid file', () => { | ||
const fileBuffer = Buffer.from([ | ||
0x00, 0x00, 0x00, 0x00 | ||
]); | ||
expect(isValidVideo(fileBuffer)).toBe(false); | ||
it('should return false when excluding all video files', () => { | ||
const result1 = isValidVideo(fileBuffer4, { exclude: ['mkv', 'mp4', 'mov'] }); | ||
const result2 = isValidVideo(fileBuffer5, { exclude: ['mkv', 'mp4', 'mov'] }); | ||
const result3 = isValidVideo(fileBuffer6, { exclude: ['mkv', 'mp4', 'mov'] }); | ||
expect(result3).toBe(false); | ||
expect(result2).toBe(false); | ||
expect(result1).toBe(false); | ||
}); | ||
}); |