Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

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

64 changes: 64 additions & 0 deletions app/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,64 @@ VISUALCI_STRIPE_WEBHOOK_SECRET=
# again.
# VISUALCI_LIVE_STREAM_SECONDS=1800

######################################################################################################################
# Abuse limits
######################################################################################################################

# GitHub App deliveries a minute acted on at once for one installation set up for an organization on a paid plan (or
# linked to a project of one). Past it a delivery is kept and acted on later at this rate (GitHub never redelivers on
# its own). A paid installation's deliveries count against nothing shared.
# VISUALCI_GITHUB_WEBHOOKS_PER_INSTALLATION=600

# The same for an installation set up for an organization on the free plan, which anyone can make. Its deliveries also
# count against VISUALCI_GITHUB_WEBHOOKS_TOTAL.
# VISUALCI_GITHUB_WEBHOOKS_PER_FREE_INSTALLATION=120

# GitHub App deliveries a minute for one installation nobody has set up. Past it a delivery is dropped (a 202, and a
# warning in the log): nothing of a customer's depends on it. Its installation deliveries (installed, suspended,
# removed), which setting it up needs, count only in VISUALCI_GITHUB_WEBHOOKS_TOTAL.
# VISUALCI_GITHUB_WEBHOOKS_PER_UNCLAIMED_INSTALLATION=60

# GitHub App deliveries a minute for all the installations nobody has set up, together, so many strangers'
# installations can't crowd out customers' deliveries. Dropped as above past it.
# VISUALCI_GITHUB_WEBHOOKS_UNCLAIMED_TOTAL=600

# GitHub App deliveries a minute for unclaimed and free installations together (paid installations' don't count). A
# free installation's delivery past it is kept for later; an unclaimed one's dropped.
# VISUALCI_GITHUB_WEBHOOKS_TOTAL=6000

# The most megabytes of deliveries kept for later for one paid installation. Past a cap, a delivery is answered 503
# (GitHub logs it as failed, and the redelivery check sends it again later) with an error in the log.
# VISUALCI_GITHUB_WEBHOOKS_DEFERRED_PER_INSTALLATION_MB=50

# The most megabytes kept for later for one free installation.
# VISUALCI_GITHUB_WEBHOOKS_DEFERRED_PER_FREE_INSTALLATION_MB=5

# The most megabytes kept for later for every free installation together, so free installations, which anyone can
# make, can't fill the store paid ones need.
# VISUALCI_GITHUB_WEBHOOKS_DEFERRED_FREE_TOTAL_MB=50

# The most megabytes kept for later for every installation together.
# VISUALCI_GITHUB_WEBHOOKS_DEFERRED_TOTAL_MB=500

# OAuth client registrations (/oauth/register) a day from one address (an IPv6 address's /64, since one host gets a
# whole /64) for one redirect URI host. Hosted platforms (such as claude.ai's connectors) register their users'
# clients from shared addresses with their own callback host, so each gets a count of its own.
# VISUALCI_OAUTH_REGISTRATIONS_PER_REDIRECT_HOST=50

# OAuth client registrations a day from one address (or IPv6 /64) across every redirect host, and three times that
# from an IPv6 /48.
# VISUALCI_OAUTH_REGISTRATIONS_PER_ADDRESS=500

# The most registered OAuth clients that have never connected. A registration past it first deletes the oldest of them
# registered over a day ago with no sign-in under way (a warning in the log), and is refused with a 429 while none is
# that old; oauth:prune deletes the rest after a week.
# VISUALCI_OAUTH_UNUSED_CLIENTS=100000

# Past this many clients that have never connected, the oldest are deleted whatever their age, so young registrations
# can't keep new ones out.
# VISUALCI_OAUTH_UNUSED_CLIENTS_CEILING=150000

######################################################################################################################
# Heartbeats
######################################################################################################################
Expand Down Expand Up @@ -255,6 +313,12 @@ VISUALCI_STRIPE_WEBHOOK_SECRET=
# Secret. The credit override reminder's token.
# VISUALCI_CREDIT_OVERRIDES_HEARTBEAT_TOKEN=

