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
10 changes: 9 additions & 1 deletion .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ jobs:
- name: Run tests
run: pnpm test:ci --passWithNoTests

- name: Install Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Run edge function tests
run: pnpm test:edge

database-compatibility:
name: Database Contract (Static)
runs-on: ubuntu-latest
Expand Down Expand Up @@ -156,4 +164,4 @@ jobs:
run: pnpm build
env:
NEXT_PUBLIC_SUPABASE_URL: https://ci-build.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY: ci-build-placeholder
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY: ci-build-placeholder
2 changes: 1 addition & 1 deletion ML/documentation/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Create `.env` at the repo root:

```bash
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_SECRET_KEY=your_service_role_or_secret_key
SUPABASE_SECRET_KEY=your_secret_key
```

`main.py` loads `.env` via `python-dotenv` when you run from `Algorithm/`.
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ Both files are gitignored. Get the values from the Supabase dashboard (**Project

```bash
NEXT_PUBLIC_SUPABASE_URL=https://<project-ref>.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=<anon key>
SUPABASE_SECRET_KEY=<service role key> # server only, never NEXT_PUBLIC_
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=<publishable key, sb_publishable_...>
SUPABASE_SECRET_KEY=<secret key, sb_secret_...> # server only, never NEXT_PUBLIC_

# Your own Google OAuth client for local sign-in (step 5)
SUPABASE_AUTH_EXTERNAL_GOOGLE_CLIENT_ID=
Expand All @@ -66,11 +66,11 @@ NEXT_PUBLIC_ENABLE_MATCH_REQUESTS=false

```bash
SUPABASE_URL=https://<project-ref>.supabase.co
SUPABASE_SECRET_KEY=<service role key>
SUPABASE_SECRET_KEY=<secret key, sb_secret_...>
```

> [!CAUTION]
> The service role key bypasses all database security. Never commit it or paste it into chats or issues.
> The secret key bypasses all database security. Never commit it or paste it into chats or issues.

### 4. Pull the database schema

Expand Down
2 changes: 1 addition & 1 deletion frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Next.js app for managing pickup workflows, matching requests, and admin operatio
2. Create `.env.local`:
```bash
NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=your_supabase_publishable_key
```
3. Run locally:
```bash
Expand Down
6 changes: 3 additions & 3 deletions frontend/documentation/CODEBASE_ONBOARDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Quick path from clone to a safe first change.
## Prerequisites

- Node 20+, **pnpm** 9.15+ (`corepack use pnpm@9.15.2`)
- Supabase project access (URL, anon key, service role secret)
- Supabase project access (URL, publishable key, secret key)
- Google OAuth configured in Supabase (for local sign-in)
- Netlify access if you need to check production env vars or deploy settings

Expand All @@ -19,8 +19,8 @@ Create `.env.local` (minimum):

```bash
NEXT_PUBLIC_SUPABASE_URL=https://<project>.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=<anon-key>
SUPABASE_SECRET_KEY=<service-role-key> # server only; never expose
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=<publishable-key>
SUPABASE_SECRET_KEY=<secret-key> # server only; never expose
```

Optional:
Expand Down
12 changes: 12 additions & 0 deletions frontend/documentation/OPERATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,15 @@ You can change the edge functions to only send emails when the matches are manua
Supabase Dashboard → **Integrations** → **Cron** → enable job **`delays-sweep-20`**.

[Cron jobs](https://supabase.com/dashboard/project/zgunhxopkgbksfoxthpn/integrations/cron/jobs)

The job must send the same `Authorization` header as the database triggers, or `delays-sweep` answers 401 on every run. Its command reads the header from `app.secrets`, so rotating the secret key only needs the `edge_auth` row updated:

```sql
select net.http_post(
url := 'https://zgunhxopkgbksfoxthpn.supabase.co/functions/v1/delays-sweep',
headers := jsonb_build_object(
'Authorization', (select value from app.secrets where name = 'edge_auth')
),
timeout_milliseconds := 5000
);
```
22 changes: 12 additions & 10 deletions frontend/documentation/SUPABASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ Source of truth: [`supabase/functions/`](../supabase/functions). Change them her
| `send-algorithm-match-email` | `on_algorithm_match_created` / `is_verified_match_trigger` triggers (both **disabled** in production) | Per-rider match email |
| `delays-sweep` | `delays-sweep-20` cron job | Polls AirLabs and emails a ride group when a member's flight is delayed or cancelled |

**Auth:** every function requires the **service role key** as `Authorization: Bearer <key>` ([`_shared/auth.ts`](../supabase/functions/_shared/auth.ts)) and returns 401 otherwise. The anon key is public, so it must never be enough to send email. Callers:
**Auth:** every function requires one of the project's **secret API keys** (`sb_secret_...`), sent as `Authorization: Bearer <key>` or in the `apikey` header ([`_shared/auth.ts`](../supabase/functions/_shared/auth.ts)), and returns 401 otherwise. Supabase injects the current secret keys into every function as `SUPABASE_SECRET_KEYS`, so creating or deleting a key in the dashboard needs no redeploy. The functions also use that key for their own database access (the key named `default`, otherwise the first one). The publishable key is public, so it must never be enough to send email, and the legacy `service_role` JWT is never accepted. Callers:

- Admin API routes check the admin role, then call the function through `relayEdgeFunction` (`src/lib/server/edgeFunctions.ts`) with `SUPABASE_SECRET_KEY`.
- Triggers send `app.secrets.edge_auth`, which must be `Bearer <service role key>`.
- The cron job must send the same header. `delays-sweep` has `verify_jwt = false` at the gateway (`config.toml`), so the in-code check is its only protection.
- Triggers send `app.secrets.edge_auth`, which must be `Bearer <secret key>`.
- The cron job must send the same header.

Secret keys are not JWTs, so every function has `verify_jwt = false` at the gateway (`config.toml`); the in-code check is their only protection.

**Custom emails** are plain text: HTML is escaped and line breaks are kept. Placeholders such as `{{name}}` are filled in per recipient (the composer in the admin dashboard lists them).

Expand All @@ -34,7 +36,7 @@ Email **content rules** (variants: subsidized × direction) are described in [BA
pnpm exec supabase functions deploy <name> --project-ref <project-ref>
```

Deploying `delays-sweep` picks up `verify_jwt = false` from `config.toml`.
Deploys pick up `verify_jwt = false` from `config.toml`.

## Postgres RPCs used by the app

Expand Down Expand Up @@ -126,10 +128,10 @@ If production already has equivalent definitions, diff before applying to avoid

## Environment variables (Supabase-related)

| Variable | Exposure | Use |
| ------------------------------- | --------------- | ---------------------------------------------------------------- |
| `NEXT_PUBLIC_SUPABASE_URL` | Public | Client + server |
| `NEXT_PUBLIC_SUPABASE_ANON_KEY` | Public | Client + server session; edge invoke from admin routes |
| `SUPABASE_SECRET_KEY` | **Server only** | Service role client for admin commands and selected student APIs |
| Variable | Exposure | Use |
| -------------------------------------- | --------------- | ---------------------------------------------------------------------------------------------- |
| `NEXT_PUBLIC_SUPABASE_URL` | Public | Client + server |
| `NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY` | Public | Client + server session (`sb_publishable_...`) |
| `SUPABASE_SECRET_KEY` | **Server only** | Secret key (`sb_secret_...`) for admin commands, selected student APIs and edge function calls |

Never prefix the service role key with `NEXT_PUBLIC_`.
Never prefix the secret key with `NEXT_PUBLIC_`.
2 changes: 2 additions & 0 deletions frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const customJestConfig = {
'^@/(.*)$': '<rootDir>/src/$1',
},
testEnvironment: 'jsdom',
// Edge functions run on Deno and are tested with `pnpm test:edge`.
testPathIgnorePatterns: ['<rootDir>/supabase/functions/'],
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
Expand Down
5 changes: 4 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"prepare": "cd .. && husky install frontend/.husky || true",
"test": "jest",
"test:ci": "jest --ci",
"test:edge": "deno test --allow-env supabase/functions",
"test:database-contract": "jest --ci --runInBand src/lib/server/databaseContract.test.ts",
"audit:security": "pnpm audit --audit-level high",
"analyze": "ANALYZE=true pnpm build",
Expand Down Expand Up @@ -82,6 +83,7 @@
"@typescript-eslint/parser": "8.61.0",
"brace-expansion": "5.0.12",
"browserslist": "4.29.3",
"cross-spawn@^7.0.0": "7.1.2",
"js-yaml@^3.0.0": "3.15.2",
"js-yaml@^4.0.0": "4.3.2",
"nanoid@^3.0.0": "3.3.20",
Expand All @@ -90,7 +92,8 @@
"smol-toml": "1.9.1",
"source-map-js": "1.2.2",
"tar": "7.5.22",
"webpack-bundle-analyzer>ws": "7.5.11"
"webpack-bundle-analyzer>ws": "7.5.14",
"ws@^8.0.0": "8.23.1"
},
"auditConfig": {
"ignoreGhsas": [
Expand Down
34 changes: 18 additions & 16 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions frontend/src/app/contact/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,14 @@ export default function Contact() {
<Mail aria-hidden className="mt-0.5 h-5 w-5 shrink-0 text-stone" />
<div>
<h3 className="font-semibold text-navy">Email support</h3>
<p className="mt-1 text-sm text-stone">
For general questions:
</p>
<p className="mt-1 text-sm text-stone">For general questions:</p>
<a
href={`mailto:${SUPPORT_EMAIL}`}
className="mt-1 inline-block break-all text-[15px] font-medium text-navy underline underline-offset-2 hover:text-sage"
>
{SUPPORT_EMAIL}
</a>
<p className="mt-1 text-sm text-stone">
For technical questions:
</p>
<p className="mt-1 text-sm text-stone">For technical questions:</p>
<a
href={`mailto:${TECH_EMAIL}`}
className="mt-1 inline-block break-all text-[15px] font-medium text-navy underline underline-offset-2 hover:text-sage"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/server/edgeFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createServiceRoleClient } from '@/lib/server/serviceRole'

/**
* Invokes an internal edge function with the service role key and relays its JSON response.
* The edge functions reject the public anon key, so callers must authorize the user first.
* The edge functions reject the public publishable key, so callers must authorize the user first.
*/
export async function relayEdgeFunction(name: string, body: unknown) {
const { data, error } = await createServiceRoleClient().functions.invoke(
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/utils/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export const createBrowserClient = () => {
if (typeof window === 'undefined') {
return browserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
)
}
if (!browserClientSingleton) {
browserClientSingleton = browserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
)
}
return browserClientSingleton
Expand All @@ -29,7 +29,7 @@ export const createServerClient = (
) =>
serverClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
{
cookies: {
getAll() {
Expand All @@ -54,7 +54,7 @@ export const createMiddlewareClient = (request: NextRequest) => {

const supabase = serverClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
{
cookies: {
getAll() {
Expand Down
18 changes: 15 additions & 3 deletions frontend/supabase/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,20 @@ enabled = true
# JSON string passed through to pg-delta SQL formatting.
# format_options = "{\"keywordCase\":\"upper\",\"indent\":2,\"maxWidth\":80,\"commaStyle\":\"trailing\"}"

# Edge functions (source in supabase/functions). All of them require the service role key in
# code (see _shared/auth.ts). delays-sweep is called by cron, so the gateway JWT check is off for
# it, matching production.
# Edge functions (source in supabase/functions). All of them require one of the project's secret
# API keys (sb_secret_...) in code, see _shared/auth.ts. Those keys are not JWTs, so the gateway JWT
# check has to be off for every function; the in-code check is what protects them.
[functions.delays-sweep]
verify_jwt = false

[functions.send-algorithm-match-email]
verify_jwt = false

[functions.send-all-match-emails-batch]
verify_jwt = false

[functions.send-flight-submission-email]
verify_jwt = false

[functions.send-unmatched-emails-batch]
verify_jwt = false
Loading
Loading