feat(guardrails): secure-by-default rule and /guardrails scanner

Always-on rules/secure-by-default.md: hosting, a11y, Supabase RLS,
SMS/email consent, cloud functions, API hygiene incl. password storage.
Stdlib scanner (FAIL/WARN/MANUAL) + skill, 89-check suite, auditor
gate wiring, profiles, routing row (plus Explore rows confirmed
through the router dialog).
This commit is contained in:
bchanot committed 2026-10-18 18:10:20 +02:00
1 parent c5fad5ee8f
commit 558c49e1a1
45 files changed
+2473 -5

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
SUPABASE_URL=
SUPABASE_ANON_KEY=
@@ -0,0 +1,2 @@
.env
node_modules
+6
View File
@@ -0,0 +1,6 @@
# Deploy
Traffic goes through Cloudflare, with WAF and DDoS rules on.
Rate limits live in the middleware.
Spend cap is set in the host dashboard, and a budget alert triggers the
kill switch described in the runbook.
+1
View File
@@ -0,0 +1 @@
{ "functions": { "source": "functions" } }
+8
View File
@@ -0,0 +1,8 @@
const { onDocumentCreated } = require("firebase-functions/v2/firestore");
const { setGlobalOptions } = require("firebase-functions/v2");
setGlobalOptions({ maxInstances: 10 });
exports.onOrder = onDocumentCreated("orders/{id}", async (event) => {
await db.collection("receipts").add({ order: event.params.id });
});
+1
View File
@@ -0,0 +1 @@
{ "name": "fixture", "dependencies": { "@supabase/supabase-js": "^2.0.0" } }
@@ -0,0 +1,6 @@
import { useState } from "react";
export function Home() {
const [n] = useState(0);
return <p>{n}</p>;
}
@@ -0,0 +1,31 @@
export function ContactForm({ go }: { go: () => void }) {
return (
<form>
<label htmlFor="email">Email</label>
<input id="email" type="email" />
<label>
Message
<textarea name="msg" />
</label>
<input type="hidden" name="token" value="x" />
<img src="/logo.png" alt="Company logo" />
<img src="/divider.png" alt="" />
<button
type="submit"
className="btn outline-none focus-visible:ring-2 focus-visible:ring-blue-500"
>
Send
</button>
<div role="button" tabIndex={0} onClick={go} onKeyDown={go}>
Open
</div>
<div role="presentation" onClick={go}>
<div role="dialog" aria-modal="true" onClick={(e) => e.stopPropagation()}>
Dialog
</div>
</div>
<input type="file" className="hidden" />
<div className="backdrop" onClick={go} />
</form>
);
}
+11
View File
@@ -0,0 +1,11 @@
import { createClient } from "@supabase/supabase-js";
export async function getUser(request: Request) {
const header = request.headers.get("authorization") ?? "";
const token = header.replace("Bearer ", "");
if (!token) return null;
const sb = createClient(
process.env.SUPABASE_URL!, process.env.SUPABASE_ANON_KEY!);
const { data } = await sb.auth.getUser(token);
return data.user;
}
+5
View File
@@ -0,0 +1,5 @@
import { Ratelimit } from "@upstash/ratelimit";
export const limiter = new Ratelimit({
limiter: Ratelimit.slidingWindow(10, "10 s"),
});
@@ -0,0 +1,7 @@
import { getUser } from "../../lib/auth";
export async function GET(request: Request) {
const user = await getUser(request);
if (!user) return new Response("unauthorized", { status: 401 });
return Response.json({ ok: true });
}
+13
View File
@@ -0,0 +1,13 @@
:focus-visible {
outline: 2px solid #1a4fd6;
outline-offset: 2px;
}
button:focus:not(:focus-visible) {
outline: none;
}
.search:focus {
outline: none;
border-color: #1a4fd6;
}
@@ -0,0 +1,2 @@
[functions.notify]
verify_jwt = true
@@ -0,0 +1,22 @@
import { Resend } from "npm:resend";
const resend = new Resend(Deno.env.get("RESEND_API_KEY"));
Deno.serve(async (request) => {
const user = await getUser(request);
const contact = await loadContact(user.id);
if (!contact.consent_at || contact.unsubscribed_at) {
return new Response("skipped");
}
const link = unsubscribeUrl(contact.id);
await resend.emails.send({
to: contact.email,
subject: "News",
html: `<p>Hello</p><a href="${link}">Unsubscribe</a>`,
headers: {
"List-Unsubscribe": `<${link}>`,
"List-Unsubscribe-Post": "List-Unsubscribe=One-Click",
},
});
return new Response("sent");
});
@@ -0,0 +1,16 @@
import twilio from "npm:twilio";
const client = twilio(Deno.env.get("TWILIO_SID"), Deno.env.get("TWILIO_TOKEN"));
Deno.serve(async (request) => {
const user = await getUser(request);
const contact = await loadContact(user.id);
if (!contact.consent_at || contact.opt_out_at) {
return new Response("skipped");
}
await client.messages.create({
to: contact.phone,
body: "Your offer is ready. Reply STOP to opt out.",
});
return new Response("sent");
});
@@ -0,0 +1,55 @@
create table public.notes (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references auth.users (id),
body text not null
);
alter table public.notes enable row level security;
create policy "owner read" on public.notes
for select to authenticated
using ((select auth.uid()) = user_id);
create policy "owner insert" on public.notes
for insert to authenticated
with check ((select auth.uid()) = user_id);
create table public.contacts (
id uuid primary key default gen_random_uuid(),
owner_id uuid not null references auth.users (id),
email text,
phone text,
consent_at timestamptz not null,
consent_source text not null,
unsubscribed_at timestamptz,
opt_out_at timestamptz
);
alter table public.contacts enable row level security;
create policy "owner contacts" on public.contacts
for all to authenticated
using ((select auth.uid()) = owner_id)
with check ((select auth.uid()) = owner_id);
create view public.my_notes with (security_invoker = true) as
select * from public.notes;
create function public.touch_updated() returns trigger
language plpgsql as $$
begin
return new;
end;
$$;
create function public.admin_count() returns bigint
language sql security definer set search_path = '' as $$
select count(*) from public.notes;
$$;
revoke execute on function public.admin_count() from public, anon;
create trigger notes_notify after insert on public.notes
for each row execute function supabase_functions.http_request(
'https://example.supabase.co/functions/v1/notify', 'POST', '{}', '{}', '5000'
);
create policy "service delete" on public.notes
for delete to service_role using (true);
+1
View File
@@ -0,0 +1 @@
{ "headers": [] }