# The check for GitHub App deliveries that failed (every ten minutes), which asks GitHub to redeliver them.
# VISUALCI_WEBHOOK_REDELIVERY_HEARTBEAT_URL=

# Secret. The redelivery check's token.
# VISUALCI_WEBHOOK_REDELIVERY_HEARTBEAT_TOKEN=

######################################################################################################################
# Laravel Rust
######################################################################################################################
Expand Down
2 changes: 2 additions & 0 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ path = "src/main.rs"
[dependencies]
# Pinned exactly: each 0.x minor of Laravel Rust may break, and upgrades are deliberate.
laravel-rust = { version = "=0.1.4", features = ["database-postgres", "minijinja"] }
# The framework's rate-limit buckets, for giving a count back (`laravel::rate_limit` doesn't export `Bucket` in 0.1.4)
laravel_cache = { package = "laravel-rust-cache", version = "=0.1.4", default-features = false }
# The same axum Laravel Rust runs on, for extractors (auth::Principal)
axum = "0.8"
base64 = "0.22"
Expand Down
112 changes: 112 additions & 0 deletions app/database/migrations/2026_12_06_000000_bounded_forge_work.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//! Bounded forge work now that the GitHub App is public (sc-9144, sc-9124).
//!
//! - `access_rechecks`: the people a webhook asked to recheck, waiting for the next `RecheckAccess` run: deliveries
//! add to it, and one debounced run reads every linked repository's collaborators once for all of them. `/status`
//! says how long the oldest has waited.
//! - `deferred_webhooks`: verified deliveries for a claimed installation that arrived over its rate limit, kept to be
//! acted on at the allowed rate (`DrainDeferredWebhooks`) instead of refused, since GitHub never redelivers on its
//! own. Capped in bytes per installation and in total. `/status` says how long the oldest has waited.
//! - `webhook_redeliveries`: failed deliveries the ten-minute check found in GitHub's delivery log, drained across runs;
//! `webhook_redelivery_marks` how far it has read the log (and where to go on after a run that stopped at its cap).
//! - `deferred_webhook_bytes`: the bytes kept for later per installation, for free installations and in total.
//! - `webhook_deliveries_seen`: claimed installations' deliveries acted on, by GUID, so one GitHub sends twice is acted
//! on once.
//! - `user_installations.repositories_listed`/`repositories_total`/`repositories_complete`: how many of an
//! installation's repositories the person's last sign-in listed for the picker, of how many GitHub has (`NULL` when
//! it read none), and whether that's all of them. The picker's search goes to GitHub for an installation it didn't
//! list completely.
//! - `projects_forge_installation` finds an installation's projects, which every delivery asks (whether anyone has
//! claimed the installation) and a team change rechecks.

use laravel::db::AnyTransaction;

pub async fn migrate(tx: &mut AnyTransaction<'_>) -> laravel::Result<()> {
tx.query(SCHEMA).unprepared().await?;
Ok(())
}

