Skip to content

Commit ee90885

Browse files
author
numbpill3d
committed
security(core): harden firestore rules, secure api keys, and fix memory leaks
- Refactor Firebase initialization to use environment variables via import.meta.env - Remove hardcoded API keys and configuration from index.html and load-env.js - Harden firestore.rules: enforce user ownership on write, restrict system collections (badges/missions), and fix rate-limiting check - Fix memory leaks in data.js by storing and clearing onSnapshot listener subscriptions - Restructure assets: move images/ to public/ for proper Vite asset resolution - Clean up technical debt: remove legacy service workers, test files, and redundant config
1 parent 44afb5f commit ee90885

22 files changed

Lines changed: 127 additions & 15405 deletions

CLAUDE.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## What this is
6+
7+
**Urbindex** — a PWA for urban exploration mapping. Users add geotagged locations to a shared Firestore database, visible as real-time map markers. Stack: vanilla JS ES6 modules, Leaflet + CartoDB Dark tiles, Firebase (Auth + Firestore), Vite build, deployed to Firebase Hosting.
8+
9+
## Commands
10+
11+
```bash
12+
npm run dev # dev server at localhost:8080
13+
npm run build # production build → dist/
14+
npm run preview # preview built output
15+
npm run deploy # build + firebase deploy (hosting only)
16+
npm run lint # ESLint on src/**/*.js
17+
npm run test # Playwright e2e tests
18+
```
19+
20+
Single e2e test file:
21+
```bash
22+
npx playwright test tests/e2e/smoke.spec.js
23+
```
24+
25+
Unit/integration tests use Mocha + Chai:
26+
```bash
27+
npx mocha tests/database_operations_test.js
28+
```
29+
30+
## Architecture
31+
32+
### Module composition
33+
34+
`src/app.js` defines `UrbindexApp` as a class, then uses `Object.assign()` to mix in all feature modules onto the prototype:
35+
36+
```js
37+
Object.assign(UrbindexApp.prototype,
38+
utilsMethods, firebaseMethods, mapMethods, uiMethods,
39+
authMethods, locationsMethods, socialMethods, profileMethods,
40+
dataMethods, settingsMethods
41+
);
42+
```
43+
44+
All modules export a plain object of methods. They share `this` at runtime — any method can call `this.showToast()`, `this.map`, `this.db`, `this.currentUser`, etc. There is one global `app` instance exposed on `window`.
45+
46+
**Init order** (`app.init()`): Firebase → Map → UI → Auth → TagSystem → SessionManagement → RateLimiting → `loadData()`
47+
48+
### Real-time data flow
49+
50+
`dataMethods.loadData()` sets up two persistent `onSnapshot` listeners:
51+
52+
1. **`loadLocations()`**`locations` collection filtered by `status == 'active'`, ordered by `createdAt desc`. On every snapshot it clears `markerClusterGroup`, rebuilds `this.markers` Map, and re-adds all markers. This is the source of truth for map pins.
53+
2. **`loadActivity()`** — same collection, limit 10, feeds the activity feed sidebar.
54+
55+
`loadStats()` is a one-time fetch.
56+
57+
### Known dual cluster group conflict
58+
59+
`map.js:initializeMap()` creates `this.markerClusterGroup` (maxClusterRadius: 60) and adds it to the map. `data.js:loadLocations()` then overwrites `this.markerClusterGroup` with a new instance (maxClusterRadius: 50, chunkedLoading, spiderfyOnMaxZoom, etc.) and adds *that* to the map — leaving the first one orphaned on the Leaflet instance. Any fix must consolidate cluster creation to one place.
60+
61+
Additionally, `map.js:updateMapMarkers()` (called from `locations.js:renderFilteredLocations()`) calls `markerClusterGroup.clearLayers()` and repopulates with only the current user's filtered locations, wiping the full dataset from `loadLocations()` until the next snapshot fires.
62+
63+
### Firestore collections
64+
65+
| Collection | Purpose |
66+
|---|---|
67+
| `locations` | Main content — coordinates, category, riskLevel, status, createdBy |
68+
| `users` | Profiles — displayName, lastSeen |
69+
| `location_comments` | Comments keyed by locationId |
70+
| `location_likes` | Likes keyed by userId_locationId |
71+
| `location_visits` | Check-ins |
72+
| `user_notifications` | In-app notifications |
73+
| `user_badges` | Earned achievements |
74+
| `routes`, `groups`, `missions` | Gamification, partially stubbed |
75+
| `direct_messages` | Private messaging |
76+
77+
Security rules: locations and users are public read; all writes are gated on `auth.uid`. Location create/update is validated server-side (coordinate bounds, field presence, name pattern). Rate limiting helper in rules limits profile updates.
78+
79+
### Styles
80+
81+
CSS is split into four files imported in order via `src/main.js`:
82+
`variables.css``base.css``layout.css``components.css`
83+
84+
All color, font, and effect tokens live in `variables.css`. The palette is `--yellow` (#FFD000) primary accent, black backgrounds, monospace everywhere, hard corners (`--radius: 0px`). Legacy `--cz-*` aliases exist for inline JS template strings — don't add new ones, use the canonical vars.
85+
86+
Map-specific Leaflet overrides (popup, tooltip, zoom controls, cluster markers, diamond pin markers) live at the bottom of `components.css`.
87+
88+
### Map markers
89+
90+
Location markers are diamond-shaped divIcons (`.ub-pin`) colored by `riskLevel` via CSS classes (`risk-safe``risk-extreme`). They have a CSS pulse ring animation. Cluster markers are styled black/yellow squares that scale to amber/red at medium/large counts. All marker and Leaflet UI CSS is in `components.css` under the `LEAFLET MAP THEME` section.
91+
92+
Tile layer: CartoDB DarkMatter (`https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png`), with a CSS filter on `.leaflet-tile-pane` for the amber-noir tint.
93+
94+
### PWA / service worker
95+
96+
Vite PWA plugin auto-generates the service worker. Caching strategy: CacheFirst for OSM/CARTO tiles (200 entries, 7-day TTL), NetworkFirst for Firestore. The manifest theme color is `#FFD000`.

FILE_TREE.txt

Lines changed: 0 additions & 164 deletions
This file was deleted.

auth-test.html

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)