Possible Duplicate:
Django (audio) File Validation
I'm building a web app, where users are able to upload media content, including audio files.
I've got a clean method in my AudioFileUploadForm that validates the following:
- That the audio file isn't too big.
- That the audio file has a valid content_type (MIME type).
- That the audio file has a valid extension.
However, I'm worried about security. A user could upload a file with malicious code, and easily pass the above validations. What I want to do next is validate that the audio file is, indeed, an audio file (before it writes to disk).
How should I do this?
class UploadAudioForm(forms.ModelForm):
audio_file = forms.FileField()
def clean_audio_file(self):
file = self.cleaned_data.get('audio_file',False):
if file:
if file._size > 12*1024*1024:
raise ValidationError("Audio file too large ( > 12mb )")
if not file.content_type in ['audio/mpeg','audio/mp4', 'audio/basic', 'audio/x-midi', 'audio/vorbis', 'audio/x-pn-realaudio', 'audio/vnd.rn-realaudio', 'audio/x-pn-realaudio', 'audio/vnd.rn-realaudio', 'audio/wav', 'audio/x-wav']:
raise ValidationError("Sorry, we do not support that audio MIME type. Please try uploading an mp3 file, or other common audio type.")
if not os.path.splitext(file.name)[1] in ['.mp3', '.au', '.midi', '.ogg', '.ra', '.ram', '.wav']:
raise ValidationError("Sorry, your audio file doesn't have a proper extension.")
# Next, I want to read the file and make sure it is
# a valid audio file. How should I do this? Use a library?
# Read a portion of the file? ...?
if not ???.is_audio(file.content):
raise ValidationError("Not a valid audio file.")
return file
else:
raise ValidationError("Couldn't read uploaded file")
EDIT: By "validate that the audio file is, indeed, an audio file", I mean the following:
A file that contains data typical of an audio file. I'm worried that a user could upload files with appropriate headers, and malicious script in the place of audio data. For example... is the mp3 file an mp3 file? Or does it contain something uncharacteristic of an mp3 file?