Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
## 2024-06-28 - Secure Native File Traversal Optimization
**Learning:** Shelling out to `chown -R` via `os.system` incurs significant process spawning overhead and forces redundant directory traversals when modifying multiple permissions. However, directly replacing it with Python's native `os.chown` inside an `os.walk` loop introduces a critical Local Privilege Escalation (LPE) vulnerability, as `os.chown` follows symlinks by default whereas GNU `chown -R` does not.
**Action:** When replacing `os.system("chown -R ...")` with native `os.walk` in Python scripts, always use `os.lchown` on files and directories to ensure symlinks are not maliciously followed, preserving the security guarantees of the original subprocess call while eliminating overhead.
## 2024-07-07 - Bash Regex vs Glob Matching in Tight Loops
**Learning:** Even when avoiding external processes like `cut` and `grep` inside a tight loop, using bash regular expression evaluation (`=~`) still incurs a significant performance penalty compared to native glob matching (`== *pattern*`). Regex compilation inside the loop causes overhead that scales linearly with the loop size.
**Action:** When filtering strings in tight bash loops (e.g., thousands of iterations), prefer native string matching via globs (e.g., `[[ "|$LIST|" == *"|$ITEM|"* ]]`) over regular expressions (`[[ "$ITEM" =~ ^($LIST)$ ]]`) for significant performance improvements.
6 changes: 4 additions & 2 deletions vpn-auto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,22 @@ if [ -n "$US_SERVERS" ]; then
# Optimization: Use native bash parameter expansion and regex to extract state
# and filter servers instead of spawning 'cut' and 'grep' subshells in the loop.
# This avoids thousands of subshell forks and speeds up processing significantly.
# ⚑ Bolt: Further optimized by replacing bash regex matching (=~) with native glob matching.
# Impact: Regex compilation is expensive inside a loop. Glob matching reduces execution time by over 50% for thousands of servers.
for server in $US_SERVERS; do
# Extract state code (e.g., US-WA#10 -> WA)
state="${server#US-}"
state="${state%%#*}"

# Check against exclusions
if [[ "$state" =~ ^($EXCLUDED_STATES)$ ]]; then
if [[ "|$EXCLUDED_STATES|" == *"|$state|"* ]]; then
continue
fi

FILTERED_SERVERS+=("$server")

# Check against favorites
if [[ "$state" =~ ^($FAVORED_STATES)$ ]]; then
if [[ "|$FAVORED_STATES|" == *"|$state|"* ]]; then
FAVORED_SERVERS+=("$server")
fi
done
Expand Down