Skip to content

Commit d08d662

Browse files
committed
chore: add standalone OWASP cheat sheet refresh script
1 parent 4485936 commit d08d662

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

scripts/update-cheatsheets.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
5+
DB_PATH="${1:-$ROOT_DIR/standards_cache.sqlite}"
6+
VENV_DIR="$ROOT_DIR/venv"
7+
8+
if [[ ! -d "$VENV_DIR" ]]; then
9+
python3 -m venv "$VENV_DIR"
10+
fi
11+
12+
source "$VENV_DIR/bin/activate"
13+
14+
if ! python -c "import flask" >/dev/null 2>&1; then
15+
pip install -r "$ROOT_DIR/requirements.txt"
16+
fi
17+
18+
if [[ ! -f "$DB_PATH" ]]; then
19+
echo "Database file does not exist: $DB_PATH" >&2
20+
exit 1
21+
fi
22+
23+
BACKUP_FILE="${DB_PATH}.$(date +%Y%m%d%H%M%S).bak"
24+
cp "$DB_PATH" "$BACKUP_FILE"
25+
if [[ ! -f "$BACKUP_FILE" ]]; then
26+
echo "Failed to create backup at $BACKUP_FILE" >&2
27+
exit 1
28+
fi
29+
echo "Created backup at $BACKUP_FILE"
30+
31+
CRE_NO_CALCULATE_GAP_ANALYSIS=1 \
32+
CRE_NO_GEN_EMBEDDINGS=1 \
33+
python "$ROOT_DIR/cre.py" --cheatsheets_in --cache_file "$DB_PATH"
34+
35+
python - "$DB_PATH" <<'PY'
36+
import os
37+
import sqlite3
38+
import sys
39+
40+
db_path = sys.argv[1]
41+
conn = sqlite3.connect(db_path)
42+
cur = conn.cursor()
43+
44+
github_prefix = "https://github.com/OWASP/CheatSheetSeries/tree/master/cheatsheets/"
45+
official_prefix = "https://cheatsheetseries.owasp.org/cheatsheets/"
46+
47+
rows = cur.execute(
48+
"""
49+
select id, link
50+
from node
51+
where name = 'OWASP Cheat Sheets'
52+
and link like ?
53+
""",
54+
(f"{github_prefix}%",),
55+
).fetchall()
56+
57+
for node_id, link in rows:
58+
filename = os.path.basename(link)
59+
html_name = os.path.splitext(filename)[0] + ".html"
60+
cur.execute(
61+
"update node set link = ? where id = ?",
62+
(f"{official_prefix}{html_name}", node_id),
63+
)
64+
65+
conn.commit()
66+
conn.close()
67+
print(f"Normalized {len(rows)} OWASP Cheat Sheet links")
68+
PY

0 commit comments

Comments
 (0)