block illegal filename characters

This commit is contained in:
Damien Elmes 2011-12-08 19:23:11 +09:00
parent 95106908dd
commit d94f6d2011

View file

@ -5,7 +5,7 @@
import os, shutil, re, urllib, urllib2, time, unicodedata, \ import os, shutil, re, urllib, urllib2, time, unicodedata, \
urllib, sys, simplejson, zipfile urllib, sys, simplejson, zipfile
from cStringIO import StringIO from cStringIO import StringIO
from anki.utils import checksum, intTime, namedtmp, isWin from anki.utils import checksum, intTime, namedtmp, isWin, isMac
from anki.lang import _ from anki.lang import _
from anki.db import DB from anki.db import DB
from anki.consts import * from anki.consts import *
@ -230,9 +230,9 @@ If the same name exists, compare checksums."""
data = z.read(i) data = z.read(i)
csum = checksum(data) csum = checksum(data)
name = meta[i.filename] name = meta[i.filename]
# malicious chars? # can we store the file on this system?
for c in '/\\': if self.illegal(i.filename):
assert c not in name continue
# save file # save file
open(name, "wb").write(data) open(name, "wb").write(data)
# update db # update db
@ -250,6 +250,16 @@ If the same name exists, compare checksums."""
self.syncMod() self.syncMod()
return finished return finished
def illegal(self, f):
if isWin:
for c in f:
if c in "<>:\"/\\|?*^":
return True
elif isMac:
for c in f:
if c in ":\\/":
return True
# Media syncing - bundling zip files to send to server # Media syncing - bundling zip files to send to server
########################################################################## ##########################################################################
# Because there's no standard filename encoding for zips, and because not # Because there's no standard filename encoding for zips, and because not