forked from freedomofpress/securedrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.py
43 lines (33 loc) · 1.22 KB
/
store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
import os
import re
import web
import config
# taken from store.urls mapping
VALIDATE_FILENAME = re.compile("^(reply-)?[0-9]+\.[0-9]+(?:_msg|_doc|)\.gpg$").match
def verify(p):
if not os.path.isabs(config.STORE_DIR):
raise Exception("config.STORE_DIR(%s) is not absolute" % (
config.STORE_DIR, ))
if os.path.commonprefix([config.STORE_DIR, p]) != config.STORE_DIR:
raise Exception("Invalid directory %s" % (p, ))
filename = os.path.basename(p)
ext = os.path.splitext(filename)[-1]
if not ext:
# if there's no extension, we're at a subdirectory
return
elif ext != '.gpg':
# if there's an extension, verify it's a GPG
raise Exception("Invalid file extension %s" % (ext, ))
if not VALIDATE_FILENAME(filename):
raise Exception("Invalid filename %s" % (filename, ))
return
def path(*s):
joined = os.path.join(os.path.abspath(config.STORE_DIR), *s)
absolute = os.path.abspath(joined)
verify(absolute)
return absolute
def log(msg):
file(path('NOTES'), 'a').write(msg)
def cleanname(fn):
return web.rstrips(web.rstrips(web.lstrips(web.rstrips(fn, '.gpg'), 'reply-'), '_doc'), '_msg')