update
This commit is contained in:
+57
-25
@@ -16,6 +16,29 @@ logger.addHandler(console_log)
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
|
||||
# Helper functions
|
||||
def sanitize_input(input_string):
|
||||
logger.info("Sanitizing input...")
|
||||
# Sanitize input to allow only certain characters
|
||||
if not isinstance(input_string, str):
|
||||
logger.error("Input is not a string.")
|
||||
return None
|
||||
sanitized = ''.join(c for c in input_string if c in ALLOWED_CHARS)
|
||||
logger.info("Sanitized input")
|
||||
return sanitized
|
||||
|
||||
|
||||
def hash_password(password):
|
||||
logger.info("Hashing password...")
|
||||
# Hash the password using SHA-256
|
||||
if not isinstance(password, str):
|
||||
logger.error("Password is not a string.")
|
||||
return None
|
||||
hashed = hashlib.sha256(password.encode()).hexdigest()
|
||||
logger.info("Hashed password")
|
||||
return hashed
|
||||
|
||||
|
||||
# Initialize Flask app
|
||||
logger.info("Initializing Flask app...")
|
||||
app = Flask(__name__, template_folder=os.getenv('TEMPLATE_FOLDER', 'html'), static_folder=os.getenv('STATIC_FOLDER', 'static'))
|
||||
@@ -33,7 +56,11 @@ if db.get_user('SYSTEM') is None:
|
||||
logger.info("Running first time setup...")
|
||||
|
||||
logger.info("Creating SYSTEM user...")
|
||||
db.create_user('SYSTEM', 'SYSTEM')
|
||||
password = os.getenv('SYSTEM_PASSWORD', None)
|
||||
if password is None:
|
||||
password = os.urandom(16).hex()
|
||||
logger.info("Generated password for SYSTEM user: %s", password)
|
||||
db.create_user('SYSTEM', hash_password(password))
|
||||
SYSTEMUID = db.get_user('SYSTEM')[0]
|
||||
logger.info("SYSTEM user created with UID: %s", SYSTEMUID)
|
||||
|
||||
@@ -61,30 +88,6 @@ else:
|
||||
logger.info("Database initialized.")
|
||||
|
||||
|
||||
# Helper functions
|
||||
|
||||
def sanitize_input(input_string):
|
||||
logger.info("Sanitizing input...")
|
||||
# Sanitize input to allow only certain characters
|
||||
if not isinstance(input_string, str):
|
||||
logger.error("Input is not a string.")
|
||||
return None
|
||||
sanitized = ''.join(c for c in input_string if c in ALLOWED_CHARS)
|
||||
logger.info("Sanitized input")
|
||||
return sanitized
|
||||
|
||||
|
||||
def hash_password(password):
|
||||
logger.info("Hashing password...")
|
||||
# Hash the password using SHA-256
|
||||
if not isinstance(password, str):
|
||||
logger.error("Password is not a string.")
|
||||
return None
|
||||
hashed = hashlib.sha256(password.encode()).hexdigest()
|
||||
logger.info("Hashed password")
|
||||
return hashed
|
||||
|
||||
|
||||
# Define routes
|
||||
@app.route('/')
|
||||
def index():
|
||||
@@ -337,6 +340,35 @@ def register():
|
||||
return render_template('register.html')
|
||||
|
||||
|
||||
@app.route('/delete/post/<int:post_id>', methods=['GET'])
|
||||
def delete_post(post_id):
|
||||
logger.info("Deleting post ID: %s", post_id)
|
||||
token = session.get('session')
|
||||
if not token:
|
||||
logger.error("Session token is missing.")
|
||||
return "Session expired. Please log in again.", 403
|
||||
|
||||
user = db.get_session(token)
|
||||
if user is None:
|
||||
logger.error("Session not found or expired.")
|
||||
return "Session expired. Please log in again.", 403
|
||||
|
||||
user_id = user[0]
|
||||
post = db.get_post(post_id)
|
||||
if post is None:
|
||||
logger.error("Post not found: %s", post_id)
|
||||
return "Post not found", 404
|
||||
|
||||
if post[1] != user_id and user_id != SYSTEMUID:
|
||||
logger.error("User %s is not allowed to delete this post.", user_id)
|
||||
return "You are not allowed to delete this post.", 403
|
||||
|
||||
db.delete_post(post_id)
|
||||
logger.info("Post ID %s deleted successfully.", post_id)
|
||||
|
||||
return redirect('/posts')
|
||||
|
||||
|
||||
# Main function
|
||||
if __name__ == '__main__':
|
||||
logger.info("Starting Flask app...")
|
||||
|
||||
Reference in New Issue
Block a user