Files
sisvietnamvn_01/sisvietnamvn_main/add_csrf.py
T
2026-07-01 15:40:46 +07:00

36 lines
1.3 KiB
Python

import os
import re
directory = '/home/x79/Documents/sisvietnamvn_01/sisvietnamvn_main/src/main/resources/templates/manage'
layout = '/home/x79/Documents/sisvietnamvn_01/sisvietnamvn_main/src/main/resources/templates/fragments/manage-layout.html'
csrf_token = '\n <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />'
def process_file(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Check if csrf token is already present
if '_csrf.parameterName' in content:
print(f"Skipping {filepath} - already has CSRF token")
return
# Regex to find <form ... method="post" ...>
# It finds the end of the <form ...> tag that has method="post"
pattern = r'(<form[^>]*?method=["\']post["\'][^>]*?>)'
# We want to replace the match with the match + csrf_token
new_content = re.sub(pattern, r'\1' + csrf_token, content, flags=re.IGNORECASE)
if new_content != content:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Updated {filepath}")
for root, _, files in os.walk(directory):
for file in files:
if file.endswith('.html'):
process_file(os.path.join(root, file))
process_file(layout)