Merge pull request #1659 from ludovic111/feat/mod-registry-server
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
name: mod-registry
|
||||
# The submission service is its own workspace (crates/horizon-mod-registry), so the checks job doesn't build
|
||||
# it: this one does, when it or horizon-mods (the checks it shares with the launcher and the game) changes.
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
paths:
|
||||
- 'crates/horizon-mods/**'
|
||||
- 'crates/horizon-mod-registry/**'
|
||||
- '.github/workflows/mod-registry.yml'
|
||||
pull_request:
|
||||
paths:
|
||||
- 'crates/horizon-mods/**'
|
||||
- 'crates/horizon-mod-registry/**'
|
||||
- '.github/workflows/mod-registry.yml'
|
||||
permissions:
|
||||
contents: read
|
||||
jobs:
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
defaults:
|
||||
run:
|
||||
working-directory: crates/horizon-mod-registry
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: rustup toolchain install stable --profile minimal --component rustfmt --component clippy
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
workspaces: crates/horizon-mod-registry
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- run: cargo +stable fmt --check
|
||||
- run: cargo +stable clippy --all-targets --locked -- -D warnings
|
||||
- run: cargo +stable test --locked
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
[workspace]
|
||||
exclude = ["launcher", "crates/horizon-telemetry-server", "third_party/wgpu-hal"]
|
||||
exclude = ["launcher", "crates/horizon-telemetry-server", "crates/horizon-mod-registry", "third_party/wgpu-hal"]
|
||||
members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"]
|
||||
resolver = "2"
|
||||
|
||||
|
||||
Generated
+1352
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,33 @@
|
||||
[package]
|
||||
name = "horizon-mod-registry"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "MIT"
|
||||
rust-version = "1.90"
|
||||
publish = false
|
||||
description = "The registry's submission service: GitHub sign-in, the file's checks, staging, and a pull request (docs/api/mod-index.md)"
|
||||
|
||||
# Its own workspace, as the telemetry server's: the game's workspace never builds a web server.
|
||||
[workspace]
|
||||
|
||||
[dependencies]
|
||||
horizon-mods = { path = "../horizon-mods", default-features = false, features = ["index"] }
|
||||
axum = { version = "0.8", default-features = false, features = ["http1", "tokio", "json", "query", "multipart"] }
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "signal", "fs", "io-util"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
toml = "1"
|
||||
# GitHub's API, blocking, on tokio's blocking pool: the launcher's HTTP client.
|
||||
ureq = { version = "2", default-features = false, features = ["json", "tls"] }
|
||||
hmac = "0.12"
|
||||
sha2 = "0.10"
|
||||
base64 = "0.22"
|
||||
getrandom = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
tower = { version = "0.5", features = ["util"] }
|
||||
http-body-util = "0.1"
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
lto = "thin"
|
||||
@@ -0,0 +1,14 @@
|
||||
# Built from the repository root: docker build -f crates/horizon-mod-registry/Dockerfile .
|
||||
FROM rust:1-slim AS build
|
||||
WORKDIR /src
|
||||
# The root manifest only for the shared crate's workspace settings (edition, lints).
|
||||
COPY Cargo.toml Cargo.toml
|
||||
COPY crates/horizon-mods crates/horizon-mods
|
||||
COPY crates/horizon-mod-registry crates/horizon-mod-registry
|
||||
RUN cargo build --release --locked --manifest-path crates/horizon-mod-registry/Cargo.toml
|
||||
|
||||
FROM debian:stable-slim
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*
|
||||
COPY --from=build /src/crates/horizon-mod-registry/target/release/horizon-mod-registry /usr/local/bin/
|
||||
USER nobody
|
||||
CMD ["horizon-mod-registry"]
|
||||
@@ -0,0 +1,44 @@
|
||||
# horizon-mod-registry
|
||||
|
||||
The service behind the registry's submission form (`playhorizon.dev/mods/submit`). A creator signs in with GitHub and
|
||||
sends a file with its page; the service runs the file's own checks (`horizon_mods::index::inspect`, the signature, the
|
||||
entry's fields), uploads it to the `staging` release of
|
||||
[ludovic111/horizon-mods](https://github.com/ludovic111/horizon-mods) as `<sha256>.<extension>`, and opens a pull
|
||||
request adding or updating `mods/<id>.toml`. That repository's CI checks it again, with a trial run of a mod in the
|
||||
sandbox; reviewers decide, and merging publishes. Formats and rules: [docs/api/mod-index.md](../../docs/api/mod-index.md).
|
||||
|
||||
It keeps no data: a session is a signed cookie holding the GitHub login, and the user's OAuth token is used once, to
|
||||
read that login, and dropped. The sign-in asks for no scope.
|
||||
|
||||
| Route | |
|
||||
| --- | --- |
|
||||
| `GET /api/mods/auth/login` | Starts the GitHub sign-in |
|
||||
| `GET /api/mods/auth/callback` | GitHub's redirect: sets the session, back to `/mods/submit` |
|
||||
| `POST /api/mods/auth/logout` | Ends it |
|
||||
| `GET /api/mods/me` | `{login, csrf}`, or 401 |
|
||||
| `POST /api/mods/submit` | Multipart: `entry` (JSON: `id kind name summary description authors license source icon key signature version game changelog`) and `file`; `{pull_request}` or `{error}` |
|
||||
| `GET /healthz` | `ok` |
|
||||
|
||||
A submission must come from `REGISTRY_PUBLIC_URL`'s origin with the session's `X-CSRF-Token`. Six per account and sixty
|
||||
in all per hour. Only an entry's `owners` publish its updates; a key change, an icon in the repository or anything else
|
||||
is a pull request by hand.
|
||||
|
||||
## Running it
|
||||
|
||||
```sh
|
||||
cargo test # against a mock GitHub (tests/submit.rs)
|
||||
REGISTRY_PUBLIC_URL=http://localhost:4173 REGISTRY_SESSION_KEY=... GITHUB_CLIENT_ID=... GITHUB_CLIENT_SECRET=... \
|
||||
GITHUB_BOT_TOKEN=... REGISTRY_REPO=you/horizon-mods-test cargo run
|
||||
```
|
||||
|
||||
| Variable | |
|
||||
| --- | --- |
|
||||
| `REGISTRY_PUBLIC_URL` | The site, `https://playhorizon.dev`: the OAuth callback is `<it>/api/mods/auth/callback` |
|
||||
| `REGISTRY_SESSION_KEY` | 32 characters or more, random: signs sessions |
|
||||
| `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET` | The OAuth app |
|
||||
| `GITHUB_BOT_TOKEN` | Fine-grained, the registry repository only: Contents and Pull requests read and write |
|
||||
| `REGISTRY_REPO` | Default `ludovic111/horizon-mods` |
|
||||
| `PORT` | Default 8080 |
|
||||
|
||||
On Railway it is its own service (`railway.json`, built with the `Dockerfile` from the repository root); the website's
|
||||
Caddy sends `/api/mods/*` to it over Railway's private network.
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"$schema": "https://railway.com/railway.schema.json",
|
||||
"build": {
|
||||
"builder": "DOCKERFILE",
|
||||
"dockerfilePath": "crates/horizon-mod-registry/Dockerfile",
|
||||
"watchPatterns": [
|
||||
"/crates/horizon-mods/**",
|
||||
"/crates/horizon-mod-registry/**",
|
||||
"/Cargo.toml"
|
||||
]
|
||||
},
|
||||
"deploy": {
|
||||
"healthcheckPath": "/healthz",
|
||||
"healthcheckTimeout": 60,
|
||||
"restartPolicyType": "ON_FAILURE",
|
||||
"restartPolicyMaxRetries": 5,
|
||||
"multiRegionConfig": {
|
||||
"europe-west4-drams3a": { "numReplicas": 1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
//! What the service asks of GitHub: the sign-in (OAuth, no scope: only the
|
||||
//! public profile), and with the bot's token on the registry repository
|
||||
//! alone, an entry, the `staging` release, a branch, a file, a pull request.
|
||||
//! Blocking (ureq): called from tokio's blocking pool.
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
use horizon_mods::index::registry::Entry;
|
||||
use serde_json::{Value, json};
|
||||
use std::{path::Path, time::Duration};
|
||||
|
||||
/// Where GitHub is: the real one, or a test's mock.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Endpoints {
|
||||
/// `https://github.com`: OAuth.
|
||||
pub web: String,
|
||||
/// `https://api.github.com`.
|
||||
pub api: String,
|
||||
/// `https://uploads.github.com`: release assets.
|
||||
pub uploads: String,
|
||||
}
|
||||
|
||||
impl Default for Endpoints {
|
||||
fn default() -> Self {
|
||||
Endpoints {
|
||||
web: "https://github.com".into(),
|
||||
api: "https://api.github.com".into(),
|
||||
uploads: "https://uploads.github.com".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GitHub {
|
||||
pub at: Endpoints,
|
||||
/// `owner/name` of the registry repository.
|
||||
pub repo: String,
|
||||
pub client_id: String,
|
||||
pub client_secret: String,
|
||||
/// Fine-grained, this repository only: contents and pull requests.
|
||||
pub bot_token: String,
|
||||
agent: ureq::Agent,
|
||||
}
|
||||
|
||||
/// A failed call: `status` is GitHub's, 0 when it wasn't reached.
|
||||
#[derive(Debug)]
|
||||
pub struct Failure {
|
||||
pub status: u16,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
type Result<T> = std::result::Result<T, Failure>;
|
||||
|
||||
fn failure(e: ureq::Error) -> Failure {
|
||||
match e {
|
||||
ureq::Error::Status(status, response) => Failure {
|
||||
status,
|
||||
message: response
|
||||
.into_string()
|
||||
.unwrap_or_default()
|
||||
.chars()
|
||||
.take(300)
|
||||
.collect(),
|
||||
},
|
||||
other => Failure {
|
||||
status: 0,
|
||||
message: other.to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn read(response: ureq::Response) -> Result<Value> {
|
||||
response.into_json().map_err(|e| Failure {
|
||||
status: 0,
|
||||
message: e.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
impl GitHub {
|
||||
pub fn new(
|
||||
at: Endpoints,
|
||||
repo: String,
|
||||
client_id: String,
|
||||
client_secret: String,
|
||||
bot_token: String,
|
||||
) -> GitHub {
|
||||
let agent = ureq::AgentBuilder::new()
|
||||
.timeout(Duration::from_secs(60))
|
||||
.user_agent("horizon-mod-registry")
|
||||
.build();
|
||||
GitHub {
|
||||
at,
|
||||
repo,
|
||||
client_id,
|
||||
client_secret,
|
||||
bot_token,
|
||||
agent,
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the sign-in starts.
|
||||
pub fn authorize_url(&self, redirect: &str, state: &str) -> String {
|
||||
format!(
|
||||
"{}/login/oauth/authorize?client_id={}&redirect_uri={}&state={state}&allow_signup=true",
|
||||
self.at.web,
|
||||
self.client_id,
|
||||
encode(redirect)
|
||||
)
|
||||
}
|
||||
|
||||
/// The signed-in account (login, id) for an OAuth `code`. Its token is
|
||||
/// used once and dropped.
|
||||
pub fn sign_in(&self, code: &str, redirect: &str) -> Result<(String, u64)> {
|
||||
let token = self
|
||||
.agent
|
||||
.post(&format!("{}/login/oauth/access_token", self.at.web))
|
||||
.set("Accept", "application/json")
|
||||
.send_json(json!({
|
||||
"client_id": self.client_id,
|
||||
"client_secret": self.client_secret,
|
||||
"code": code,
|
||||
"redirect_uri": redirect,
|
||||
}))
|
||||
.map_err(failure)
|
||||
.and_then(read)?;
|
||||
let Some(token) = token["access_token"].as_str() else {
|
||||
return Err(Failure {
|
||||
status: 401,
|
||||
message: "GitHub refused the sign-in".into(),
|
||||
});
|
||||
};
|
||||
let user = self
|
||||
.agent
|
||||
.get(&format!("{}/user", self.at.api))
|
||||
.set("Authorization", &format!("Bearer {token}"))
|
||||
.set("Accept", "application/vnd.github+json")
|
||||
.call()
|
||||
.map_err(failure)
|
||||
.and_then(read)?;
|
||||
match (user["login"].as_str(), user["id"].as_u64()) {
|
||||
(Some(login), Some(id)) => Ok((login.to_owned(), id)),
|
||||
_ => Err(Failure {
|
||||
status: 0,
|
||||
message: "GitHub sent no login".into(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&self, method: &str, path: &str) -> ureq::Request {
|
||||
self.agent
|
||||
.request(
|
||||
method,
|
||||
&format!("{}/repos/{}{path}", self.at.api, self.repo),
|
||||
)
|
||||
.set("Authorization", &format!("Bearer {}", self.bot_token))
|
||||
.set("Accept", "application/vnd.github+json")
|
||||
.set("X-GitHub-Api-Version", "2022-11-28")
|
||||
}
|
||||
|
||||
/// `mods/<id>.toml` on main, with its blob sha; None when it isn't there.
|
||||
pub fn entry(&self, id: &str) -> Result<Option<(Entry, String)>> {
|
||||
let response = self
|
||||
.call("GET", &format!("/contents/mods/{id}.toml"))
|
||||
.query("ref", "main")
|
||||
.call();
|
||||
let file = match response {
|
||||
Ok(r) => read(r)?,
|
||||
Err(ureq::Error::Status(404, _)) => return Ok(None),
|
||||
Err(e) => return Err(failure(e)),
|
||||
};
|
||||
let bytes = file["content"]
|
||||
.as_str()
|
||||
.map(|c| c.replace('\n', ""))
|
||||
.and_then(|c| STANDARD.decode(c).ok())
|
||||
.unwrap_or_default();
|
||||
let entry = toml::from_str(&String::from_utf8_lossy(&bytes)).map_err(|e| Failure {
|
||||
status: 0,
|
||||
message: format!("mods/{id}.toml on main: {e}"),
|
||||
})?;
|
||||
Ok(Some((entry, file["sha"].as_str().unwrap_or("").to_owned())))
|
||||
}
|
||||
|
||||
/// Uploads `path` as asset `name` of the `staging` prerelease, unless it
|
||||
/// is already there (the same file submitted again).
|
||||
pub fn stage(&self, name: &str, path: &Path) -> Result<()> {
|
||||
let release = self
|
||||
.call("GET", "/releases/tags/staging")
|
||||
.call()
|
||||
.map_err(failure)
|
||||
.and_then(read)?;
|
||||
let there = release["assets"]
|
||||
.as_array()
|
||||
.is_some_and(|a| a.iter().any(|x| x["name"] == name));
|
||||
if there {
|
||||
return Ok(());
|
||||
}
|
||||
let id = release["id"].as_u64().unwrap_or(0);
|
||||
let bytes = std::fs::read(path).map_err(|e| Failure {
|
||||
status: 0,
|
||||
message: e.to_string(),
|
||||
})?;
|
||||
self.agent
|
||||
.post(&format!(
|
||||
"{}/repos/{}/releases/{id}/assets",
|
||||
self.at.uploads, self.repo
|
||||
))
|
||||
.query("name", name)
|
||||
.set("Authorization", &format!("Bearer {}", self.bot_token))
|
||||
.set("Accept", "application/vnd.github+json")
|
||||
.set("Content-Type", "application/octet-stream")
|
||||
.send_bytes(&bytes)
|
||||
.map_err(failure)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Makes branch `name` from main; false when it already exists.
|
||||
pub fn branch(&self, name: &str) -> Result<bool> {
|
||||
let main = self
|
||||
.call("GET", "/git/ref/heads/main")
|
||||
.call()
|
||||
.map_err(failure)
|
||||
.and_then(read)?;
|
||||
let sha = main["object"]["sha"].as_str().unwrap_or("");
|
||||
match self
|
||||
.call("POST", "/git/refs")
|
||||
.send_json(json!({"ref": format!("refs/heads/{name}"), "sha": sha}))
|
||||
{
|
||||
Ok(_) => Ok(true),
|
||||
Err(ureq::Error::Status(422, _)) => Ok(false),
|
||||
Err(e) => Err(failure(e)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes `path` on `branch` in one commit; `sha` is the blob it replaces.
|
||||
pub fn put(
|
||||
&self,
|
||||
path: &str,
|
||||
text: &str,
|
||||
branch: &str,
|
||||
sha: Option<&str>,
|
||||
message: &str,
|
||||
) -> Result<()> {
|
||||
let mut body = json!({
|
||||
"message": message,
|
||||
"content": STANDARD.encode(text),
|
||||
"branch": branch,
|
||||
});
|
||||
if let Some(sha) = sha {
|
||||
body["sha"] = json!(sha);
|
||||
}
|
||||
self.call("PUT", &format!("/contents/{path}"))
|
||||
.send_json(body)
|
||||
.map_err(failure)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Opens the pull request; its address.
|
||||
pub fn pull(&self, title: &str, head: &str, body: &str) -> Result<String> {
|
||||
let pr = self
|
||||
.call("POST", "/pulls")
|
||||
.send_json(json!({"title": title, "head": head, "base": "main", "body": body}))
|
||||
.map_err(failure)
|
||||
.and_then(read)?;
|
||||
Ok(pr["html_url"].as_str().unwrap_or("").to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
/// Percent-encodes a query value.
|
||||
fn encode(s: &str) -> String {
|
||||
s.bytes()
|
||||
.map(|b| match b {
|
||||
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
|
||||
(b as char).to_string()
|
||||
}
|
||||
_ => format!("%{b:02X}"),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -0,0 +1,675 @@
|
||||
//! `horizon-mod-registry`: the submission form's service at
|
||||
//! `playhorizon.dev/api/mods/*` (docs/api/mod-index.md). A creator signs in
|
||||
//! with GitHub, sends a file and its page; the service runs the file's own
|
||||
//! checks, uploads it to the registry's `staging` release and opens a pull
|
||||
//! request with the entry. Reviewers decide; merging publishes. It keeps no
|
||||
//! data: sessions are signed cookies, and GitHub holds everything else.
|
||||
pub mod github;
|
||||
|
||||
use axum::{
|
||||
Json, Router,
|
||||
extract::{DefaultBodyLimit, Multipart, Query, State},
|
||||
http::{HeaderMap, StatusCode, header},
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
routing::{get, post},
|
||||
};
|
||||
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
|
||||
use github::GitHub;
|
||||
use hmac::{Hmac, Mac};
|
||||
use horizon_mods::index::{
|
||||
self, Kind,
|
||||
registry::{self, Entry, EntryVersion, Sources},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use sha2::Sha256;
|
||||
use std::{
|
||||
collections::{HashMap, VecDeque},
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Mutex},
|
||||
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
/// How long a sign-in lasts.
|
||||
const SESSION: u64 = 12 * 3600;
|
||||
/// Submissions per account per hour, and from everyone.
|
||||
const PER_ACCOUNT: usize = 6;
|
||||
const PER_HOUR: usize = 60;
|
||||
/// The page's JSON.
|
||||
const MAX_FORM: usize = 64 << 10;
|
||||
|
||||
pub struct Config {
|
||||
/// `https://playhorizon.dev`: where the form is, and the only origin it
|
||||
/// takes submissions from.
|
||||
pub public_url: String,
|
||||
/// At least 32 bytes: signs sessions.
|
||||
pub session_key: Vec<u8>,
|
||||
pub github: GitHub,
|
||||
/// Where files wait during a submission.
|
||||
pub scratch: PathBuf,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// `REGISTRY_PUBLIC_URL`, `REGISTRY_SESSION_KEY`, `REGISTRY_REPO`,
|
||||
/// `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET`, `GITHUB_BOT_TOKEN`.
|
||||
pub fn from_env() -> Result<Config, String> {
|
||||
let var = |name: &str| std::env::var(name).map_err(|_| format!("{name} is not set"));
|
||||
let key = var("REGISTRY_SESSION_KEY")?;
|
||||
if key.len() < 32 {
|
||||
return Err("REGISTRY_SESSION_KEY needs at least 32 characters".into());
|
||||
}
|
||||
Ok(Config {
|
||||
public_url: var("REGISTRY_PUBLIC_URL")?.trim_end_matches('/').to_owned(),
|
||||
session_key: key.into_bytes(),
|
||||
github: GitHub::new(
|
||||
github::Endpoints::default(),
|
||||
std::env::var("REGISTRY_REPO").unwrap_or_else(|_| "ludovic111/horizon-mods".into()),
|
||||
var("GITHUB_CLIENT_ID")?,
|
||||
var("GITHUB_CLIENT_SECRET")?,
|
||||
var("GITHUB_BOT_TOKEN")?,
|
||||
),
|
||||
scratch: std::env::temp_dir().join("horizon-mod-registry"),
|
||||
})
|
||||
}
|
||||
|
||||
fn callback(&self) -> String {
|
||||
format!("{}/api/mods/auth/callback", self.public_url)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct App(Arc<Inner>);
|
||||
|
||||
struct Inner {
|
||||
config: Config,
|
||||
recent: Mutex<HashMap<String, VecDeque<Instant>>>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(config: Config) -> App {
|
||||
App(Arc::new(Inner {
|
||||
config,
|
||||
recent: Mutex::new(HashMap::new()),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn router(app: App) -> Router {
|
||||
Router::new()
|
||||
.route("/healthz", get(|| async { "ok" }))
|
||||
.route("/api/mods/auth/login", get(login))
|
||||
.route("/api/mods/auth/callback", get(callback))
|
||||
.route("/api/mods/auth/logout", post(logout))
|
||||
.route("/api/mods/me", get(me))
|
||||
.route(
|
||||
"/api/mods/submit",
|
||||
post(submit).layer(DefaultBodyLimit::max(
|
||||
index::MAX_FILE as usize + MAX_FORM * 2,
|
||||
)),
|
||||
)
|
||||
.with_state(app)
|
||||
}
|
||||
|
||||
/// An error players read in the form.
|
||||
#[derive(Debug)]
|
||||
pub struct Refusal(StatusCode, String);
|
||||
|
||||
impl Refusal {
|
||||
fn new(status: StatusCode, message: impl Into<String>) -> Refusal {
|
||||
Refusal(status, message.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for Refusal {
|
||||
fn into_response(self) -> Response {
|
||||
(self.0, Json(json!({"error": self.1}))).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
fn bad(message: impl Into<String>) -> Refusal {
|
||||
Refusal::new(StatusCode::BAD_REQUEST, message)
|
||||
}
|
||||
|
||||
fn github_down(e: github::Failure) -> Refusal {
|
||||
eprintln!("horizon-mod-registry: GitHub {}: {}", e.status, e.message);
|
||||
Refusal::new(
|
||||
StatusCode::BAD_GATEWAY,
|
||||
"GitHub didn't answer as expected: try again in a minute",
|
||||
)
|
||||
}
|
||||
|
||||
// Signed values: `<payload>.<HMAC-SHA256>`, both base64url.
|
||||
|
||||
fn sign(key: &[u8], payload: &str) -> String {
|
||||
let mut mac = Hmac::<Sha256>::new_from_slice(key).expect("any key length");
|
||||
mac.update(payload.as_bytes());
|
||||
format!(
|
||||
"{}.{}",
|
||||
URL_SAFE_NO_PAD.encode(payload),
|
||||
URL_SAFE_NO_PAD.encode(mac.finalize().into_bytes())
|
||||
)
|
||||
}
|
||||
|
||||
fn unsign(key: &[u8], value: &str) -> Option<String> {
|
||||
let (payload, tag) = value.split_once('.')?;
|
||||
let payload = String::from_utf8(URL_SAFE_NO_PAD.decode(payload).ok()?).ok()?;
|
||||
let mut mac = Hmac::<Sha256>::new_from_slice(key).ok()?;
|
||||
mac.update(payload.as_bytes());
|
||||
mac.verify_slice(&URL_SAFE_NO_PAD.decode(tag).ok()?).ok()?;
|
||||
Some(payload)
|
||||
}
|
||||
|
||||
fn now() -> u64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
fn random_hex() -> String {
|
||||
let mut bytes = [0u8; 16];
|
||||
getrandom::fill(&mut bytes).expect("the system's random numbers");
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
fn cookie(headers: &HeaderMap, name: &str) -> Option<String> {
|
||||
headers
|
||||
.get_all(header::COOKIE)
|
||||
.iter()
|
||||
.filter_map(|v| v.to_str().ok())
|
||||
.flat_map(|v| v.split(';'))
|
||||
.filter_map(|c| c.trim().split_once('='))
|
||||
.find(|(k, _)| *k == name)
|
||||
.map(|(_, v)| v.to_owned())
|
||||
}
|
||||
|
||||
fn set_cookie(name: &str, value: &str, max_age: u64) -> String {
|
||||
format!("{name}={value}; Path=/api/mods; HttpOnly; Secure; SameSite=Lax; Max-Age={max_age}")
|
||||
}
|
||||
|
||||
/// The signed-in account: (login, id).
|
||||
fn session(app: &Inner, headers: &HeaderMap) -> Option<(String, u64)> {
|
||||
let payload = unsign(&app.config.session_key, &cookie(headers, "hm_session")?)?;
|
||||
let mut parts = payload.split('\n');
|
||||
let (login, id, until) = (parts.next()?, parts.next()?, parts.next()?);
|
||||
(until.parse::<u64>().ok()? > now()).then(|| (login.to_owned(), id.parse().unwrap_or(0)))
|
||||
}
|
||||
|
||||
/// The form sends this with each submission: a page on another site can't
|
||||
/// read it, so it can't submit in a signed-in creator's name.
|
||||
fn csrf(app: &Inner, headers: &HeaderMap) -> Option<String> {
|
||||
let session = cookie(headers, "hm_session")?;
|
||||
let signed = sign(&app.config.session_key, &format!("csrf\n{session}"));
|
||||
Some(signed.rsplit('.').next()?.chars().take(32).collect())
|
||||
}
|
||||
|
||||
async fn login(State(app): State<App>) -> Response {
|
||||
let state = random_hex();
|
||||
let signed = sign(&app.0.config.session_key, &state);
|
||||
let url = app
|
||||
.0
|
||||
.config
|
||||
.github
|
||||
.authorize_url(&app.0.config.callback(), &state);
|
||||
(
|
||||
[(header::SET_COOKIE, set_cookie("hm_state", &signed, 600))],
|
||||
Redirect::to(&url),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Callback {
|
||||
code: String,
|
||||
state: String,
|
||||
}
|
||||
|
||||
async fn callback(
|
||||
State(app): State<App>,
|
||||
headers: HeaderMap,
|
||||
Query(q): Query<Callback>,
|
||||
) -> Result<Response, Refusal> {
|
||||
let expected = cookie(&headers, "hm_state").and_then(|v| unsign(&app.0.config.session_key, &v));
|
||||
if expected.as_deref() != Some(q.state.as_str()) {
|
||||
return Err(Refusal::new(
|
||||
StatusCode::FORBIDDEN,
|
||||
"the sign-in expired or didn't start here: try again",
|
||||
));
|
||||
}
|
||||
let inner = app.0.clone();
|
||||
let (login, id) = tokio::task::spawn_blocking(move || {
|
||||
inner
|
||||
.config
|
||||
.github
|
||||
.sign_in(&q.code, &inner.config.callback())
|
||||
})
|
||||
.await
|
||||
.map_err(|_| Refusal::new(StatusCode::INTERNAL_SERVER_ERROR, "sign-in failed"))?
|
||||
.map_err(github_down)?;
|
||||
let value = sign(
|
||||
&app.0.config.session_key,
|
||||
&format!("{login}\n{id}\n{}", now() + SESSION),
|
||||
);
|
||||
let back = format!("{}/mods/submit", app.0.config.public_url);
|
||||
let mut response = Redirect::to(&back).into_response();
|
||||
let headers = response.headers_mut();
|
||||
headers.append(
|
||||
header::SET_COOKIE,
|
||||
set_cookie("hm_session", &value, SESSION).parse().unwrap(),
|
||||
);
|
||||
headers.append(
|
||||
header::SET_COOKIE,
|
||||
set_cookie("hm_state", "", 0).parse().unwrap(),
|
||||
);
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
async fn logout() -> Response {
|
||||
(
|
||||
[(header::SET_COOKIE, set_cookie("hm_session", "", 0))],
|
||||
StatusCode::NO_CONTENT,
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
async fn me(
|
||||
State(app): State<App>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<Json<serde_json::Value>, Refusal> {
|
||||
let (login, _) = session(&app.0, &headers)
|
||||
.ok_or_else(|| Refusal::new(StatusCode::UNAUTHORIZED, "not signed in"))?;
|
||||
Ok(Json(
|
||||
json!({"login": login, "csrf": csrf(&app.0, &headers)}),
|
||||
))
|
||||
}
|
||||
|
||||
/// The page a submission fills in: a project's fields, and the version's.
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Form {
|
||||
pub id: String,
|
||||
pub kind: String,
|
||||
pub name: String,
|
||||
pub summary: String,
|
||||
#[serde(default)]
|
||||
pub description: String,
|
||||
#[serde(default)]
|
||||
pub authors: Vec<String>,
|
||||
pub license: String,
|
||||
pub source: String,
|
||||
/// `https://` only; an icon in the repository is a pull request by hand.
|
||||
#[serde(default)]
|
||||
pub icon: Option<String>,
|
||||
/// A new project's minisign public key.
|
||||
#[serde(default)]
|
||||
pub key: Option<String>,
|
||||
/// The `.minisig` text.
|
||||
#[serde(default)]
|
||||
pub signature: Option<String>,
|
||||
pub version: String,
|
||||
pub game: String,
|
||||
#[serde(default)]
|
||||
pub changelog: String,
|
||||
}
|
||||
|
||||
fn take(window: &mut VecDeque<Instant>, max: usize) -> bool {
|
||||
while window
|
||||
.front()
|
||||
.is_some_and(|t| t.elapsed() > Duration::from_secs(3600))
|
||||
{
|
||||
window.pop_front();
|
||||
}
|
||||
window.len() < max
|
||||
}
|
||||
|
||||
async fn submit(
|
||||
State(app): State<App>,
|
||||
headers: HeaderMap,
|
||||
mut multipart: Multipart,
|
||||
) -> Result<Json<serde_json::Value>, Refusal> {
|
||||
let inner = app.0.clone();
|
||||
let origin = headers.get(header::ORIGIN).and_then(|v| v.to_str().ok());
|
||||
if origin != Some(inner.config.public_url.as_str()) {
|
||||
return Err(Refusal::new(
|
||||
StatusCode::FORBIDDEN,
|
||||
"submit from the registry's form",
|
||||
));
|
||||
}
|
||||
let (login, user_id) = session(&inner, &headers)
|
||||
.ok_or_else(|| Refusal::new(StatusCode::UNAUTHORIZED, "sign in with GitHub first"))?;
|
||||
let sent = headers.get("x-csrf-token").and_then(|v| v.to_str().ok());
|
||||
if sent.is_none() || sent.map(str::to_owned) != csrf(&inner, &headers) {
|
||||
return Err(Refusal::new(
|
||||
StatusCode::FORBIDDEN,
|
||||
"reload the form and try again",
|
||||
));
|
||||
}
|
||||
{
|
||||
let mut recent = inner.recent.lock().unwrap();
|
||||
let ok = take(recent.entry(String::new()).or_default(), PER_HOUR)
|
||||
&& take(recent.entry(login.to_lowercase()).or_default(), PER_ACCOUNT);
|
||||
if !ok {
|
||||
return Err(Refusal::new(
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
"too many submissions this hour: try again later",
|
||||
));
|
||||
}
|
||||
let at = Instant::now();
|
||||
recent.entry(String::new()).or_default().push_back(at);
|
||||
recent
|
||||
.entry(login.to_lowercase())
|
||||
.or_default()
|
||||
.push_back(at);
|
||||
}
|
||||
let dir = inner.config.scratch.join(random_hex());
|
||||
std::fs::create_dir_all(&dir)
|
||||
.map_err(|e| Refusal::new(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
let result = receive(&mut multipart, &dir).await;
|
||||
let result = match result {
|
||||
Ok((form, file)) => {
|
||||
let inner = inner.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
publish(&inner.config, &login, user_id, &form, &file)
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
Err(Refusal::new(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"the check crashed",
|
||||
))
|
||||
})
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
result.map(|url| Json(json!({"pull_request": url})))
|
||||
}
|
||||
|
||||
/// Reads the form's two parts: `entry` (JSON) and `file`, streamed to `dir`.
|
||||
async fn receive(multipart: &mut Multipart, dir: &Path) -> Result<(Form, PathBuf), Refusal> {
|
||||
use tokio::io::AsyncWriteExt;
|
||||
let (mut form, mut file) = (None, None);
|
||||
while let Some(mut field) = multipart
|
||||
.next_field()
|
||||
.await
|
||||
.map_err(|e| bad(e.body_text()))?
|
||||
{
|
||||
match field.name() {
|
||||
Some("entry") => {
|
||||
let text = field.text().await.map_err(|e| bad(e.body_text()))?;
|
||||
if text.len() > MAX_FORM {
|
||||
return Err(bad("the page's text is too long"));
|
||||
}
|
||||
form = Some(serde_json::from_str::<Form>(&text).map_err(|e| bad(e.to_string()))?);
|
||||
}
|
||||
Some("file") => {
|
||||
let path = dir.join("upload");
|
||||
let mut out = tokio::fs::File::create(&path)
|
||||
.await
|
||||
.map_err(|e| Refusal::new(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
let mut size = 0u64;
|
||||
while let Some(chunk) = field.chunk().await.map_err(|e| bad(e.body_text()))? {
|
||||
size += chunk.len() as u64;
|
||||
if size > index::MAX_FILE {
|
||||
return Err(bad(format!(
|
||||
"the file is over {} MiB",
|
||||
index::MAX_FILE >> 20
|
||||
)));
|
||||
}
|
||||
out.write_all(&chunk).await.map_err(|e| {
|
||||
Refusal::new(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
|
||||
})?;
|
||||
}
|
||||
out.flush().await.ok();
|
||||
file = Some(path);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
match (form, file) {
|
||||
(Some(form), Some(file)) => Ok((form, file)),
|
||||
_ => Err(bad("send the page and the file")),
|
||||
}
|
||||
}
|
||||
|
||||
/// `YYYY-MM-DD` of a Unix time (UTC).
|
||||
fn date(secs: u64) -> String {
|
||||
// Howard Hinnant's days-to-civil.
|
||||
let z = (secs / 86400) as i64 + 719_468;
|
||||
let era = z.div_euclid(146_097);
|
||||
let doe = z - era * 146_097;
|
||||
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
|
||||
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
let mp = (5 * doy + 2) / 153;
|
||||
let d = doy - (153 * mp + 2) / 5 + 1;
|
||||
let m = if mp < 10 { mp + 3 } else { mp - 9 };
|
||||
let y = yoe + era * 400 + i64::from(m <= 2);
|
||||
format!("{y:04}-{m:02}-{d:02}")
|
||||
}
|
||||
|
||||
/// The submission, past its checks: the entry written on a new branch and a
|
||||
/// pull request opened. Returns the pull request's address.
|
||||
pub fn publish(
|
||||
config: &Config,
|
||||
login: &str,
|
||||
user_id: u64,
|
||||
form: &Form,
|
||||
file: &Path,
|
||||
) -> Result<String, Refusal> {
|
||||
let gh = &config.github;
|
||||
let kind = Kind::from(form.kind.clone());
|
||||
registry::open(&kind).map_err(bad)?;
|
||||
if !index::valid_id(&form.id) {
|
||||
return Err(bad("an id is 1 to 64 of a-z, 0-9 and _"));
|
||||
}
|
||||
if form
|
||||
.icon
|
||||
.as_ref()
|
||||
.is_some_and(|i| !i.starts_with("https://"))
|
||||
{
|
||||
return Err(bad("the icon must be an https:// address"));
|
||||
}
|
||||
if !form.source.trim().starts_with("https://") {
|
||||
return Err(bad("the source must be an https:// address of the code"));
|
||||
}
|
||||
if form.name.trim().is_empty() || form.license.trim().is_empty() {
|
||||
return Err(bad("a project needs a name and a licence"));
|
||||
}
|
||||
// The file's own checks first: a wrong file never reaches GitHub.
|
||||
let facts = index::inspect(&kind, file).map_err(bad)?;
|
||||
if let Some(id) = facts.id.as_ref().filter(|id| **id != form.id) {
|
||||
return Err(bad(format!("the file's id is {id:?}, not {:?}", form.id)));
|
||||
}
|
||||
if let Some(v) = facts
|
||||
.version
|
||||
.as_ref()
|
||||
.filter(|v| **v != form.version.trim())
|
||||
{
|
||||
return Err(bad(format!(
|
||||
"the file says version {v:?}, not {:?}",
|
||||
form.version.trim()
|
||||
)));
|
||||
}
|
||||
let sha256 = index::sha256_file(file).map_err(bad)?;
|
||||
let extension = kind.extension().unwrap_or("zip");
|
||||
let version = EntryVersion {
|
||||
version: form.version.trim().to_owned(),
|
||||
published: date(now()),
|
||||
game: form.game.trim().to_owned(),
|
||||
file: format!("{}-{}.{extension}", form.id, form.version.trim()),
|
||||
sha256: sha256.clone(),
|
||||
signature: form.signature.clone().filter(|s| !s.trim().is_empty()),
|
||||
changelog: form.changelog.clone(),
|
||||
};
|
||||
let existing = gh.entry(&form.id).map_err(github_down)?;
|
||||
let is_new = existing.is_none();
|
||||
let (entry, blob) = match existing {
|
||||
None => (
|
||||
Entry {
|
||||
id: form.id.clone(),
|
||||
kind: kind.clone(),
|
||||
name: form.name.trim().to_owned(),
|
||||
summary: form.summary.trim().to_owned(),
|
||||
description: form.description.clone(),
|
||||
authors: form.authors.clone(),
|
||||
owners: vec![login.to_owned()],
|
||||
license: form.license.trim().to_owned(),
|
||||
source: form.source.trim().to_owned(),
|
||||
icon: form.icon.clone(),
|
||||
key: form.key.clone().filter(|k| !k.trim().is_empty()),
|
||||
versions: vec![version.clone()],
|
||||
},
|
||||
None,
|
||||
),
|
||||
Some((mut entry, blob)) => {
|
||||
if !entry.owners.iter().any(|o| o.eq_ignore_ascii_case(login)) {
|
||||
return Err(Refusal::new(
|
||||
StatusCode::FORBIDDEN,
|
||||
format!(
|
||||
"{} is {}'s: only its owners publish its updates",
|
||||
form.id,
|
||||
entry.owners.join(", ")
|
||||
),
|
||||
));
|
||||
}
|
||||
if entry.kind != kind {
|
||||
return Err(bad(format!("{} is a {}", form.id, entry.kind.name())));
|
||||
}
|
||||
if entry.versions.iter().any(|v| v.version == version.version) {
|
||||
return Err(Refusal::new(
|
||||
StatusCode::CONFLICT,
|
||||
format!(
|
||||
"{} {} is already published: give this file a new version",
|
||||
form.id, version.version
|
||||
),
|
||||
));
|
||||
}
|
||||
let key = form.key.clone().filter(|k| !k.trim().is_empty());
|
||||
if key.is_some() && key != entry.key {
|
||||
return Err(bad(
|
||||
"changing a project's key is a pull request by hand, reviewed on its own",
|
||||
));
|
||||
}
|
||||
entry.name = form.name.trim().to_owned();
|
||||
entry.summary = form.summary.trim().to_owned();
|
||||
entry.description = form.description.clone();
|
||||
entry.authors = form.authors.clone();
|
||||
entry.license = form.license.trim().to_owned();
|
||||
entry.source = form.source.trim().to_owned();
|
||||
if form.icon.is_some() {
|
||||
entry.icon = form.icon.clone();
|
||||
}
|
||||
entry.versions.insert(0, version.clone());
|
||||
(entry, Some(blob))
|
||||
}
|
||||
};
|
||||
// Every check CI runs but the trial run: the file as its kind, its id and
|
||||
// version, its signature, the entry's fields. CI runs it all again.
|
||||
let files = file.parent().unwrap_or(Path::new("."));
|
||||
std::fs::copy(file, files.join(&sha256)).map_err(|e| bad(e.to_string()))?;
|
||||
let mut checked = entry.clone();
|
||||
checked.versions.truncate(1);
|
||||
if checked
|
||||
.icon
|
||||
.as_ref()
|
||||
.is_some_and(|i| !i.starts_with("https://"))
|
||||
{
|
||||
checked.icon = None;
|
||||
}
|
||||
let project = registry::project(
|
||||
&checked,
|
||||
&Sources {
|
||||
registry: files,
|
||||
files,
|
||||
release: index::FILE_HOST,
|
||||
site: "https://example.invalid/",
|
||||
generated: String::new(),
|
||||
},
|
||||
)
|
||||
.map_err(|errors| bad(errors.join("; ")))?;
|
||||
let text = entry.to_toml().map_err(bad)?;
|
||||
gh.stage(&format!("{sha256}.{extension}"), file)
|
||||
.map_err(github_down)?;
|
||||
let branch = format!("submit/{}-{}", form.id, version.version);
|
||||
if !gh.branch(&branch).map_err(github_down)? {
|
||||
return Err(Refusal::new(
|
||||
StatusCode::CONFLICT,
|
||||
format!(
|
||||
"{} {} was already submitted: its pull request is open",
|
||||
form.id, version.version
|
||||
),
|
||||
));
|
||||
}
|
||||
let what = if is_new { "Add" } else { "Update" };
|
||||
let message = format!(
|
||||
"{what} {} {}\n\nSubmitted by @{login} through the form.\n\nCo-authored-by: {login} <{user_id}+{login}@users.noreply.github.com>",
|
||||
form.id, version.version
|
||||
);
|
||||
gh.put(
|
||||
&format!("mods/{}.toml", form.id),
|
||||
&text,
|
||||
&branch,
|
||||
blob.as_deref(),
|
||||
&message,
|
||||
)
|
||||
.map_err(github_down)?;
|
||||
let v = &project.versions[0];
|
||||
let capabilities: Vec<String> = v
|
||||
.capabilities
|
||||
.iter()
|
||||
.map(|c| format!("- may {}", c.describe()))
|
||||
.collect();
|
||||
let requires: Vec<String> = v
|
||||
.requires
|
||||
.iter()
|
||||
.map(|r| format!("- needs {} {}", r.id, r.version))
|
||||
.collect();
|
||||
let body = format!(
|
||||
"<!-- submitted-by: {login} -->\nSubmitted by @{login} through [the form]({public}/mods/submit).\n\n\
|
||||
| | |\n|---|---|\n| Project | `{id}` ({kind}, {new}) |\n| Version | {version} for {game} |\n| File | `{file}`, {size} bytes, SHA-256 `{sha256}` |\n| Signed | {signed} |\n| Source | {source} |\n\n{lists}\n\n\
|
||||
The checks below run the file's own checks again and, for a mod, a trial run in the sandbox. \
|
||||
Reviewers: read the source and the checks' report before you approve.",
|
||||
public = config.public_url,
|
||||
id = form.id,
|
||||
kind = kind.name(),
|
||||
new = if is_new { "new" } else { "update" },
|
||||
version = v.version,
|
||||
game = v.game,
|
||||
file = v.file.name,
|
||||
size = v.file.size,
|
||||
signed = if entry.key.is_some() {
|
||||
"yes, with the project's key"
|
||||
} else {
|
||||
"no"
|
||||
},
|
||||
source = entry.source,
|
||||
lists = [capabilities, requires].concat().join("\n"),
|
||||
);
|
||||
gh.pull(
|
||||
&format!("{what} {} {}", form.id, version.version),
|
||||
&branch,
|
||||
&body,
|
||||
)
|
||||
.map_err(github_down)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn dates_and_signatures() {
|
||||
assert_eq!(date(0), "1970-01-01");
|
||||
assert_eq!(date(1_792_800_000), "2026-10-24");
|
||||
let key = b"0123456789abcdef0123456789abcdef";
|
||||
let signed = sign(key, "alice\n7\n99");
|
||||
assert_eq!(unsign(key, &signed).as_deref(), Some("alice\n7\n99"));
|
||||
assert_eq!(unsign(b"another key, another key, another", &signed), None);
|
||||
let forged = format!(
|
||||
"{}.{}",
|
||||
URL_SAFE_NO_PAD.encode("mallory\n8\n99"),
|
||||
signed.split_once('.').unwrap().1
|
||||
);
|
||||
assert_eq!(unsign(key, &forged), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//! `horizon-mod-registry`: serves on `PORT` (default 8080). Configured by the
|
||||
//! environment (see `Config::from_env`); keeps no data.
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use horizon_mod_registry::{App, Config, router};
|
||||
|
||||
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn main() {
|
||||
let port: u16 = std::env::var("PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse().ok())
|
||||
.unwrap_or(8080);
|
||||
let config = Config::from_env().unwrap_or_else(|e| panic!("horizon-mod-registry: {e}"));
|
||||
let repo = config.github.repo.clone();
|
||||
let listener = tokio::net::TcpListener::bind(SocketAddr::from(([0, 0, 0, 0], port)))
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("port {port}: {e}"));
|
||||
eprintln!("horizon-mod-registry: listening on {port}, submitting to {repo}");
|
||||
axum::serve(listener, router(App::new(config)))
|
||||
.with_graceful_shutdown(async {
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("{e}"));
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
//! The whole submission against a mock GitHub: sign in, submit the sample
|
||||
//! signed mod (`crates/horizon-mods/tests/index`), and what lands on GitHub;
|
||||
//! then what is refused.
|
||||
use axum::{
|
||||
Json, Router,
|
||||
body::{Body, Bytes},
|
||||
extract::{Path as UrlPath, Query, State},
|
||||
http::{Request, StatusCode, header},
|
||||
routing::{get, post},
|
||||
};
|
||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||
use horizon_mod_registry::{App, Config, github, router};
|
||||
use horizon_mods::index::registry::Entry;
|
||||
use http_body_util::BodyExt;
|
||||
use serde_json::{Value, json};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use tower::ServiceExt;
|
||||
|
||||
const PUBLIC: &str = "https://playhorizon.test";
|
||||
const HMOD_SHA: &str = "e1135345692f6dd8fd4c51920151944b82333e17049c9bb7d756f27b8deefc53";
|
||||
|
||||
#[derive(Default)]
|
||||
struct Mock {
|
||||
/// Path -> text on main.
|
||||
main: HashMap<String, String>,
|
||||
assets: Vec<(String, usize)>,
|
||||
branches: Vec<String>,
|
||||
/// (branch, path) -> (text, message, replaced blob).
|
||||
commits: HashMap<(String, String), (String, String, Option<String>)>,
|
||||
pulls: Vec<Value>,
|
||||
/// The account the next sign-in gets.
|
||||
login: String,
|
||||
}
|
||||
|
||||
type Shared = Arc<Mutex<Mock>>;
|
||||
|
||||
async fn mock_github() -> (String, Shared) {
|
||||
let state: Shared = Arc::default();
|
||||
state.lock().unwrap().login = "alice".into();
|
||||
let app = Router::new()
|
||||
.route(
|
||||
"/login/oauth/access_token",
|
||||
post(|Json(body): Json<Value>| async move {
|
||||
Json(if body["code"] == "good" {
|
||||
json!({"access_token": "gho_user"})
|
||||
} else {
|
||||
json!({"error": "bad_verification_code"})
|
||||
})
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/user",
|
||||
get(|State(s): State<Shared>| async move {
|
||||
Json(json!({"login": s.lock().unwrap().login, "id": 7}))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/repos/o/r/contents/mods/{file}",
|
||||
get(
|
||||
|State(s): State<Shared>, UrlPath(file): UrlPath<String>| async move {
|
||||
let s = s.lock().unwrap();
|
||||
match s.main.get(&format!("mods/{file}")) {
|
||||
Some(text) => Ok(Json(json!({"content": STANDARD.encode(text), "sha": "blob1"}))),
|
||||
None => Err(StatusCode::NOT_FOUND),
|
||||
}
|
||||
},
|
||||
)
|
||||
.put(
|
||||
|State(s): State<Shared>, UrlPath(file): UrlPath<String>, Json(b): Json<Value>| async move {
|
||||
let text = String::from_utf8(STANDARD.decode(b["content"].as_str().unwrap()).unwrap()).unwrap();
|
||||
let key = (b["branch"].as_str().unwrap().to_owned(), format!("mods/{file}"));
|
||||
let value = (text, b["message"].as_str().unwrap().to_owned(), b["sha"].as_str().map(str::to_owned));
|
||||
s.lock().unwrap().commits.insert(key, value);
|
||||
Json(json!({}))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/repos/o/r/releases/tags/staging",
|
||||
get(|State(s): State<Shared>| async move {
|
||||
let names: Vec<Value> = s.lock().unwrap().assets.iter().map(|(n, _)| json!({"name": n})).collect();
|
||||
Json(json!({"id": 42, "assets": names}))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/repos/o/r/releases/42/assets",
|
||||
post(
|
||||
|State(s): State<Shared>, Query(q): Query<HashMap<String, String>>, body: Bytes| async move {
|
||||
s.lock().unwrap().assets.push((q["name"].clone(), body.len()));
|
||||
Json(json!({}))
|
||||
},
|
||||
),
|
||||
)
|
||||
.route(
|
||||
"/repos/o/r/git/ref/heads/main",
|
||||
get(|| async { Json(json!({"object": {"sha": "abc123"}})) }),
|
||||
)
|
||||
.route(
|
||||
"/repos/o/r/git/refs",
|
||||
post(|State(s): State<Shared>, Json(b): Json<Value>| async move {
|
||||
assert_eq!(b["sha"], "abc123");
|
||||
let name = b["ref"].as_str().unwrap().trim_start_matches("refs/heads/").to_owned();
|
||||
let mut s = s.lock().unwrap();
|
||||
if s.branches.contains(&name) {
|
||||
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
s.branches.push(name);
|
||||
Ok(Json(json!({})))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/repos/o/r/pulls",
|
||||
post(|State(s): State<Shared>, Json(b): Json<Value>| async move {
|
||||
let mut s = s.lock().unwrap();
|
||||
s.pulls.push(b);
|
||||
Json(json!({"html_url": format!("https://github.com/o/r/pull/{}", s.pulls.len())}))
|
||||
}),
|
||||
)
|
||||
.with_state(state.clone());
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let base = format!("http://{}", listener.local_addr().unwrap());
|
||||
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
|
||||
(base, state)
|
||||
}
|
||||
|
||||
fn fixtures() -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../horizon-mods/tests/index")
|
||||
}
|
||||
|
||||
struct Client {
|
||||
app: Router,
|
||||
cookies: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
async fn send(&mut self, request: Request<Body>) -> (StatusCode, Value, Option<String>) {
|
||||
let response = self.app.clone().oneshot(request).await.unwrap();
|
||||
let status = response.status();
|
||||
for c in response.headers().get_all(header::SET_COOKIE) {
|
||||
let (pair, _) = c.to_str().unwrap().split_once(';').unwrap();
|
||||
let (k, v) = pair.split_once('=').unwrap();
|
||||
self.cookies.insert(k.into(), v.into());
|
||||
}
|
||||
let location = response
|
||||
.headers()
|
||||
.get(header::LOCATION)
|
||||
.map(|l| l.to_str().unwrap().to_owned());
|
||||
let bytes = response.into_body().collect().await.unwrap().to_bytes();
|
||||
(
|
||||
status,
|
||||
serde_json::from_slice(&bytes).unwrap_or(Value::Null),
|
||||
location,
|
||||
)
|
||||
}
|
||||
|
||||
fn cookie_header(&self) -> String {
|
||||
self.cookies
|
||||
.iter()
|
||||
.filter(|(_, v)| !v.is_empty())
|
||||
.map(|(k, v)| format!("{k}={v}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("; ")
|
||||
}
|
||||
|
||||
fn get(&self, uri: &str) -> Request<Body> {
|
||||
Request::get(uri)
|
||||
.header(header::COOKIE, self.cookie_header())
|
||||
.body(Body::empty())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn sign_in(&mut self) -> String {
|
||||
let (status, _, location) = self.send(self.get("/api/mods/auth/login")).await;
|
||||
assert_eq!(status, StatusCode::SEE_OTHER);
|
||||
let location = location.unwrap();
|
||||
assert!(location.contains(
|
||||
"redirect_uri=https%3A%2F%2Fplayhorizon.test%2Fapi%2Fmods%2Fauth%2Fcallback"
|
||||
));
|
||||
let state = location
|
||||
.split("state=")
|
||||
.nth(1)
|
||||
.unwrap()
|
||||
.split('&')
|
||||
.next()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
// Another state than the one this browser started with is refused.
|
||||
let (status, ..) = self
|
||||
.send(self.get("/api/mods/auth/callback?code=good&state=other"))
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::FORBIDDEN);
|
||||
let (status, _, location) = self
|
||||
.send(self.get(&format!("/api/mods/auth/callback?code=good&state={state}")))
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::SEE_OTHER);
|
||||
assert_eq!(
|
||||
location.as_deref(),
|
||||
Some("https://playhorizon.test/mods/submit")
|
||||
);
|
||||
let (status, me, _) = self.send(self.get("/api/mods/me")).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
me["csrf"].as_str().unwrap().to_owned()
|
||||
}
|
||||
|
||||
async fn submit(&mut self, csrf: &str, form: &Value, file: &[u8]) -> (StatusCode, Value) {
|
||||
let boundary = "XhorizonX";
|
||||
let mut body = format!(
|
||||
"--{boundary}\r\nContent-Disposition: form-data; name=\"entry\"\r\n\r\n{form}\r\n--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"f\"\r\nContent-Type: application/octet-stream\r\n\r\n"
|
||||
)
|
||||
.into_bytes();
|
||||
body.extend_from_slice(file);
|
||||
body.extend_from_slice(format!("\r\n--{boundary}--\r\n").as_bytes());
|
||||
let request = Request::post("/api/mods/submit")
|
||||
.header(header::COOKIE, self.cookie_header())
|
||||
.header(header::ORIGIN, PUBLIC)
|
||||
.header("x-csrf-token", csrf)
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
format!("multipart/form-data; boundary={boundary}"),
|
||||
)
|
||||
.body(Body::from(body))
|
||||
.unwrap();
|
||||
let (status, json, _) = self.send(request).await;
|
||||
(status, json)
|
||||
}
|
||||
}
|
||||
|
||||
fn form() -> Value {
|
||||
let key = std::fs::read_to_string(fixtures().join("test.pub")).unwrap();
|
||||
json!({
|
||||
"id": "coords_hud",
|
||||
"kind": "mod",
|
||||
"name": "Coordinates HUD",
|
||||
"summary": "Your coordinates in a corner.",
|
||||
"authors": ["Alice"],
|
||||
"license": "MIT",
|
||||
"source": "https://github.com/alice/coords",
|
||||
"key": key.lines().last().unwrap(),
|
||||
"signature": std::fs::read_to_string(fixtures().join("coords_hud-0.1.0.hmod.minisig")).unwrap(),
|
||||
"version": "0.1.0",
|
||||
"game": "26.3",
|
||||
})
|
||||
}
|
||||
|
||||
async fn setup() -> (Client, Shared) {
|
||||
let (base, mock) = mock_github().await;
|
||||
let scratch =
|
||||
std::env::temp_dir().join(format!("horizon-mod-registry-test-{}", std::process::id()));
|
||||
let config = Config {
|
||||
public_url: PUBLIC.into(),
|
||||
session_key: b"a test key of at least thirty-two bytes".to_vec(),
|
||||
github: github::GitHub::new(
|
||||
github::Endpoints {
|
||||
web: base.clone(),
|
||||
api: base.clone(),
|
||||
uploads: base,
|
||||
},
|
||||
"o/r".into(),
|
||||
"client".into(),
|
||||
"secret".into(),
|
||||
"bot".into(),
|
||||
),
|
||||
scratch,
|
||||
};
|
||||
let client = Client {
|
||||
app: router(App::new(config)),
|
||||
cookies: HashMap::new(),
|
||||
};
|
||||
(client, mock)
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn a_submission_becomes_a_pull_request() {
|
||||
let (mut client, mock) = setup().await;
|
||||
let file = std::fs::read(fixtures().join("coords_hud-0.1.0.hmod")).unwrap();
|
||||
// Not signed in.
|
||||
let (status, _) = client.submit("x", &form(), &file).await;
|
||||
assert_eq!(status, StatusCode::UNAUTHORIZED);
|
||||
let csrf = client.sign_in().await;
|
||||
let (status, _) = client.submit("wrong", &form(), &file).await;
|
||||
assert_eq!(status, StatusCode::FORBIDDEN);
|
||||
|
||||
let (status, json) = client.submit(&csrf, &form(), &file).await;
|
||||
assert_eq!(status, StatusCode::OK, "{json}");
|
||||
assert_eq!(json["pull_request"], "https://github.com/o/r/pull/1");
|
||||
{
|
||||
let m = mock.lock().unwrap();
|
||||
assert_eq!(m.assets, [(format!("{HMOD_SHA}.hmod"), file.len())]);
|
||||
assert_eq!(m.branches, ["submit/coords_hud-0.1.0"]);
|
||||
let (text, message, replaced) = &m.commits[&(
|
||||
"submit/coords_hud-0.1.0".into(),
|
||||
"mods/coords_hud.toml".into(),
|
||||
)];
|
||||
let entry: Entry = toml::from_str(text).unwrap();
|
||||
assert_eq!(entry.owners, ["alice"]);
|
||||
assert_eq!(entry.versions[0].sha256, HMOD_SHA);
|
||||
assert_eq!(entry.versions[0].file, "coords_hud-0.1.0.hmod");
|
||||
assert!(entry.versions[0].signature.is_some() && entry.key.is_some());
|
||||
assert!(message.contains("Co-authored-by: alice <7+alice@users.noreply.github.com>"));
|
||||
assert_eq!(replaced, &None);
|
||||
let body = m.pulls[0]["body"].as_str().unwrap();
|
||||
assert!(body.starts_with("<!-- submitted-by: alice -->"), "{body}");
|
||||
assert!(body.contains("may draw text on your screen"), "{body}");
|
||||
}
|
||||
// The same version again: its branch is there.
|
||||
let (status, json) = client.submit(&csrf, &form(), &file).await;
|
||||
assert_eq!(status, StatusCode::CONFLICT, "{json}");
|
||||
assert_eq!(mock.lock().unwrap().assets.len(), 1, "uploaded once");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn what_the_form_refuses() {
|
||||
let (mut client, mock) = setup().await;
|
||||
let file = std::fs::read(fixtures().join("coords_hud-0.1.0.hmod")).unwrap();
|
||||
let csrf = client.sign_in().await;
|
||||
let refused = |status: StatusCode, json: &Value, part: &str| {
|
||||
assert!(
|
||||
json["error"].as_str().unwrap_or("").contains(part),
|
||||
"{status} {json}"
|
||||
);
|
||||
};
|
||||
// A file that isn't what the page says.
|
||||
let mut f = form();
|
||||
f["id"] = json!("other_mod");
|
||||
let (status, json) = client.submit(&csrf, &f, &file).await;
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST);
|
||||
refused(status, &json, "the file's id is \"coords_hud\"");
|
||||
// A broken signature.
|
||||
let mut f = form();
|
||||
f["signature"] = json!(
|
||||
std::fs::read_to_string(fixtures().join("coords_hud-0.1.0.hmod.minisig"))
|
||||
.unwrap()
|
||||
.replace("RUTV", "RUTW")
|
||||
);
|
||||
let (status, json) = client.submit(&csrf, &f, &file).await;
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
|
||||
// Shader packs aren't open.
|
||||
let mut f = form();
|
||||
f["kind"] = json!("shader");
|
||||
let (status, json) = client.submit(&csrf, &f, &file).await;
|
||||
refused(status, &json, "shader packs open");
|
||||
// An update by someone who isn't an owner.
|
||||
let owned = "id = \"coords_hud\"\nkind = \"mod\"\nname = \"C\"\nsummary = \"s\"\nowners = [\"bob\"]\nlicense = \"MIT\"\nsource = \"https://x.test\"\n\n[[versions]]\nversion = \"0.0.1\"\npublished = \"2026-01-01\"\ngame = \"26.3\"\nfile = \"coords_hud-0.0.1.hmod\"\nsha256 = \"0000000000000000000000000000000000000000000000000000000000000000\"\n";
|
||||
mock.lock()
|
||||
.unwrap()
|
||||
.main
|
||||
.insert("mods/coords_hud.toml".into(), owned.into());
|
||||
let (status, json) = client.submit(&csrf, &form(), &file).await;
|
||||
assert_eq!(status, StatusCode::FORBIDDEN);
|
||||
refused(status, &json, "bob's");
|
||||
// From another site.
|
||||
let mut request = Request::post("/api/mods/submit")
|
||||
.header(header::COOKIE, client.cookie_header())
|
||||
.header(header::ORIGIN, "https://evil.test")
|
||||
.header("x-csrf-token", &csrf)
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
request.headers_mut().insert(
|
||||
header::CONTENT_TYPE,
|
||||
"multipart/form-data; boundary=x".parse().unwrap(),
|
||||
);
|
||||
let (status, ..) = client.send(request).await;
|
||||
assert_eq!(status, StatusCode::FORBIDDEN);
|
||||
let m = mock.lock().unwrap();
|
||||
assert!(
|
||||
m.assets.is_empty() && m.pulls.is_empty(),
|
||||
"nothing reached GitHub"
|
||||
);
|
||||
}
|
||||
@@ -129,6 +129,8 @@ reason = "Sent chat messages it didn't declare."
|
||||
date = "2026-10-24"
|
||||
```
|
||||
|
||||
The submission form's service (`crates/horizon-mod-registry`) writes these entries: it signs the creator in with GitHub, runs the file's checks, uploads the file to the repository's `staging` release as `<sha256>.<extension>` and opens the pull request. Only an entry's `owners` publish updates through it.
|
||||
|
||||
Unknown keys are refused. Its CI runs `cargo-horizon-mod` (`crates/cargo-horizon-mod`):
|
||||
|
||||
| Command | What it does |
|
||||
|
||||
Reference in New Issue
Block a user