const SCHEMA: &str = r#"
CREATE TABLE access_rechecks (
user_id BIGINT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
queued_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE deferred_webhooks (
id BIGSERIAL PRIMARY KEY,
installation_id TEXT NOT NULL,
-- GitHub's delivery GUID (X-GitHub-Delivery), so a redelivered one is kept once.
delivery TEXT NOT NULL,
kind TEXT NOT NULL,
-- The delivery's body, whose signature was checked before it was kept.
payload TEXT NOT NULL,
bytes BIGINT NOT NULL,
-- Whether its installation was a paid organization's when it was kept (free ones share a smaller cap).
paid BOOLEAN NOT NULL DEFAULT false,
attempts INT NOT NULL DEFAULT 0,
-- A drain acting on it until then: another run leaves it (and the rest of its installation's) alone.
claimed_until TIMESTAMPTZ,
received_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX deferred_webhooks_delivery ON deferred_webhooks (delivery) WHERE delivery <> '';
CREATE INDEX deferred_webhooks_installation ON deferred_webhooks (installation_id, id);

-- Failed deliveries the redelivery check found in the App's delivery log, by GUID, drained across runs: `pending`
-- (to ask GitHub for, from `next_at`), `redelivered` (asked; a newer attempt that fails makes it pending again),
-- `succeeded` (an attempt got a 2xx), `given_up` (after `tries` asks, or older than GitHub keeps), or `skipped` (an
-- unclaimed installation's).
CREATE TABLE webhook_redeliveries (
guid TEXT PRIMARY KEY,
installation_id TEXT,
-- Whether its installation was a paid organization's when it was recorded: paid ones are asked for first.
paid BOOLEAN NOT NULL DEFAULT false,
-- The oldest and the newest attempt seen, and the attempt last asked to be redelivered.
first_attempt_id BIGINT NOT NULL,
last_attempt_id BIGINT NOT NULL,
requested_attempt_id BIGINT,
state TEXT NOT NULL DEFAULT 'pending',
tries INT NOT NULL DEFAULT 0,
next_at TIMESTAMPTZ NOT NULL DEFAULT now(),
error TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX webhook_redeliveries_pending ON webhook_redeliveries (paid DESC, first_attempt_id) WHERE state = 'pending';

-- How far the redelivery check has read GitHub's delivery log: everything at or below `delivery_id` is read. A run
-- that stopped at its page cap keeps where to go on (`resume_cursor`) and the newest ID that scan started from
-- (`resume_newest`), which becomes the mark once the scan reaches the old one.
CREATE TABLE webhook_redelivery_marks (
name TEXT PRIMARY KEY,
delivery_id BIGINT,
resume_cursor TEXT,
resume_newest BIGINT,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

-- Bytes of deliveries kept for later, per installation (`installation:<id>`), for free installations (`free`) and in
-- total (`total`), kept in step with `deferred_webhooks` so keeping one locks only its installation's row.
CREATE TABLE deferred_webhook_bytes (
scope TEXT PRIMARY KEY,
bytes BIGINT NOT NULL DEFAULT 0
);

-- Claimed installations' deliveries, by GUID, so one sent twice is acted on once: `processing` from `started_at` (a
-- claim older than two minutes was abandoned, a request cut off or a worker gone, and the next delivery takes it
-- over), then `done`. Kept three days.
CREATE TABLE webhook_deliveries_seen (
delivery TEXT PRIMARY KEY,
state TEXT NOT NULL DEFAULT 'processing',
-- A random token naming the claim: only its holder finishes or lets it go, so a late release can't undo a
-- takeover's claim.
owner TEXT NOT NULL DEFAULT '',
started_at TIMESTAMPTZ NOT NULL DEFAULT now(),
seen_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX webhook_deliveries_seen_at ON webhook_deliveries_seen (seen_at);

ALTER TABLE user_installations ADD COLUMN repositories_listed INT NOT NULL DEFAULT 0;
ALTER TABLE user_installations ADD COLUMN repositories_total INT;
ALTER TABLE user_installations ADD COLUMN repositories_complete BOOLEAN NOT NULL DEFAULT true;

CREATE INDEX projects_forge_installation ON projects (forge_installation) WHERE forge_installation IS NOT NULL;
"#;
7 changes: 5 additions & 2 deletions app/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,12 @@ pub fn routes(route: &mut laravel::prelude::Router) {
oauth::routes(route);
device::routes(route);
staff::routes(route);
route.get("/auth/{forge}", sign_in::start);
// Anyone can start a sign-in, and each callback exchanges its code with the forge: 60 a minute per address keeps a
// flood from becoming a flood of calls to GitHub on VisualCI's OAuth client (sc-9144)
let sign_ins = || laravel::http::middleware::Throttle::per_minute(60).bucket("sign-in");
route.get("/auth/{forge}", sign_in::start).middleware(sign_ins());
route.post("/auth/{forge}/link", sign_in::link);
route.get("/auth/{forge}/callback", sign_in::callback);
route.get("/auth/{forge}/callback", sign_in::callback).middleware(sign_ins());
route.get("/auth/session", sign_in::session);
route.get("/auth/providers", sign_in::providers);
route.post("/auth/logout", sign_in::logout);
Expand Down
Loading
Loading