31 INSERT into waterquality_data(uniqueid, plotid, valid,
32 sample_type, varname, value) VALUES (%s, %s, %s, %s, %s, %s)
33 """
34 for i, line in enumerate(open(filename)):35 if i == 0:
36 continue
37 (
31 INSERT into tileflow_data(uniqueid, plotid, valid,
32 discharge_mm, discharge_mm_qc) VALUES (%s, %s, %s, %s, %s)
33 """
34 with open(filename, encoding="utf-8") as fh:35 for i, line in enumerate(fh):
36 if i == 0:
37 continue
Python's open()
function can take in a relative or absolute path and read its file contents.
If a user is provided direct access to the path that is opened, it can have serious security risks.
def read_file(path):
with open(os.path.join('some/path', path)) as f:
f.read()
# Someone can exploit `read_file` and see your secrets this way:
read_file('../../../secrets.txt')
Either use a static path:
def read_file(path):
with open('some/path/to/file.txt') as f:
f.read()
Or, do some kind of validation to make sure you're not allowing arbitrary file access:
def read_file(filename):
if filename not in ('x.txt', 'y.txt'):
return 'Invalid filename'
with open(os.path.join('some/path', path)) as f:
f.read()