Skip to content
Merged
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
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ the security desk, and owners collect them with a one-time code.

**Live app:** https://classfind-prod.eba-ttyqcasp.ap-south-1.elasticbeanstalk.com/

<p align="center">
<img src="docs/campus_map.svg" alt="Map of the BIT campus: buildings, parking, gates and the paths between them, with the security desk marked" width="60%">
</p>
<p align="center"><em>The campus map ClassFind is built on: 29 places, the paths between them, and the security desk at the front of the Main Block.</em></p>

<p align="center">
<img src="docs/screenshots/classfind_home.png" alt="The home page: a map of the BIT campus beside the search and the latest reports" width="80%">
</p>
Expand Down Expand Up @@ -58,10 +63,13 @@ the security desk, and owners collect them with a one-time code.
- **Photos** go to S3 through presigned URLs. Amazon Rekognition labels them
("Bottle", "Backpack") and the labels count in matching and Retrace. A Lambda
function makes thumbnails for the home page.
- **Security desk.** Every found item goes to the desk. A student's found report
shows as awaiting drop-off until staff mark it received. Each item keeps a
chain of custody: reported, received, claim approved, released, with who did
each and when. Valuables left unclaimed for 72 hours move to the admin office.
- **Security desk.** Every found item goes to the desk, and both sides confirm the
hand-over. The finder can mark it "I've handed it in", which puts it at the
top of the Desk page for staff to confirm or mark not received. If the desk
logs it first, it is held at once and the finder is asked "Was that you?";
"That wasn't me" flags it for the admin. Each item keeps a chain of custody:
reported, handed in, received, claim approved, released, with who did each
and when. Valuables left unclaimed for 72 hours move to the admin office.
- **Claim** with proof only the owner would know; electronics, wallets and keys
need a longer proof such as a serial number. Desk staff, not the finder,
approve or reject claims.
Expand All @@ -80,6 +88,10 @@ the security desk, and owners collect them with a one-time code.
accepts from their own account within 7 days.
- **AI check.** One button on the admin page tries every configured model and
shows "working" or the provider's exact error, never the key.
- **Limits that suit a campus.** A whole class on campus Wi-Fi shares one
address, so sign-up allows 30 accounts an hour per address and sign-in 60
attempts a minute, while guessing one account's password is stopped after 10
tries a minute.

## How sentences and voice are understood

Expand All @@ -101,7 +113,8 @@ a current chat model and remembers it.

## How places are matched

`static/campus.json` holds the places, their outlines, about 300 other names,
`tools/build_campus.py` writes `static/campus.json` and the README map from one
table. `static/campus.json` holds the places, their outlines, about 300 other names,
the paths, and a graph of the paths with the door where each building meets
one. `campus.py` turns text into a place: longest name first, a trailing "lab"
or "block" belongs to the name before it, words like "near" or "outside" mark
Expand Down Expand Up @@ -204,8 +217,9 @@ check below.
.venv/Scripts/python.exe -m unittest discover -s tests -v
```

116 tests, with no network or AWS account needed. Besides accounts, reporting,
the desk flow from drop-off to collection code, admin, CSRF, security headers
122 tests, with no network or AWS account needed. Besides accounts, reporting,
the desk flow from drop-off and two-sided hand-over to collection code, admin,
CSRF, rate limits, security headers
and uploads, they cover place names (aliases, misspellings, Kannada and Hindi,
places that are ambiguous on purpose), the path graph being fully connected,
Retrace, voice in each mode, every model fallback (busy, retired, timed out),
Expand Down Expand Up @@ -245,7 +259,7 @@ ClassFind/
├── tools/ build_campus.py, smoke.py, mutate.py
├── tests/ test_app.py, test_campus.py, test_quality.py,
│ test_thumbnail_lambda.py
├── docs/ AWS setup and screenshots
├── docs/ AWS setup, campus map image, screenshots
└── .github/workflows/ CI on SQLite and PostgreSQL
```

Expand Down
33 changes: 21 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ def protect_csrf():
# memory, which is enough because Gunicorn runs a single process; a restart
# simply starts the counts again.
RATE_LIMITS = {
"login": (10, 60),
"register": (5, 60 * 60),
# A whole class on campus Wi-Fi shares one address, so these are per
# address but generous; guessing one account's password is capped below.
"login": (60, 60),
"register": (30, 60 * 60),
"report": (20, 60 * 60),
"submit_claim": (20, 60 * 60),
# Collection codes are six digits, so the desk screen caps guesses.
Expand All @@ -177,6 +179,9 @@ def protect_csrf():
# Each clip is sent to Gemini.
"voice": (40, 60 * 60),
}
# Limits per address and per account, for forms where one person attacking one
# account matters more than how many people share an address.
ACCOUNT_RATE_LIMITS = {"login": (10, 60)}
app.config["RATE_LIMITS_ENABLED"] = True
_rate_hits = {}
_rate_lock = threading.Lock()
Expand All @@ -193,20 +198,24 @@ def rate_limit():
limit = RATE_LIMITS.get(request.endpoint)
if not limit:
return
count, window = limit
key = (request.endpoint, request.remote_addr)
checks = [((request.endpoint, request.remote_addr), limit)]
if request.endpoint in ACCOUNT_RATE_LIMITS:
account = request.form.get("email", "").strip().lower()
checks.append(((request.endpoint, request.remote_addr, account), ACCOUNT_RATE_LIMITS[request.endpoint]))
now = time.monotonic()
with _rate_lock:
recent = [t for t in _rate_hits.get(key, ()) if now - t < window]
if len(recent) >= count:
for key, (count, window) in checks:
recent = [t for t in _rate_hits.get(key, ()) if now - t < window]
_rate_hits[key] = recent
retry_after = int(window - (now - recent[0])) + 1
abort(429, description=f"Too many attempts. Try again in {retry_after} seconds.")
recent.append(now)
_rate_hits[key] = recent
# Drop addresses with nothing left in their window so the table stays small.
if len(recent) >= count:
retry_after = int(window - (now - recent[0])) + 1
abort(429, description=f"Too many attempts. Try again in {retry_after} seconds.")
for key, _ in checks:
_rate_hits[key].append(now)
# Drop keys with nothing left in the longest window so the table stays small.
if len(_rate_hits) > 10000:
for stale in [k for k, hits in _rate_hits.items() if now - hits[-1] >= RATE_LIMITS[k[0]][1]]:
longest = max(window for _, window in [*RATE_LIMITS.values(), *ACCOUNT_RATE_LIMITS.values()])
for stale in [k for k, hits in _rate_hits.items() if not hits or now - hits[-1] >= longest]:
del _rate_hits[stale]


Expand Down
96 changes: 96 additions & 0 deletions docs/campus_map.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading