feat: add backend sanitizing
This commit is contained in:
parent
83fee6293f
commit
27bc790d5d
7 changed files with 82 additions and 1 deletions
|
|
@ -92,6 +92,20 @@ def command_init_backend(**kwargs):
|
|||
config.backend.paste.init()
|
||||
|
||||
|
||||
def command_sanitize_backend(**kwargs):
|
||||
"""sanitize the backend
|
||||
"""
|
||||
|
||||
from httpaste import load_config
|
||||
|
||||
config, _ = load_config(kwargs.get('config'))
|
||||
|
||||
config, _ = load_config(kwargs.get('config'))
|
||||
|
||||
config.backend.user.sanitize()
|
||||
config.backend.paste.sanitize()
|
||||
|
||||
|
||||
def parser():
|
||||
|
||||
p = argparse.ArgumentParser(description='Process some integers.')
|
||||
|
|
@ -121,6 +135,11 @@ def parser():
|
|||
help=command_init_backend.__doc__)
|
||||
p_init_backend.add_argument('--config', '-c', required=True)
|
||||
|
||||
p_sanitize_backend = sp.add_parser(
|
||||
'sanitize-backend',
|
||||
help=command_sanitize_backend.__doc__)
|
||||
p_sanitize_backend.add_argument('--config', '-c', required=True)
|
||||
|
||||
return p
|
||||
|
||||
|
||||
|
|
@ -136,7 +155,8 @@ def main():
|
|||
'cgi': command_cgi,
|
||||
'fcgi': command_fcgi,
|
||||
'default-config': command_default_config,
|
||||
'init-backend': command_init_backend
|
||||
'init-backend': command_init_backend,
|
||||
'sanitize-backend': command_sanitize_backend
|
||||
}[kwargs.pop('command')](**kwargs)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -58,6 +58,13 @@ class User(object):
|
|||
|
||||
return user.init(self.path)
|
||||
|
||||
def sanitize(self):
|
||||
|
||||
if self.path.exists():
|
||||
return user.sanitize(self.path, self.model_class, self.model_schema)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
class Paste(object):
|
||||
"""Filesystem paste model backend
|
||||
|
|
@ -96,3 +103,10 @@ class Paste(object):
|
|||
def init(self):
|
||||
|
||||
return paste.init(self.path)
|
||||
|
||||
def sanitize(self):
|
||||
|
||||
if self.path.exists():
|
||||
return paste.sanitize(self.path, self.model_class, self.model_schema)
|
||||
|
||||
return None
|
||||
|
|
@ -5,6 +5,7 @@ acting as cells.
|
|||
"""
|
||||
from pathlib import Path
|
||||
from ast import literal_eval
|
||||
from time import time
|
||||
|
||||
|
||||
COLUMNS = [
|
||||
|
|
@ -98,6 +99,22 @@ def init(path: Path):
|
|||
return None
|
||||
|
||||
|
||||
def sanitize(path: Path, model_class: type, model_schema: type):
|
||||
|
||||
for row in path.iterdir():
|
||||
|
||||
expiration_cell = row.joinpath('expiration')
|
||||
|
||||
if not expiration_cell.exists():
|
||||
continue
|
||||
|
||||
expiration = literal_eval(expiration_cell.read_text())
|
||||
|
||||
if expiration < int(time()) and expiration > 0:
|
||||
|
||||
delete(model_class(bytes.fromhex(row.name)), path)
|
||||
|
||||
|
||||
def _rm_tree(pth: Path):
|
||||
for child in pth.iterdir():
|
||||
if child.is_file():
|
||||
|
|
|
|||
|
|
@ -82,6 +82,11 @@ def init(path: Path):
|
|||
return None
|
||||
|
||||
|
||||
def sanitize(path: Path, model_class: type, model_schema: type):
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _rm_tree(pth: Path):
|
||||
for child in pth.iterdir():
|
||||
if child.is_file():
|
||||
|
|
|
|||
|
|
@ -45,6 +45,10 @@ class User(object):
|
|||
|
||||
return user.init(self.connection)
|
||||
|
||||
def sanitize(self):
|
||||
|
||||
return user.sanitize(self.connection, self.model_class)
|
||||
|
||||
|
||||
class Paste(object):
|
||||
"""SQLite paste model backend
|
||||
|
|
@ -74,6 +78,10 @@ class Paste(object):
|
|||
|
||||
return paste.init(self.connection)
|
||||
|
||||
def sanitize(self):
|
||||
|
||||
return paste.sanitize(self.connection, self.model_class)
|
||||
|
||||
|
||||
def get_connection(parameters: Parameters):
|
||||
"""get an sqlite connection object
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
"""
|
||||
from os import path
|
||||
from sqlite3 import Connection
|
||||
from time import time
|
||||
|
||||
|
||||
def load(proto: object, connection: Connection, model_class: type):
|
||||
|
|
@ -67,3 +68,14 @@ def init(connection: Connection):
|
|||
cur.execute(fh.read())
|
||||
|
||||
connection.commit()
|
||||
|
||||
|
||||
def sanitize(connection: Connection, model_class: type) -> bool:
|
||||
|
||||
cur = connection.cursor()
|
||||
|
||||
cur.execute('''SELECT pid FROM pastes WHERE expiration < ? AND expiration > 0''', (int(time()),))
|
||||
|
||||
for row in cur.fetchall():
|
||||
|
||||
delete(model_class(row['pid']))
|
||||
|
|
@ -53,3 +53,8 @@ def init(connection: Connection):
|
|||
cur.execute(fh.read())
|
||||
|
||||
connection.commit()
|
||||
|
||||
|
||||
def sanitize(connection: Connection, model_class) -> bool:
|
||||
|
||||
return None
|
||||
Loading…
Add table
Add a link
Reference in a new issue