|
Última actualización: 2026-12-02
DAY 003 OF 1600 · 2026-08-30 Day 003 — Search that proves itselfA student's private learning log · one concept at a time. Date: 2026-08-30 · Day: 3 of 1600 Log: a student's private learning log · one concept at a time. --- Concept #3 — Problem solving by search: BFS on a state spacePR01 frames AI problems as search : from a starting state, explore a space of states until you reach a goal. Search algorithms split into informed (a heuristic guides which node to expand next) and uninformed (no guidance, just systematic exploration). I studied Breadth-First Search (BFS), an uninformed method that expands the frontier level by level. Because every move costs the same, the first time BFS touches the goal that path is guaranteed to be the shortest. I am at the very start of the program, so this is a first pass. What I tried today: BFS on a 5x5 grid (0 = open, 1 = wall), tracking each node's parent so I can rebuild the shortest path after the goal is found. # bfs_grid.py — uninformed search, first-found path is shortest from collections import deque GRID = [ [0, 0, 0, 0, 1], [1, 1, 0, 1, 0], [0, 0, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0], ] START, GOAL = (0, 0), (4, 4) DIRS = [(-1, 0), (1, 0), (0, -1), (0, 1)] def bfs(start, goal): frontier = deque([start]) # FIFO queue = level by level came_from = {start: None} explored = 0 while frontier: cur = frontier.popleft() explored += 1 if cur == goal: return came_from, explored r, c = cur for dr, dc in DIRS: nxt = (r + dr, c + dc) nr, nc = nxt if not (0 <= nr < 5 and 0 <= nc < 5): continue if GRID[nr][nc] == 1 or nxt in came_from: continue came_from[nxt] = cur frontier.append(nxt) return None, explored def reconstruct(came_from, start, goal): path, cur = [], goal while cur is not None: path.append(cur) cur = came_from[cur] return path[::-1] came_from, explored = bfs(START, GOAL) path = reconstruct(came_from, START, GOAL) print("BFS (uninformed) on a 5x5 grid, 1 = wall") print(f"Start: {START} Goal: {GOAL}") print(f"Nodes explored: {explored}") print(f"Shortest path ({len(path)-1} steps):") for i, (r, c) in enumerate(path): m = "START" if (r, c) == START else ("GOAL" if (r, c) == GOAL else "") print(f" {i}: ({r},{c}) {m}") Real output: BFS (uninformed search) on a 5x5 grid, 1 = wall Start: (0, 0) Goal: (4, 4) Nodes explored: 14 Shortest path (12 steps): 0: (0,0) START 1: (0,1) 2: (0,2) 3: (1,2) 4: (2,2) 5: (2,1) 6: (2,0) 7: (3,0) 8: (4,0) 9: (4,1) 10: (4,2) 11: (4,3) 12: (4,4) GOAL What I understood from it: BFS only needs to "see" each state once (the came_from dict skips re-visits), and exploring 14 nodes out of 25 was enough to find the goal. The key insight is the FIFO queue: it guarantees I check everything at distance k before anything at distance k+1, which is exactly why the first hit on the goal is the shortest path. The trade-off — it can eat memory on big spaces — is why informed search (and heuristics) exists. That is next on my list. --- Security #3 — SQL injection (CWE-89)I studied SQL injection: when user input is stitched straight into a SQL string, a crafted value can change the query's logic. A classic demo is a login bypass with a payload that makes the WHERE clause always true. I ran this only against a throwaway in-memory SQLite database in /tmp — never against a real system. The pattern I studied: # UNSAFE — user input concatenated into the SQL string q = f"SELECT username FROM users WHERE username='{username}' AND pass='{password}'" A username of ' OR '1'='1' -- turns that into: WHERE username='' OR '1'='1' --' AND pass='...' The -- comments out the rest, and '1'='1' is always true, so the query returns the first user — the attacker "logs in" as someone else. What I understood about preventing it: never build SQL with concatenation. Use parameterized queries — the driver escapes the values automatically, so the input stays data, not code. # SAFE — parameters, values are escaped by the driver q = "SELECT username FROM users WHERE username=? AND pass=?" DB.execute(q, (username, password)) Sandbox check (local only, in-memory DB, no real target): SQL injection (CWE-89) demo — local sandbox, in-memory DB Attacker username payload: "' OR '1'='1' --" UNSAFE login (concatenated SQL): logged in as -> 'alice' SAFE login (parameterized): logged in as -> None Expected: unsafe returns 'alice' (bypass), safe returns None. What I understood: the concatenated version let the payload escape its string, turn AND pass='...' into a comment and force the WHERE to be true (sandbox logged in as alice ), while the parameterized version treated the whole payload as a literal username and returned no row. One small detail cost me time: the payload needs balanced quotes ( '1'='1' not '1'='1 ) or the string literal doesn't close and the trick silently fails — SQLi is exacting. Rule I keep: queries are parameterized, always. --- Day 3 notesConcept #3: BFS ran, found the 12-step shortest path, output above. ✔ Security #3: SQLi bypass + parameterized fix understood, sandbox only. ✔ 1597 days left. One at a time. Written by my own daily machine, checked by me. · Day 003 of 1600 · Matías Rathgeb [ inicio ] [ proyectos ] [ posts ] [ daylog ] Página estática. Sin cookies, sin javascript, sin fuentes externas. Contacto: ver inicio. |