mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-19 02:24:26 +01:00
merge: reconcile paid hosting with latest main
This commit is contained in:
commit
ded6f905fd
180 files changed
+21709
-3614
No files matched your search
@@ -4,6 +4,7 @@ import CredentialModel from "../core/model/credentials/credentials.model";
|
||||
import mongoose, { ConnectOptions } from "mongoose";
|
||||
import Repository from "../core/Repository";
|
||||
import config from "../config";
|
||||
import { initializeOwnerAccessAlerts } from "../core/owner-notifications";
|
||||
import AnonymizedRepositoryModel from "../core/model/anonymizedRepositories/anonymizedRepositories.model";
|
||||
import AnonymousError from "../core/AnonymousError";
|
||||
import AnonymizedPullRequestModel from "../core/model/anonymizedPullRequests/anonymizedPullRequests.model";
|
||||
@@ -34,6 +35,7 @@ export async function connect(overrides: Pick<ConnectOptions, "appName" | "maxPo
|
||||
await mongoose.connect(getMongoUrl(), options);
|
||||
await CredentialModel.createIndexes();
|
||||
if (config.GITHUB_APP_ENABLED) await InstallationModel.createIndexes();
|
||||
await initializeOwnerAccessAlerts();
|
||||
isConnected = true;
|
||||
|
||||
return database;
|
||||
|
||||
+55
-13
@@ -1,5 +1,7 @@
|
||||
import { startBillingMaintenance } from "../core/billing/maintenance";
|
||||
import billing, { billingWebhook } from "./routes/billing";
|
||||
import { sendRequestRateLimit } from "./request-rate-limit";
|
||||
import { installFatalErrorLogging } from "../core/process-monitoring";
|
||||
import { githubAppRouter, githubAppWebhook } from "./routes/github-app";
|
||||
import { config as dotenv } from "dotenv";
|
||||
dotenv();
|
||||
@@ -16,7 +18,10 @@ import * as passport from "passport";
|
||||
import { connect } from "./database";
|
||||
import { startTemporaryStorageMaintenance } from "../core/temporary-storage";
|
||||
import { initSession, router as connectionRouter } from "./routes/connection";
|
||||
import maintenanceRouter from "./routes/maintenance";
|
||||
import maintenanceTokens from "./routes/maintenance-tokens";
|
||||
import { bearerTokenAuth } from "./routes/token-auth";
|
||||
import { tokenAttemptLimitOptions, maintenanceRefreshLimitOptions } from "./routes/maintenance-limits";
|
||||
import router from "./routes";
|
||||
import {
|
||||
conferenceStatusCheck,
|
||||
@@ -39,11 +44,18 @@ import config from "../config";
|
||||
import { resolveTrustProxy } from "./trustProxy";
|
||||
import { requestRateLimitKey } from "./rate-limit-key";
|
||||
import { createLogger, serializeError } from "../core/logger";
|
||||
import { serverOptions } from "./server-options";
|
||||
import { monitorRequests } from "../core/request-monitoring";
|
||||
import { startPerformanceMonitoring } from "../core/performance-monitoring";
|
||||
import { getAnonymizationPoolStats } from "../core/anonymization-pool";
|
||||
|
||||
import { createReviewCapabilities } from "./service/review-capabilities";
|
||||
|
||||
import { createReviewConsentPage, isReviewConsentPagePath } from "./review-consent-page";
|
||||
|
||||
const reviewConsentPage = createReviewConsentPage();
|
||||
const logger = createLogger("server");
|
||||
installFatalErrorLogging("api");
|
||||
|
||||
// Lazily build the templated index.html on first request so the server
|
||||
// works even when started before `gulp` finishes.
|
||||
@@ -80,6 +92,7 @@ function getIndexHtml(): string {
|
||||
}
|
||||
|
||||
function indexResponse(req: express.Request, res: express.Response) {
|
||||
if (isReviewConsentPagePath(req.path)) return reviewConsentPage(req, res);
|
||||
if (
|
||||
req.path.startsWith("/script") ||
|
||||
req.path.startsWith("/css") ||
|
||||
@@ -106,8 +119,10 @@ function indexResponse(req: express.Request, res: express.Response) {
|
||||
export default async function start() {
|
||||
const app = express();
|
||||
app.use(monitorRequests("api"));
|
||||
app.use(serverOptions);
|
||||
startPerformanceMonitoring("api", getAnonymizationPoolStats);
|
||||
app.set("query parser", "extended");
|
||||
app.use("/service", createReviewCapabilities(process.env.REVIEW_SERVICE_KEYS));
|
||||
app.use("/github/app/webhook", githubAppWebhook);
|
||||
app.use("/api/billing/webhook", billingWebhook);
|
||||
app.use(express.json());
|
||||
@@ -140,12 +155,6 @@ export default async function start() {
|
||||
// client-spoofable header.
|
||||
app.set("trust proxy", resolveTrustProxy(config.TRUST_PROXY));
|
||||
|
||||
// handle session and connection
|
||||
app.use(initSession());
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
app.use(bearerTokenAuth);
|
||||
|
||||
startWorker();
|
||||
|
||||
const redisClient = createClient({
|
||||
@@ -160,6 +169,32 @@ export default async function start() {
|
||||
|
||||
await redisClient.connect();
|
||||
|
||||
// Charge failed and forbidden bearer requests before their database lookup.
|
||||
app.use(rateLimit({
|
||||
...tokenAttemptLimitOptions,
|
||||
store: new RedisStore({
|
||||
prefix: "token-attempt:",
|
||||
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
|
||||
}),
|
||||
}));
|
||||
// Bearer attempts are limited before session deserialization as well.
|
||||
app.use(initSession());
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
app.use(bearerTokenAuth);
|
||||
app.use(rateLimit({
|
||||
...maintenanceRefreshLimitOptions,
|
||||
store: new RedisStore({
|
||||
prefix: "maintenance-refresh:",
|
||||
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
|
||||
}),
|
||||
}));
|
||||
|
||||
// All maintenance tokens for an account share a budget across client IPs.
|
||||
const apiRateKey = (req: express.Request, res: express.Response) =>
|
||||
res.locals.maintenanceOwnerId
|
||||
? `maintenance:${res.locals.maintenanceOwnerId}`
|
||||
: requestRateLimitKey(req);
|
||||
const rate = rateLimit({
|
||||
store: new RedisStore({
|
||||
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
|
||||
@@ -168,7 +203,7 @@ export default async function start() {
|
||||
skip: async (request: express.Request, _response: express.Response) => {
|
||||
try {
|
||||
const user = await getUser(request);
|
||||
if (user && user.isAdmin) return true;
|
||||
if (user && user.isAdmin && !_response.locals.maintenanceToken) return true;
|
||||
} catch {
|
||||
// ignore: user not connected
|
||||
}
|
||||
@@ -184,26 +219,24 @@ export default async function start() {
|
||||
// if not logged in, limit to half the rate
|
||||
return config.RATE_LIMIT / 2;
|
||||
},
|
||||
keyGenerator: requestRateLimitKey,
|
||||
keyGenerator: apiRateKey,
|
||||
standardHeaders: "draft-6",
|
||||
legacyHeaders: false,
|
||||
message: (_request: express.Request, _response: express.Response) => {
|
||||
return `You can only make ${config.RATE_LIMIT} requests every 15min. Please try again later.`;
|
||||
},
|
||||
handler: sendRequestRateLimit,
|
||||
});
|
||||
const speedLimiter = slowDown({
|
||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||
delayAfter: 50,
|
||||
delayMs: () => 150,
|
||||
maxDelayMs: 5000,
|
||||
keyGenerator: requestRateLimitKey,
|
||||
keyGenerator: apiRateKey,
|
||||
});
|
||||
const webViewSpeedLimiter = slowDown({
|
||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||
delayAfter: 200,
|
||||
delayMs: () => 150,
|
||||
maxDelayMs: 5000,
|
||||
keyGenerator: requestRateLimitKey,
|
||||
keyGenerator: apiRateKey,
|
||||
});
|
||||
|
||||
app.use("/github", rate, speedLimiter, githubAppRouter);
|
||||
@@ -214,6 +247,8 @@ export default async function start() {
|
||||
app.use("/api", rate, apiRouter);
|
||||
|
||||
apiRouter.use("/billing", billing);
|
||||
apiRouter.use("/maintenance", speedLimiter, maintenanceRouter);
|
||||
apiRouter.use("/user/maintenance-tokens", maintenanceTokens);
|
||||
apiRouter.use("/admin", router.admin);
|
||||
apiRouter.use("/options", router.option);
|
||||
apiRouter.use("/conferences", router.conference);
|
||||
@@ -283,6 +318,13 @@ export default async function start() {
|
||||
app.use(
|
||||
express.static(join("public"), {
|
||||
etag: true,
|
||||
setHeaders(res, filePath) {
|
||||
// Sandboxed Markdown pages have an opaque origin; public math fonts
|
||||
// must permit anonymous cross-origin requests without relaxing CSP.
|
||||
if (/[/\\]css[/\\]fonts[/\\]KaTeX_[^/\\]+\.(?:woff2?|ttf)$/.test(filePath)) {
|
||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||
}
|
||||
},
|
||||
lastModified: true,
|
||||
maxAge: 86400000, // 1 day (fonts, images, partials)
|
||||
index: false, // don't serve index.html for "/" — indexResponse handles it
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Request, Response } from "express";
|
||||
|
||||
/** Give clients a retry time instead of an unparseable plain-text failure. */
|
||||
export function sendRequestRateLimit(req: Request, res: Response) {
|
||||
const rate = (req as Request & { rateLimit?: { resetTime?: Date } }).rateLimit;
|
||||
const resetAt = rate?.resetTime?.getTime() || Date.now() + 15 * 60_000;
|
||||
res.setHeader("Retry-After", String(Math.max(1, Math.ceil((resetAt - Date.now()) / 1000))));
|
||||
res.status(429).json({ error: "rate_limited", resetAt });
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { ReviewCallbacks } from "./service/review-callbacks";
|
||||
import { Request, Response } from "express";
|
||||
import { existsSync, readFileSync } from "fs";
|
||||
import { resolve } from "path";
|
||||
|
||||
// Reserve aliases too: they must never fall through to the ordinary page,
|
||||
// which loads third-party scripts. Only the canonical URL serves this page.
|
||||
export function isReviewConsentPagePath(path: string): boolean {
|
||||
try {
|
||||
return (
|
||||
decodeURIComponent(path).toLowerCase().replace(/\/+$/, "") ===
|
||||
"/review-link"
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function createReviewConsentPage(
|
||||
manifestPath = resolve("public", "asset-manifest.json"),
|
||||
callbacks?: ReviewCallbacks,
|
||||
) {
|
||||
return (req: Request, res: Response): void => {
|
||||
res.set({
|
||||
"Cache-Control": "no-store",
|
||||
"Referrer-Policy": "no-referrer",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"X-Frame-Options": "DENY",
|
||||
"Content-Security-Policy":
|
||||
"default-src 'self'; script-src 'self'; connect-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; font-src 'self' data:; frame-src 'none'; object-src 'none'; base-uri 'none'; form-action " + (callbacks?.formAction || "'self'") + "; frame-ancestors 'none'",
|
||||
});
|
||||
if (
|
||||
req.originalUrl !== "/review-link" ||
|
||||
(req.method !== "GET" && req.method !== "HEAD")
|
||||
) {
|
||||
res.status(404).json({ error: "not_found" });
|
||||
return;
|
||||
}
|
||||
let manifest: Record<string, unknown> = {};
|
||||
try {
|
||||
if (existsSync(manifestPath)) {
|
||||
const parsed: unknown = JSON.parse(readFileSync(manifestPath, "utf8"));
|
||||
if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
|
||||
manifest = parsed as Record<string, unknown>;
|
||||
}
|
||||
} catch {
|
||||
// A missing or malformed manifest uses the ordinary unhashed assets.
|
||||
}
|
||||
const asset = (name: string): string => {
|
||||
const value = manifest[name];
|
||||
const extension = name.endsWith(".js") ? "js" : "css";
|
||||
return typeof value === "string" &&
|
||||
new RegExp(`^[A-Za-z0-9][A-Za-z0-9_.-]*\\.${extension}$`).test(value)
|
||||
? value
|
||||
: name;
|
||||
};
|
||||
res.type("html").send(`<!doctype html>
|
||||
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="referrer" content="no-referrer"><meta name="review-consent-page" content="1"><title>Artifact consent · Anonymous GitHub</title><link rel="stylesheet" href="/css/${asset("all.min.css")}"></head><body><div id="app"></div><script src="/script/${asset("core.min.js")}"></script><script defer src="/script/${asset("vendor.min.js")}"></script></body></html>`);
|
||||
};
|
||||
}
|
||||
@@ -381,7 +381,16 @@ router.get("/queues", dashboardCache, async (req, res) => {
|
||||
...QUEUE_STATES.map(async (state) => {
|
||||
const jobs = await targetQueue.getJobs([state], 0, 199);
|
||||
return jobs.map((j) => {
|
||||
const json: Record<string, unknown> = { ...j.asJSON(), _state: state };
|
||||
// asJSON() serializes these fields to strings; the dashboard needs
|
||||
// the objects (payload repoId, stack lines, failure reason).
|
||||
const json: Record<string, unknown> = {
|
||||
...j.asJSON(),
|
||||
data: j.data,
|
||||
failedReason: j.failedReason,
|
||||
stacktrace: j.stacktrace,
|
||||
returnvalue: j.returnvalue,
|
||||
_state: state,
|
||||
};
|
||||
if (state === "delayed" && j.delay > 0) {
|
||||
json.delayUntil = j.timestamp + j.delay;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { resetOwnerAccessAlerts } from "../../core/owner-notifications";
|
||||
import { saveRegistrationEmail } from "../../core/registration-email";
|
||||
import { createClient } from "redis";
|
||||
import * as passport from "passport";
|
||||
import * as session from "express-session";
|
||||
@@ -99,12 +101,9 @@ export const verify = async (
|
||||
externalIDs: {
|
||||
github: profile.id,
|
||||
},
|
||||
emails: profile.emails?.map((email) => {
|
||||
return { email: email.value, default: false };
|
||||
}),
|
||||
emails: [],
|
||||
photo,
|
||||
});
|
||||
if (user.emails?.length) user.emails[0].default = true;
|
||||
await user.save();
|
||||
}
|
||||
}
|
||||
@@ -120,6 +119,8 @@ export const verify = async (
|
||||
return;
|
||||
}
|
||||
await setCredential(String(user!._id), accessToken);
|
||||
await saveRegistrationEmail(user!, accessToken);
|
||||
if (flow.ownerId) await resetOwnerAccessAlerts(String(user!._id));
|
||||
done(null, { username: user!.username, user });
|
||||
} catch (error) {
|
||||
logger.error("verify failed", serializeError(error));
|
||||
@@ -206,7 +207,7 @@ router.get(
|
||||
returnTo: safeAuthReturnTo(req.query.returnTo, current ? "/connections" : "/dashboard"), expires: Date.now() + 10 * 60000 };
|
||||
next();
|
||||
},
|
||||
passport.authenticate("github", { scope: ["repo"] }), // Note the scope here
|
||||
passport.authenticate("github", { scope: ["repo", "user:email"] }),
|
||||
function (req: express.Request, res: express.Response) {
|
||||
res.redirect("/");
|
||||
}
|
||||
@@ -251,7 +252,11 @@ router.post(
|
||||
const model = await UserModel.findOne({
|
||||
"apiTokens.tokenHash": hashToken(token),
|
||||
});
|
||||
if (!model) return res.status(401).json({ error: "invalid_token" });
|
||||
const entry = model?.apiTokens?.find(t => t.tokenHash === hashToken(token));
|
||||
if (!model || !entry || entry.scope ||
|
||||
(entry.expiresAt && entry.expiresAt.getTime() <= Date.now())) {
|
||||
return res.status(401).json({ error: "invalid_token" });
|
||||
}
|
||||
if (isDisabledAccount(model.status)) {
|
||||
return res.status(model.status === "banned" ? 403 : 401).json({
|
||||
error: model.status === "banned" ? "user_banned" : "not_connected",
|
||||
|
||||
@@ -21,8 +21,8 @@ function decodePathSegment(segment: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
// Extensions the browser will execute script from when it renders the
|
||||
// response as a document (directly, or via the "Raw" action).
|
||||
// Keep explicit fallbacks for markup types not consistently mapped by MIME
|
||||
// databases. MIME classification below also covers aliases such as svgz/shtml.
|
||||
const SCRIPTABLE_EXTENSIONS = new Set([
|
||||
"html",
|
||||
"htm",
|
||||
@@ -33,10 +33,16 @@ const SCRIPTABLE_EXTENSIONS = new Set([
|
||||
"xsl",
|
||||
"xslt",
|
||||
"mhtml",
|
||||
"mht",
|
||||
]);
|
||||
|
||||
export function isScriptableDocument(anonymizedPath: string): boolean {
|
||||
const name = anonymizedPath.substring(anonymizedPath.lastIndexOf("/") + 1);
|
||||
const mime = lookup(name);
|
||||
if (mime && (
|
||||
mime === "text/html" || mime === "text/xml" || mime === "application/xml" ||
|
||||
mime.endsWith("+xml") || mime === "message/rfc822" || mime === "multipart/related"
|
||||
)) return true;
|
||||
const dot = name.lastIndexOf(".");
|
||||
if (dot < 0) return false;
|
||||
return SCRIPTABLE_EXTENSIONS.has(name.substring(dot + 1).toLowerCase());
|
||||
@@ -115,6 +121,7 @@ router.get(
|
||||
object: f,
|
||||
});
|
||||
}
|
||||
res.header("X-Content-Type-Options", "nosniff");
|
||||
if (req.query.download) {
|
||||
res.attachment(
|
||||
anonymizedPath.substring(anonymizedPath.lastIndexOf("/") + 1)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { resetOwnerAccessAlerts } from "../../core/owner-notifications";
|
||||
import * as express from "express";
|
||||
import { ensureAuthenticated } from "./connection";
|
||||
|
||||
@@ -29,6 +30,7 @@ router.post(
|
||||
|
||||
const user = await getUser(req);
|
||||
isOwnerOrAdmin([gist.owner.id], user);
|
||||
if (user.id === gist.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "gist", id: String(gist.model._id) });
|
||||
await gist.updateIfNeeded({ force: true });
|
||||
res.json({ status: gist.status });
|
||||
} catch (error) {
|
||||
@@ -68,6 +70,7 @@ router.post(
|
||||
{ $set: { "options.expirationDate": newExpiration } }
|
||||
).exec();
|
||||
|
||||
if (user.id === gist.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "gist", id: String(gist.model._id) });
|
||||
await gist.updateIfNeeded({ force: true });
|
||||
res.json({ status: gist.status, expirationDate: newExpiration });
|
||||
} catch (error) {
|
||||
@@ -222,6 +225,7 @@ router.post(
|
||||
}
|
||||
).exec();
|
||||
await gist.updateStatus(RepositoryStatus.PREPARING);
|
||||
if (user.id === gist.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "gist", id: String(gist.model._id) });
|
||||
await gist.updateIfNeeded({ force: true });
|
||||
res.json(gist.toJSON());
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { notificationEmail, normalizeEmail, resetOwnerAccessAlerts } from "../../core/owner-notifications";
|
||||
import { saveRegistrationEmail } from "../../core/registration-email";
|
||||
import * as express from "express";
|
||||
import { createHmac, randomBytes, randomUUID, timingSafeEqual } from "crypto";
|
||||
import config from "../../config";
|
||||
@@ -16,7 +18,7 @@ import { isDisabledAccount, safeAuthReturnTo } from "./auth-utils";
|
||||
|
||||
type Flow = { state: string; expires: number; ownerId?: string; returnTo: string; repository?: string; install?: boolean };
|
||||
declare module "express-session" {
|
||||
interface SessionData { githubAppFlow?: Flow; githubInstallFlow?: Flow; githubConnectionCSRF?: string; }
|
||||
interface SessionData { githubAppFlow?: Flow; githubInstallFlow?: Flow; githubConnectionCSRF?: string; emailPromptDismissed?: boolean; }
|
||||
}
|
||||
export function safeReturnTo(value: unknown): string {
|
||||
return safeAuthReturnTo(value, "/connections");
|
||||
@@ -28,6 +30,12 @@ export function consumeFlow(flow: Flow | undefined, state: unknown): Flow {
|
||||
function newFlow(ownerId: string | undefined, returnTo: unknown): Flow {
|
||||
return { state: randomBytes(32).toString("hex"), expires: Date.now() + 10 * 60000, ownerId, returnTo: safeReturnTo(returnTo) };
|
||||
}
|
||||
// Parallel user/connections reads must initialize the same token even when
|
||||
// both loaded the session before either request saved it.
|
||||
export function connectionCSRF(req: express.Request): string {
|
||||
return req.session.githubConnectionCSRF ||= createHmac("sha256", config.SESSION_SECRET)
|
||||
.update(`github-connections:${req.sessionID}`).digest("hex");
|
||||
}
|
||||
function saveSession(req: express.Request) { return new Promise<void>((resolve, reject) => req.session.save(err => err ? reject(err) : resolve())); }
|
||||
function enabled(req: express.Request, res: express.Response, next: express.NextFunction) {
|
||||
if (!config.GITHUB_APP_ENABLED || !config.GITHUB_APP_NEW_CONNECTIONS) return res.status(503).json({ error: "github_app_disabled" });
|
||||
@@ -87,6 +95,8 @@ router.get("/app/callback", enabled, async (req, res) => {
|
||||
}
|
||||
if (isDisabledAccount(user.status)) throw appError("not_connected", 403);
|
||||
await saveAppGrant(user.id, tokens);
|
||||
await saveRegistrationEmail(user, tokens.access_token);
|
||||
if (flow.ownerId) await resetOwnerAccessAlerts(user.id);
|
||||
await new Promise<void>((resolve, reject) => req.login({ username: user!.username, user }, err => err ? reject(err) : resolve()));
|
||||
res.redirect(flow.install ? `/github/app/install?returnTo=${encodeURIComponent(flow.returnTo)}&repository=${encodeURIComponent(flow.repository || "")}` : flow.returnTo);
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
@@ -141,6 +151,7 @@ router.get("/app/setup", enabled, async (req, res) => {
|
||||
if (req.query.setup_action !== "request") {
|
||||
const installations = await userInstallations(user.id);
|
||||
if (!installations.some(i => String(i.id) === req.query.installation_id)) throw appError("github_app_access_required");
|
||||
await resetOwnerAccessAlerts(user.id);
|
||||
}
|
||||
await UserModel.updateOne({ _id: user.id }, { $set: { repositories: [] } });
|
||||
res.redirect(flow.returnTo);
|
||||
@@ -150,7 +161,7 @@ router.get("/app/setup", enabled, async (req, res) => {
|
||||
router.get("/connections", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
req.session.githubConnectionCSRF ||= randomBytes(32).toString("hex");
|
||||
connectionCSRF(req);
|
||||
await saveSession(req);
|
||||
const credentials = await CredentialModel.find({ ownerId: user.id }).select("provider revoked").lean();
|
||||
const appConnected = credentials.some(c => c.provider === APP_PROVIDER && !c.revoked);
|
||||
@@ -167,7 +178,8 @@ router.get("/connections", async (req, res) => {
|
||||
const repos = await RepositoryModel.find({ owner: user.id, status: { $ne: "removed" } }).select("repoId source.repositoryName githubAccess status statusDate").lean();
|
||||
const prs = await PullRequestModel.find({ owner: user.id, status: { $ne: "removed" } }).select("pullRequestId source.repositoryFullName githubAccess status statusDate").lean();
|
||||
const gistCount = await GistModel.countDocuments({ owner: user.id, status: { $ne: "removed" } });
|
||||
res.json({ csrf: req.session.githubConnectionCSRF, appEnabled: config.GITHUB_APP_ENABLED && config.GITHUB_APP_NEW_CONNECTIONS,
|
||||
res.json({ notificationEmail: notificationEmail(user.model.emails, user.model.notificationEmail), emailNotificationsEnabled: !!(config.RESEND_API_KEY && config.EMAIL_FROM),
|
||||
csrf: req.session.githubConnectionCSRF, appEnabled: config.GITHUB_APP_ENABLED && config.GITHUB_APP_NEW_CONNECTIONS,
|
||||
gistConnection: config.GITHUB_APP_ENABLED && credentials.some(c => c.provider === APP_PROVIDER) ? "github-app" : "oauth",
|
||||
oauthEnabled: config.GITHUB_OAUTH_ENABLED, oauthConnected: !!(await getCredentialToken(user.id)), appConnected, appError: appErrorCode,
|
||||
installations, gistCount, resources: [
|
||||
@@ -184,6 +196,33 @@ router.use("/connections", (req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
router.get("/connections/email", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
const csrf = connectionCSRF(req);
|
||||
await saveSession(req);
|
||||
res.json({ csrf, notificationEmail: notificationEmail(user.model.emails, user.model.notificationEmail) });
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
});
|
||||
|
||||
router.post("/connections/email", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
const action = req.body?.action || "save";
|
||||
if (!["save", "later", "never"].includes(action)) return res.status(400).json({ error: "Invalid email preference." });
|
||||
if (action === "later" || action === "never") {
|
||||
if (action === "never") await UserModel.updateOne({ _id: user.id }, { $set: { emailPromptNever: true } });
|
||||
req.session.emailPromptDismissed = true;
|
||||
await saveSession(req);
|
||||
return res.json({ ok: true });
|
||||
}
|
||||
const email = normalizeEmail(req.body?.email);
|
||||
if (!email) return res.status(400).json({ error: "Enter a valid email address." });
|
||||
await UserModel.updateOne({ _id: user.id }, { $set: { notificationEmail: email } });
|
||||
res.json({ notificationEmail: email });
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
});
|
||||
|
||||
router.post("/connections/migrate", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
@@ -225,7 +264,8 @@ router.post("/connections/migrate", async (req, res) => {
|
||||
} };
|
||||
const result = isRepo ? await RepositoryModel.updateOne(filter, change) : await PullRequestModel.updateOne(filter, change);
|
||||
if (!result.modifiedCount) throw appError("connection_changed", 409);
|
||||
res.json({ connection });
|
||||
await resetOwnerAccessAlerts(user.id, { kind: isRepo ? "repository" : "pull-request", id: String(model._id) });
|
||||
res.json({ connection, fullName: selected.fullName || name });
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Options } from "express-rate-limit";
|
||||
import { requestRateLimitKey } from "../rate-limit-key";
|
||||
|
||||
// Redis stores are supplied by the server. These policies also run in tests
|
||||
// with the in-memory store, through real HTTP requests and the auth middleware.
|
||||
export const tokenAttemptLimitOptions: Partial<Options> = {
|
||||
windowMs: 60_000,
|
||||
limit: 120,
|
||||
skip: req => !req.headers.authorization,
|
||||
keyGenerator: requestRateLimitKey,
|
||||
standardHeaders: "draft-6",
|
||||
legacyHeaders: false,
|
||||
message: { error: "token_attempts_rate_limited" },
|
||||
};
|
||||
|
||||
export const maintenanceRefreshLimitOptions: Partial<Options> = {
|
||||
windowMs: 60_000,
|
||||
limit: 1,
|
||||
skip: (req, res) => !res.locals.maintenanceToken || req.method !== "POST" ||
|
||||
!/^\/api\/repo\/[a-zA-Z0-9_-]+\/refresh\/?$/.test(req.path),
|
||||
// Share the cooldown across this account's tokens and client IPs. Keep
|
||||
// different accounts separate so a removed coauthor cannot block the owner.
|
||||
keyGenerator: (req, res) =>
|
||||
`${res.locals.maintenanceOwnerId}:${req.path.split("/")[3].toLowerCase()}`,
|
||||
standardHeaders: "draft-6",
|
||||
legacyHeaders: false,
|
||||
message: { error: "refresh_rate_limited" },
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
import * as express from "express";
|
||||
import { Types } from "mongoose";
|
||||
import { getUser, handleError, isOwnerCoauthorOrAdmin } from "./route-utils";
|
||||
import { generateToken, hashToken } from "./token-auth";
|
||||
import UserModel from "../../core/model/users/users.model";
|
||||
import { getRepository } from "../database";
|
||||
import AnonymousError from "../../core/AnonymousError";
|
||||
|
||||
const router = express.Router();
|
||||
router.use((req, res, next) => {
|
||||
res.set("Cache-Control", "no-store");
|
||||
// A restricted token must never mint credentials or become a session.
|
||||
if (req.headers.authorization || !req.isAuthenticated()) {
|
||||
return res.status(401).json({ error: "session_required" });
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
res.json((user.model.apiTokens || []).filter(t => t.scope === "repository-maintenance")
|
||||
.map(t => ({ id: t._id, name: t.name, repoIds: t.repoIds, expiresAt: t.expiresAt,
|
||||
createdAt: t.createdAt, lastUsedAt: t.lastUsedAt })));
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
});
|
||||
|
||||
router.post("/", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
const { name, repoIds, expiresInDays = 30 } = req.body;
|
||||
if (typeof name !== "string" || !name.trim() || name.length > 80 ||
|
||||
!Array.isArray(repoIds) || !repoIds.length || repoIds.length > 20 ||
|
||||
!repoIds.every(id => typeof id === "string" && /^[a-zA-Z0-9_-]{3,200}$/.test(id)) ||
|
||||
!Number.isInteger(expiresInDays) || expiresInDays < 1 || expiresInDays > 90) {
|
||||
throw new AnonymousError("invalid_token_options", { httpStatus: 400 });
|
||||
}
|
||||
for (const id of new Set<string>(repoIds)) {
|
||||
isOwnerCoauthorOrAdmin(await getRepository(id), user);
|
||||
}
|
||||
const token = generateToken();
|
||||
const entry = { _id: new Types.ObjectId(), name: name.trim(), scope: "repository-maintenance",
|
||||
repoIds: [...new Set(repoIds)], tokenHash: hashToken(token), createdAt: new Date(),
|
||||
expiresAt: new Date(Date.now() + expiresInDays * 86400_000) };
|
||||
const saved = await UserModel.updateOne({ _id: user.model._id,
|
||||
"apiTokens.50": { $exists: false } }, { $push: { apiTokens: entry } });
|
||||
if (!saved.matchedCount) throw new AnonymousError("token_limit_reached", { httpStatus: 409 });
|
||||
res.status(201).json({ id: entry._id, name: entry.name, repoIds: entry.repoIds,
|
||||
expiresAt: entry.expiresAt, token });
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
});
|
||||
|
||||
router.delete("/:id", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
if (!Types.ObjectId.isValid(req.params.id)) throw new AnonymousError("invalid_token_id", { httpStatus: 400 });
|
||||
const result = await UserModel.updateOne({ _id: user.model._id },
|
||||
{ $pull: { apiTokens: { _id: req.params.id, scope: "repository-maintenance" } } });
|
||||
res.json({ removed: result.modifiedCount });
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
});
|
||||
export default router;
|
||||
@@ -0,0 +1,59 @@
|
||||
import * as express from "express";
|
||||
import { getRepo, getUser, handleError, isOwnerCoauthorOrAdmin } from "./route-utils";
|
||||
import AnonymousError from "../../core/AnonymousError";
|
||||
import RepoModel from "../../core/model/anonymizedRepositories/anonymizedRepositories.model";
|
||||
|
||||
const router = express.Router();
|
||||
const toggles = ["update", "image", "pdf", "notebook", "link"];
|
||||
|
||||
/** Only in-place anonymization settings; never accept source or connection input. */
|
||||
export function maintenanceSettings(body: unknown): Record<string, unknown> {
|
||||
const invalid = () => new AnonymousError("invalid_settings", { httpStatus: 400 });
|
||||
if (!body || typeof body !== "object" || Array.isArray(body)) throw invalid();
|
||||
const entries = Object.entries(body);
|
||||
if (!entries.length) throw invalid();
|
||||
const updates: Record<string, unknown> = {};
|
||||
for (const [key, value] of entries) {
|
||||
if (key === "terms") {
|
||||
if (!Array.isArray(value) || value.length > 1000 ||
|
||||
!value.every(t => typeof t === "string" && t.length > 0 && t.length <= 1000)) throw invalid();
|
||||
} else if (!toggles.includes(key) || typeof value !== "boolean") throw invalid();
|
||||
updates[`options.${key}`] = value;
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
|
||||
router.route("/repositories/:repoId")
|
||||
.all((_req, res, next) => { res.set("Cache-Control", "no-store"); next(); })
|
||||
.get(async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
const repo = await getRepo(req, res, { nocheck: true });
|
||||
if (!repo) return;
|
||||
isOwnerCoauthorOrAdmin(repo, user);
|
||||
// Read saved metadata without refreshing GitHub credentials or content.
|
||||
res.json({ repoId: repo.repoId, status: repo.status, statusMessage: repo.model.statusMessage,
|
||||
statusDate: repo.model.statusDate, settingsSavedAt: repo.model.settingsSavedAt,
|
||||
source: { branch: repo.model.source.branch, commit: repo.model.source.commit },
|
||||
options: repo.model.options });
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
})
|
||||
.patch(async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
const repo = await getRepo(req, res, { nocheck: true });
|
||||
if (!repo) return;
|
||||
isOwnerCoauthorOrAdmin(repo, user);
|
||||
if (!["ready", "error"].includes(repo.status || "")) throw new AnonymousError("invalid_status", { httpStatus: 409 });
|
||||
repo.assertNotArchived();
|
||||
const updates = maintenanceSettings(req.body);
|
||||
const settingsSavedAt = new Date();
|
||||
const saved = await RepoModel.updateOne({ _id: repo.model._id, status: repo.model.status,
|
||||
statusDate: repo.model.statusDate,
|
||||
settingsSavedAt: repo.model.settingsSavedAt || { $exists: false } },
|
||||
{ $set: { ...updates, settingsSavedAt } }).exec();
|
||||
if (!saved.matchedCount) throw new AnonymousError("invalid_status", { httpStatus: 409 });
|
||||
res.json({ repoId: repo.repoId, status: repo.status, settingsSavedAt });
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
});
|
||||
export default router;
|
||||
@@ -1,3 +1,4 @@
|
||||
import { resetOwnerAccessAlerts } from "../../core/owner-notifications";
|
||||
import { selectRepositoryAccess } from "../../core/github-app";
|
||||
import * as express from "express";
|
||||
import { ensureAuthenticated } from "./connection";
|
||||
@@ -30,6 +31,7 @@ router.post(
|
||||
|
||||
const user = await getUser(req);
|
||||
isOwnerOrAdmin([pullRequest.owner.id], user);
|
||||
if (user.id === pullRequest.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "pull-request", id: String(pullRequest.model._id) });
|
||||
await pullRequest.updateIfNeeded({ force: true });
|
||||
res.json({ status: pullRequest.status });
|
||||
} catch (error) {
|
||||
@@ -71,6 +73,7 @@ router.post(
|
||||
{ $set: { "options.expirationDate": newExpiration } }
|
||||
).exec();
|
||||
|
||||
if (user.id === pullRequest.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "pull-request", id: String(pullRequest.model._id) });
|
||||
await pullRequest.updateIfNeeded({ force: true });
|
||||
res.json({ status: pullRequest.status, expirationDate: newExpiration });
|
||||
} catch (error) {
|
||||
@@ -242,6 +245,7 @@ router.post(
|
||||
}
|
||||
).exec();
|
||||
await pullRequest.updateStatus(RepositoryStatus.PREPARING);
|
||||
if (user.id === pullRequest.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "pull-request", id: String(pullRequest.model._id) });
|
||||
await pullRequest.updateIfNeeded({ force: true });
|
||||
res.json({ ...pullRequest.toJSON(), connection: pullRequest.model.githubAccess?.kind || "oauth" });
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { neverExpirationDeadline, validateBillingExpiration } from "../../core/billing/expiration";
|
||||
import config from "../../config";
|
||||
import { assertConferenceAdmission, reserveRepository } from "../../core/billing/quotas";
|
||||
import { resetOwnerAccessAlerts } from "../../core/owner-notifications";
|
||||
import { randomUUID } from "crypto";
|
||||
import { githubQuotaKey } from "../../core/github-token-context";
|
||||
import { selectRepositoryAccess, boundAppToken, appError } from "../../core/github-app";
|
||||
@@ -35,6 +36,24 @@ const router = express.Router();
|
||||
// user needs to be connected for all user API
|
||||
router.use(ensureAuthenticated);
|
||||
|
||||
async function enqueueSavedSnapshot(model: IAnonymizedRepositoryDocument) {
|
||||
try {
|
||||
await downloadQueue.add(model.repoId, { repoId: model.repoId }, {
|
||||
jobId: `repo-${model.repoId}`, attempts: 3,
|
||||
});
|
||||
} catch (error) {
|
||||
// Leave a failed enqueue retryable without overwriting a worker or a
|
||||
// lifecycle change that happened after this save.
|
||||
await AnonymizedRepositoryModel.updateOne({
|
||||
_id: model._id, status: RepositoryStatus.PREPARING,
|
||||
statusDate: model.statusDate, anonymizeDate: model.anonymizeDate,
|
||||
"githubAccess.revision": model.githubAccess?.revision || { $exists: false },
|
||||
}, { $set: { status: RepositoryStatus.ERROR, statusDate: new Date(),
|
||||
statusMessage: "preparation_interrupted" } }).exec();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function previewToken(req: express.Request) {
|
||||
const user = await getUser(req);
|
||||
if (typeof req.query.anonymizedRepoId === "string") {
|
||||
@@ -117,7 +136,8 @@ router.post("/claim", async (req, res) => {
|
||||
|
||||
await AnonymizedRepositoryModel.updateOne(
|
||||
{ repoId: repoConfig.repoId },
|
||||
{ $set: { owner: user.model.id, githubAccess: selectedAccess.binding } }
|
||||
{ $set: { owner: user.model.id, githubAccess: selectedAccess.binding, accessAlertGeneration: randomUUID() },
|
||||
$unset: { accessAlertClaimedAt: "" } }
|
||||
).collation({ locale: "en", strength: 2 });
|
||||
return res.send("Ok");
|
||||
} catch (error) {
|
||||
@@ -149,6 +169,7 @@ router.post(
|
||||
throw new AnonymousError("invalid_status", { httpStatus: 409 });
|
||||
}
|
||||
|
||||
if (user.id === repo.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "repository", id: String(repo.model._id) });
|
||||
await repo.refresh();
|
||||
res.json({ status: repo.status });
|
||||
} catch (error) {
|
||||
@@ -185,33 +206,35 @@ router.post(
|
||||
repo.model.options.expirationDate
|
||||
);
|
||||
const reactivating = repo.status === RepositoryStatus.EXPIRED;
|
||||
const updates: Record<string, Date> = {
|
||||
const updates: Record<string, Date | string | null> = {
|
||||
"options.expirationDate": newExpiration,
|
||||
settingsSavedAt: new Date(),
|
||||
};
|
||||
repo.model.options.expirationDate = newExpiration;
|
||||
repo.model.settingsSavedAt = updates.settingsSavedAt;
|
||||
repo.model.settingsSavedAt = updates.settingsSavedAt as Date;
|
||||
if (reactivating) {
|
||||
repo.model.anonymizeDate = new Date();
|
||||
updates.anonymizeDate = repo.model.anonymizeDate;
|
||||
updates.status = RepositoryStatus.PREPARING;
|
||||
updates.statusDate = new Date();
|
||||
updates.statusMessage = null;
|
||||
}
|
||||
const saved = await AnonymizedRepositoryModel.updateOne(
|
||||
{ _id: repo.model._id, status: repo.status, statusDate: repo.model.statusDate },
|
||||
{ $set: updates }
|
||||
).exec();
|
||||
if (saved && saved.matchedCount === 0) throw new AnonymousError("invalid_status", { httpStatus: 409 });
|
||||
if (user.id === repo.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "repository", id: String(repo.model._id) });
|
||||
|
||||
if (reactivating) {
|
||||
// Expiration removes the cached files. Rebuild the saved commit
|
||||
// directly instead of asking GitHub for the latest branch head first;
|
||||
// that lookup can fail after the new date has already been persisted
|
||||
// and leave the repository stuck in the expired state.
|
||||
await repo.updateStatus(RepositoryStatus.PREPARING);
|
||||
await downloadQueue.add(
|
||||
repo.repoId,
|
||||
{ repoId: repo.repoId },
|
||||
{ jobId: `repo-${repo.repoId}`, attempts: 3 }
|
||||
);
|
||||
repo.model.status = RepositoryStatus.PREPARING;
|
||||
repo.model.statusDate = updates.statusDate as Date;
|
||||
repo.model.statusMessage = undefined;
|
||||
await enqueueSavedSnapshot(repo.model);
|
||||
}
|
||||
res.json({ status: repo.status, expirationDate: newExpiration });
|
||||
} catch (error) {
|
||||
@@ -620,6 +643,9 @@ router.post(
|
||||
}
|
||||
}
|
||||
repo.model.conference = repoUpdate.conference;
|
||||
const preparation = sourceChanged || reactivating ? {
|
||||
status: RepositoryStatus.PREPARING, statusDate: new Date(), statusMessage: null,
|
||||
} : undefined;
|
||||
const saved = await AnonymizedRepositoryModel.updateOne(
|
||||
{ _id: repo.model._id, status: repo.status, statusDate: repo.model.statusDate,
|
||||
"githubAccess.revision": previousAccessRevision || { $exists: false } },
|
||||
@@ -632,22 +658,22 @@ router.post(
|
||||
githubAccess: repo.model.githubAccess,
|
||||
conference: repo.model.conference,
|
||||
anonymizeDate: repo.model.anonymizeDate,
|
||||
...preparation,
|
||||
},
|
||||
...(!repo.model.billingNeverExpiresUntil ? { $unset: { billingNeverExpiresUntil: 1 } } : {}),
|
||||
}
|
||||
).exec();
|
||||
if (!saved.matchedCount) throw appError("connection_changed", 409);
|
||||
if (user.id === repo.owner.id) await resetOwnerAccessAlerts(user.id, { kind: "repository", id: String(repo.model._id) });
|
||||
if (!sourceChanged && !reactivating) {
|
||||
return res.json({ status: repo.status });
|
||||
}
|
||||
|
||||
await repo.updateStatus(RepositoryStatus.PREPARING);
|
||||
repo.model.status = preparation!.status;
|
||||
repo.model.statusDate = preparation!.statusDate;
|
||||
repo.model.statusMessage = undefined;
|
||||
await enqueueSavedSnapshot(repo.model);
|
||||
res.json({ status: repo.status });
|
||||
await downloadQueue.add(
|
||||
repo.repoId,
|
||||
{ repoId: repo.repoId },
|
||||
{ jobId: `repo-${repo.repoId}` }
|
||||
);
|
||||
} catch (error) {
|
||||
return handleError(error, res, req);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { repositoryFailureStatus } from "../../core/repository-failure-status";
|
||||
import { anonymizePath } from "../../core/anonymize-utils";
|
||||
import * as express from "express";
|
||||
import config from "../../config";
|
||||
@@ -57,6 +58,7 @@ router.get(
|
||||
res.attachment(`${repo.repoId}.zip`);
|
||||
if (req.method === "HEAD") { res.end(); return; }
|
||||
await repo.countView();
|
||||
const submodules = await repo.submoduleMounts();
|
||||
|
||||
if (config.STREAMER_ENTRYPOINT) {
|
||||
// use the streamer service
|
||||
@@ -75,6 +77,7 @@ router.get(
|
||||
branch: repo.model.source.branch,
|
||||
repoId: repo.repoId,
|
||||
anonymizerOptions: anonymizer.opt,
|
||||
submodules,
|
||||
contentOptions: {
|
||||
image: repo.options.image,
|
||||
pdf: repo.options.pdf,
|
||||
@@ -169,6 +172,7 @@ router.get(
|
||||
commit: repo.model.source.commit || "HEAD",
|
||||
getToken: () => repo.getToken(),
|
||||
anonymizerOptions: anonymizer.opt,
|
||||
submodules,
|
||||
contentOptions: {
|
||||
image: repo.options.image,
|
||||
pdf: repo.options.pdf,
|
||||
@@ -335,7 +339,7 @@ router.get(
|
||||
: "repository_not_accessible",
|
||||
{
|
||||
object: repo,
|
||||
httpStatus: 500,
|
||||
httpStatus: repositoryFailureStatus(repo.model.statusMessage),
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -372,21 +376,26 @@ router.get(
|
||||
sourceCommitDate: repo.model.source.commitDate,
|
||||
settingsSavedAt: repo.model.settingsSavedAt,
|
||||
publishedAt: repo.model.publishedAt,
|
||||
// Lets reviewers see how long the link stays available.
|
||||
expirationDate:
|
||||
repo.options.expirationMode !== "never"
|
||||
? repo.options.expirationDate || null
|
||||
: null,
|
||||
isAdmin: user?.isAdmin === true,
|
||||
isOwner: user?.id == repo.model.owner,
|
||||
hasWebsite: !!repo.options.page && !!repo.options.pageSource,
|
||||
truncatedFolders: (repo.model.truncatedFolders || []).map((path) =>
|
||||
anonymizePath(path, repo.options.terms || [])
|
||||
),
|
||||
// Submodule contents are not included in GitHub archives/trees, so
|
||||
// they end up as empty folders in the anonymized repository. Surface
|
||||
// a warning in the explorer when the repository uses submodules (#737).
|
||||
// Legacy trees retain .gitmodules until their next tree refresh.
|
||||
hasSubmodules:
|
||||
(await FileModel.exists({
|
||||
repoId: repo.repoId,
|
||||
treeGeneration: repo.model.treeGeneration || { $exists: false },
|
||||
name: ".gitmodules",
|
||||
path: "",
|
||||
$or: [
|
||||
{ submodule: { $exists: true, $ne: "included" } },
|
||||
{ name: ".gitmodules", path: "" },
|
||||
],
|
||||
})) != null,
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -233,10 +233,10 @@ export function handleError(
|
||||
errorCode = String(error);
|
||||
}
|
||||
let status = 500;
|
||||
if (error.httpStatus) {
|
||||
status = error.httpStatus;
|
||||
} else if (error.$metadata?.httpStatusCode) {
|
||||
status = error.$metadata.httpStatusCode;
|
||||
const explicitStatus = [error?.httpStatus, error?.status, error?.response?.statusCode,
|
||||
error?.$metadata?.httpStatusCode].find(value => Number.isInteger(value) && value >= 400 && value <= 599);
|
||||
if (explicitStatus !== undefined) {
|
||||
status = explicitStatus;
|
||||
} else if (
|
||||
errorCode &&
|
||||
(errorCode.indexOf("not_found") > -1 ||
|
||||
|
||||
@@ -16,21 +16,35 @@ export function generateToken(): string {
|
||||
|
||||
export async function bearerTokenAuth(
|
||||
req: express.Request,
|
||||
_res: express.Response,
|
||||
res: express.Response,
|
||||
next: express.NextFunction
|
||||
): Promise<void> {
|
||||
if (req.user) return next();
|
||||
|
||||
const header = req.headers["authorization"];
|
||||
if (!header || typeof header !== "string") return next();
|
||||
const match = header.match(/^Bearer\s+(.+)$/i);
|
||||
if (!match) return next();
|
||||
if (!match || !match[1].trim() || match[1].length > 512) {
|
||||
res.status(401).json({ error: "invalid_token" });
|
||||
return;
|
||||
}
|
||||
|
||||
const tokenHash = hashToken(match[1].trim());
|
||||
try {
|
||||
const model = await UserModel.findOne({ "apiTokens.tokenHash": tokenHash });
|
||||
if (!model) return next();
|
||||
if (isDisabledAccount(model.status)) return next();
|
||||
const token = model?.apiTokens?.find(t => t.tokenHash === tokenHash);
|
||||
if (!model || !token || isDisabledAccount(model.status) ||
|
||||
(token.expiresAt && token.expiresAt.getTime() <= Date.now()) ||
|
||||
(token.scope && (!token.expiresAt || token.scope !== "repository-maintenance"))) {
|
||||
res.status(401).json({ error: "invalid_token" });
|
||||
return;
|
||||
}
|
||||
if (token.scope) {
|
||||
if (!maintenanceTokenAllows(req.method, req.path, token.repoIds || [])) {
|
||||
res.status(403).json({ error: "token_scope_forbidden" });
|
||||
return;
|
||||
}
|
||||
res.locals.maintenanceToken = true;
|
||||
res.locals.maintenanceOwnerId = String(model._id);
|
||||
}
|
||||
|
||||
// Mirror the shape produced by passport's verify() in connection.ts
|
||||
// so existing getUser()/route code works unchanged.
|
||||
@@ -48,6 +62,16 @@ export async function bearerTokenAuth(
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error("lookup failed", serializeError(err));
|
||||
res.status(503).json({ error: "authentication_unavailable" });
|
||||
return;
|
||||
}
|
||||
return next();
|
||||
}
|
||||
|
||||
/** Exact paths keep scoped tokens out of GitHub proxy and session routes. */
|
||||
export function maintenanceTokenAllows(method: string, path: string, repoIds: string[]): boolean {
|
||||
const readOrEdit = path.match(/^\/api\/maintenance\/repositories\/([a-zA-Z0-9_-]+)\/?$/);
|
||||
if (readOrEdit && ["GET", "PATCH"].includes(method)) return repoIds.includes(readOrEdit[1]);
|
||||
const action = path.match(/^\/api\/repo\/([a-zA-Z0-9_-]+)\/(refresh|extend)\/?$/);
|
||||
return !!action && method === "POST" && repoIds.includes(action[1]);
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
import { BillingQuota, BillingDraft } from "../../core/billing/models";
|
||||
import { cancelAccountBilling } from "../../core/billing/stripe";
|
||||
import { allowance } from "../../core/billing/quotas";
|
||||
import { notificationEmail } from "../../core/owner-notifications";
|
||||
import DashboardName from "../../core/model/dashboard-name";
|
||||
import { dashboardSummary } from "./dashboard-summary";
|
||||
import { revokeGrant } from "./github-app";
|
||||
import { connectionCSRF, revokeGrant } from "./github-app";
|
||||
import CredentialModel from "../../core/model/credentials/credentials.model";
|
||||
import * as express from "express";
|
||||
import config from "../../config";
|
||||
@@ -43,7 +44,18 @@ router.get("/logout", async (req, res) => {
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
const showEmailPrompt = !!(config.RESEND_API_KEY && config.EMAIL_FROM) &&
|
||||
!notificationEmail(user.model.emails, user.model.notificationEmail) && !user.model.emailPromptNever && !req.session.emailPromptDismissed;
|
||||
if (showEmailPrompt) {
|
||||
connectionCSRF(req);
|
||||
await new Promise<void>((resolve, reject) => req.session.save(error => error ? reject(error) : resolve()));
|
||||
}
|
||||
res.set("Cache-Control", "no-store");
|
||||
res.json({
|
||||
showEmailPrompt,
|
||||
emailPromptCSRF: showEmailPrompt ? req.session.githubConnectionCSRF : undefined,
|
||||
notificationEmail: notificationEmail(user.model.emails, user.model.notificationEmail),
|
||||
emailNotificationsEnabled: !!(config.RESEND_API_KEY && config.EMAIL_FROM),
|
||||
username: user.username,
|
||||
photo: user.photo,
|
||||
isAdmin: user.isAdmin,
|
||||
@@ -234,6 +246,9 @@ router.delete("/", async (req, res) => {
|
||||
repositories: [],
|
||||
},
|
||||
$unset: {
|
||||
notificationEmail: "",
|
||||
emailPromptNever: "",
|
||||
accessAlertAfter: "",
|
||||
accessTokens: "",
|
||||
accessTokenDates: "",
|
||||
externalIDs: "",
|
||||
|
||||
@@ -3,8 +3,7 @@ import { getRepo, handleError } from "./route-utils";
|
||||
import * as path from "path";
|
||||
import AnonymizedFile from "../../core/AnonymizedFile";
|
||||
import AnonymousError from "../../core/AnonymousError";
|
||||
import * as marked from "marked";
|
||||
import * as sanitizeHtml from "sanitize-html";
|
||||
import { renderMarkdown } from "../../markdown";
|
||||
import { streamToString, isTextFile } from "../../core/anonymize-utils";
|
||||
import { IFile } from "../../core/model/files/files.types";
|
||||
import { fileETag } from "./file-etag";
|
||||
@@ -20,33 +19,6 @@ function escapeHtml(str: string): string {
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
const sanitizeOptions: sanitizeHtml.IOptions = {
|
||||
allowedTags: sanitizeHtml.defaults.allowedTags.concat([
|
||||
"img",
|
||||
"video",
|
||||
"input",
|
||||
"details",
|
||||
"summary",
|
||||
"del",
|
||||
"ins",
|
||||
"sup",
|
||||
"sub",
|
||||
]),
|
||||
allowedAttributes: {
|
||||
...sanitizeHtml.defaults.allowedAttributes,
|
||||
img: ["src", "srcset", "alt", "title", "width", "height", "loading"],
|
||||
video: ["src", "controls", "title"],
|
||||
input: ["type", "checked", "disabled"],
|
||||
code: ["class"],
|
||||
span: ["class"],
|
||||
div: ["class"],
|
||||
pre: ["class"],
|
||||
td: ["align"],
|
||||
th: ["align"],
|
||||
},
|
||||
allowedSchemes: ["http", "https", "mailto"],
|
||||
};
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const indexPriority = [
|
||||
@@ -161,7 +133,7 @@ async function webView(
|
||||
}">${escapeHtml(c.name) + (c.size == null ? "/" : "")}</a>`
|
||||
)
|
||||
.join("")}</div></div>`;
|
||||
const html = `<!DOCTYPE html><html><head><title>Content</title></head><link rel="stylesheet" href="/css/all.min.css" /><body>${body}</body></html>`;
|
||||
const html = `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Content</title></head><link rel="stylesheet" href="/css/all.min.css" /><body>${body}</body></html>`;
|
||||
return res.contentType("text/html").send(html);
|
||||
}
|
||||
}
|
||||
@@ -174,7 +146,7 @@ async function webView(
|
||||
});
|
||||
}
|
||||
res.set("Cache-Control", "private, no-cache, must-revalidate");
|
||||
const pageETag = fileETag(JSON.stringify(["web-v1", repo.model.source.commit, repo.model.treeGeneration, repo.model.contentCacheRevision,
|
||||
const pageETag = fileETag(JSON.stringify(["web-v2", repo.model.source.commit, repo.model.treeGeneration, repo.model.contentCacheRevision,
|
||||
await f.sha(), repo.model.source.repositoryName, repo.model.source.branch, config.APP_HOSTNAME, config.ANONYMIZATION_MASK]),
|
||||
requestPath, repo.options);
|
||||
res.set("ETag", pageETag);
|
||||
@@ -189,8 +161,8 @@ async function webView(
|
||||
if (f.extension() == "md") {
|
||||
try {
|
||||
const content = await streamToString(await f.anonymizedContent());
|
||||
const body = sanitizeHtml(marked.marked.parse(content, { async: false }), sanitizeOptions);
|
||||
const html = `<!DOCTYPE html><html><head><title>Content</title></head><link rel="stylesheet" href="/css/all.min.css" /><body><div class="container p-3 file-content markdown-body">${body}</div></body></html>`;
|
||||
const body = renderMarkdown(content);
|
||||
const html = `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Content</title></head><link rel="stylesheet" href="/css/all.min.css" /><body><div class="container p-3 file-content markdown-body">${body}</div><script defer src="/script/markdown.min.js"></script></body></html>`;
|
||||
res.contentType("text/html").send(html);
|
||||
} catch {
|
||||
// Raw Markdown must re-enter rendering when a transient failure clears.
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { RequestHandler } from "express";
|
||||
|
||||
/** The asterisk request target asks about the server, not a session or route. */
|
||||
export const serverOptions: RequestHandler = (req, res, next) => {
|
||||
if (req.originalUrl !== "*") return next();
|
||||
if (req.method !== "OPTIONS") { res.status(400).json({ error: "invalid_request" }); return; }
|
||||
res.setHeader("Allow", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS");
|
||||
res.status(204).end();
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
import { decodeReviewJSON } from "./review-json";
|
||||
const invalid = (): never => { throw new Error("Invalid review callback registry"); };
|
||||
export function createReviewCallbacks(raw: string) {
|
||||
let value: unknown;
|
||||
try { value = decodeReviewJSON(Buffer.from(raw, "utf8")); } catch { return invalid(); }
|
||||
if (!value || typeof value !== "object" || Array.isArray(value) || Object.keys(value).sort().join(",") !== "callbacks,version") return invalid();
|
||||
const config = value as { version: unknown; callbacks: unknown };
|
||||
if (config.version !== 1 || !Array.isArray(config.callbacks) || !config.callbacks.length || config.callbacks.length > 32) return invalid();
|
||||
const callbacks = new Map<string, string>(), destinations = new Set<string>();
|
||||
for (const item of config.callbacks) {
|
||||
if (!item || typeof item !== "object" || Array.isArray(item) || Object.keys(item).sort().join(",") !== "callbackId,clientId,url") return invalid();
|
||||
const { clientId, callbackId, url } = item;
|
||||
if (![clientId, callbackId].every(id => typeof id === "string" && /^[a-f0-9]{32}$/.test(id)) || typeof url !== "string" || url.length > 2048) return invalid();
|
||||
let parsed: URL;
|
||||
try { parsed = new URL(url); } catch { return invalid(); }
|
||||
if (parsed.protocol !== "https:" || parsed.href !== url || parsed.username || parsed.password || parsed.search || parsed.hash || url.includes("?") || url.includes("#") || !/^\/[A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*$/.test(parsed.pathname)) return invalid();
|
||||
const key = clientId + ":" + callbackId;
|
||||
if (callbacks.has(key)) return invalid();
|
||||
callbacks.set(key, url); destinations.add(url);
|
||||
}
|
||||
const formAction = "'self' " + [...destinations].join(" ");
|
||||
if (Buffer.byteLength(formAction) > 4096) return invalid();
|
||||
return Object.freeze({
|
||||
formAction,
|
||||
resolve(clientId: string, callbackId: string): string | undefined { return callbacks.get(clientId + ":" + callbackId); },
|
||||
});
|
||||
}
|
||||
export type ReviewCallbacks = ReturnType<typeof createReviewCallbacks>;
|
||||
@@ -0,0 +1,200 @@
|
||||
import { createHash, randomBytes, timingSafeEqual } from "crypto";
|
||||
import { RequestHandler } from "express";
|
||||
|
||||
const contract = "4open.artifacts/1";
|
||||
const clientPattern = /^[a-f0-9]{32}$/;
|
||||
const keyPattern = /^[A-Za-z0-9_-]{1,64}$/;
|
||||
const secretPattern = /^[a-f0-9]{64}$/;
|
||||
type Key = { clientId: string; digest: Buffer; from: number; until: number };
|
||||
|
||||
function invalid(): never {
|
||||
// Never include private configuration or request values in diagnostic errors.
|
||||
throw new Error("Invalid REVIEW_SERVICE_KEYS configuration");
|
||||
}
|
||||
function record(value: unknown, fields: string[]): Record<string, unknown> {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) invalid();
|
||||
const data = value as Record<string, unknown>;
|
||||
if (
|
||||
Object.keys(data).length !== fields.length ||
|
||||
fields.some((k) => !Object.prototype.hasOwnProperty.call(data, k))
|
||||
)
|
||||
invalid();
|
||||
return data;
|
||||
}
|
||||
function timestamp(value: unknown): number {
|
||||
if (
|
||||
typeof value !== "string" ||
|
||||
!/^20[0-9]{2}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/.test(value)
|
||||
)
|
||||
invalid();
|
||||
const time = Date.parse(value);
|
||||
if (
|
||||
!Number.isFinite(time) ||
|
||||
new Date(time).toISOString() !== value.replace("Z", ".000Z")
|
||||
)
|
||||
invalid();
|
||||
return time;
|
||||
}
|
||||
function registry(raw: string): Map<string, Key> {
|
||||
if (Buffer.byteLength(raw, "utf8") > 65536) invalid();
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(raw);
|
||||
} catch {
|
||||
invalid();
|
||||
}
|
||||
const config = record(parsed, ["version", "keys"]);
|
||||
if (
|
||||
config.version !== 1 ||
|
||||
!Array.isArray(config.keys) ||
|
||||
config.keys.length < 1 ||
|
||||
config.keys.length > 64
|
||||
)
|
||||
invalid();
|
||||
const keys = new Map<string, Key>(),
|
||||
digests = new Set<string>(),
|
||||
clients = new Map<string, number>();
|
||||
for (const entry of config.keys) {
|
||||
const k = record(entry, [
|
||||
"clientId",
|
||||
"keyId",
|
||||
"tokenSHA256",
|
||||
"notBefore",
|
||||
"notAfter",
|
||||
]);
|
||||
if (
|
||||
typeof k.clientId !== "string" ||
|
||||
!clientPattern.test(k.clientId) ||
|
||||
typeof k.keyId !== "string" ||
|
||||
!keyPattern.test(k.keyId) ||
|
||||
typeof k.tokenSHA256 !== "string" ||
|
||||
!secretPattern.test(k.tokenSHA256)
|
||||
)
|
||||
invalid();
|
||||
const from = timestamp(k.notBefore),
|
||||
until = timestamp(k.notAfter);
|
||||
const id = k.clientId + ":" + k.keyId;
|
||||
const count = (clients.get(k.clientId) || 0) + 1;
|
||||
if (
|
||||
until <= from ||
|
||||
keys.has(id) ||
|
||||
digests.has(k.tokenSHA256) ||
|
||||
count > 16
|
||||
)
|
||||
invalid();
|
||||
clients.set(k.clientId, count);
|
||||
if (clients.size > 32) invalid();
|
||||
digests.add(k.tokenSHA256);
|
||||
keys.set(id, {
|
||||
clientId: k.clientId,
|
||||
digest: Buffer.from(k.tokenSHA256, "hex"),
|
||||
from,
|
||||
until,
|
||||
});
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/** Optional server-to-server read-only endpoint. No browser, owner or repository authority is inferred. */
|
||||
export function createReviewCapabilities(
|
||||
raw?: string,
|
||||
now: () => number = Date.now,
|
||||
): RequestHandler {
|
||||
const keys = raw === undefined ? null : registry(raw);
|
||||
// One bounded counter per registered client, shared by its rotation keys.
|
||||
const rates = new Map<string, { minute: number; count: number }>();
|
||||
const dummyDigest = Buffer.alloc(32);
|
||||
return (req, res) => {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.setHeader("Referrer-Policy", "no-referrer");
|
||||
res.setHeader("X-Content-Type-Options", "nosniff");
|
||||
const fail = (status: number, code: string, retry?: number) => {
|
||||
// Do not keep a rejected request alive while Node drains an unread body.
|
||||
res.setHeader("Connection", "close");
|
||||
if (retry) res.setHeader("Retry-After", String(retry));
|
||||
res.status(status).json({
|
||||
contract,
|
||||
code,
|
||||
requestId: randomBytes(16).toString("hex"),
|
||||
...(retry ? { retryAfterSeconds: retry } : {}),
|
||||
});
|
||||
};
|
||||
if (!keys || req.originalUrl !== "/service/v1/capabilities")
|
||||
return fail(404, "not-found");
|
||||
if (req.method !== "GET") return fail(405, "invalid-request");
|
||||
// Reject browser context and bodies before authentication; never read or log them.
|
||||
if (
|
||||
[
|
||||
"cookie",
|
||||
"origin",
|
||||
"referer",
|
||||
"sec-fetch-site",
|
||||
"sec-fetch-mode",
|
||||
"sec-fetch-dest",
|
||||
"sec-fetch-user",
|
||||
].some((h) => req.headers[h] !== undefined)
|
||||
)
|
||||
return fail(403, "forbidden");
|
||||
if (
|
||||
req.headers["transfer-encoding"] !== undefined ||
|
||||
req.headers["content-type"] !== undefined ||
|
||||
(req.headers["content-length"] !== undefined &&
|
||||
req.headers["content-length"] !== "0")
|
||||
)
|
||||
return fail(400, "invalid-request");
|
||||
const one = (name: string): string | undefined => {
|
||||
let count = 0;
|
||||
for (let i = 0; i < req.rawHeaders.length; i += 2)
|
||||
if (req.rawHeaders[i].toLowerCase() === name) count++;
|
||||
const value = req.headers[name];
|
||||
return count === 1 && typeof value === "string" ? value : undefined;
|
||||
};
|
||||
const client = one("x-4open-artifact-client-id"),
|
||||
keyId = one("x-4open-artifact-service-key-id"),
|
||||
authorization = one("authorization");
|
||||
if (
|
||||
!client ||
|
||||
!clientPattern.test(client) ||
|
||||
!keyId ||
|
||||
!keyPattern.test(keyId) ||
|
||||
!authorization ||
|
||||
!/^Bearer [a-f0-9]{64}$/i.test(authorization) ||
|
||||
!secretPattern.test(authorization.slice(7))
|
||||
)
|
||||
return fail(401, "unauthorized");
|
||||
const key = keys.get(client + ":" + keyId);
|
||||
const digest = createHash("sha256")
|
||||
.update(authorization.slice(7), "ascii")
|
||||
.digest();
|
||||
const equal = timingSafeEqual(digest, key ? key.digest : dummyDigest);
|
||||
const time = now();
|
||||
if (
|
||||
!key ||
|
||||
!equal ||
|
||||
!Number.isFinite(time) ||
|
||||
time < key.from ||
|
||||
time >= key.until
|
||||
)
|
||||
return fail(401, "unauthorized");
|
||||
const minute = Math.floor(time / 60000);
|
||||
let rate = rates.get(client);
|
||||
if (!rate || rate.minute !== minute) {
|
||||
rate = { minute, count: 0 };
|
||||
rates.set(client, rate);
|
||||
}
|
||||
if (rate.count >= 60)
|
||||
return fail(
|
||||
429,
|
||||
"rate-limited",
|
||||
Math.max(1, Math.ceil((60000 - (time % 60000)) / 1000)),
|
||||
);
|
||||
rate.count++;
|
||||
// No link, snapshot, protected-delivery or retention support exists yet.
|
||||
res.status(200).json({
|
||||
contract,
|
||||
clientId: client,
|
||||
contracts: [contract],
|
||||
available: [],
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
import { randomBytes } from "crypto";
|
||||
import { RequestHandler } from "express";
|
||||
import {
|
||||
createReviewOwnerConsent,
|
||||
ReviewConsentError,
|
||||
} from "./review-owner-consent";
|
||||
import {
|
||||
createReviewServiceAuth,
|
||||
singleReviewHeader,
|
||||
} from "./review-service-auth";
|
||||
import { decodeReviewJSON } from "./review-json";
|
||||
|
||||
const contract = "4open.artifacts/1";
|
||||
const opaque = /^[a-f0-9]{32}$/;
|
||||
function exact(
|
||||
value: unknown,
|
||||
keys: string[],
|
||||
): value is Record<string, unknown> {
|
||||
return (
|
||||
!!value &&
|
||||
typeof value === "object" &&
|
||||
!Array.isArray(value) &&
|
||||
Object.keys(value).sort().join(",") === keys.sort().join(",")
|
||||
);
|
||||
}
|
||||
function receipt(value: unknown, clientId: string, intentId: string): unknown {
|
||||
if (
|
||||
!exact(value, [
|
||||
"contract",
|
||||
"clientId",
|
||||
"intentId",
|
||||
"bindingId",
|
||||
"submissionRef",
|
||||
"policy",
|
||||
"entitlementId",
|
||||
]) ||
|
||||
value.contract !== contract ||
|
||||
value.clientId !== clientId ||
|
||||
value.intentId !== intentId ||
|
||||
![value.bindingId, value.submissionRef, value.entitlementId].every(
|
||||
(id) => typeof id === "string" && opaque.test(id),
|
||||
) ||
|
||||
!exact(value.policy, ["version", "access", "retainUntil"])
|
||||
)
|
||||
throw new Error("Invalid receipt");
|
||||
const policy = value.policy;
|
||||
if (
|
||||
!Number.isSafeInteger(policy.version) ||
|
||||
(policy.version as number) < 1 ||
|
||||
!["anonymous-link", "restricted-review"].includes(
|
||||
policy.access as string,
|
||||
) ||
|
||||
typeof policy.retainUntil !== "string" ||
|
||||
!/^2[01][0-9]{2}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/.test(
|
||||
policy.retainUntil,
|
||||
) ||
|
||||
!Number.isFinite(Date.parse(policy.retainUntil)) ||
|
||||
new Date(policy.retainUntil).toISOString() !==
|
||||
policy.retainUntil.replace("Z", ".000Z")
|
||||
)
|
||||
throw new Error("Invalid receipt");
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Mount before global JSON parsing and browser authentication. Disabled without
|
||||
* its own scoped registry. This factory is intentionally not mounted at startup. */
|
||||
export function createReviewCompletionService(
|
||||
raw: string | undefined,
|
||||
backend: Pick<
|
||||
ReturnType<typeof createReviewOwnerConsent>,
|
||||
"exchange" | "receipt"
|
||||
>,
|
||||
now: () => number = Date.now,
|
||||
): RequestHandler {
|
||||
const auth =
|
||||
raw === undefined ? undefined : createReviewServiceAuth(raw, now);
|
||||
const rates = new Map<string, { minute: number; count: number }>();
|
||||
let active = 0;
|
||||
return async (req, res) => {
|
||||
res.set({
|
||||
"Cache-Control": "no-store",
|
||||
"Referrer-Policy": "no-referrer",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
});
|
||||
res.removeHeader("Access-Control-Allow-Origin");
|
||||
const fail = (status: number, code: string) => {
|
||||
if (res.headersSent || res.destroyed) return;
|
||||
res.setHeader("Connection", "close");
|
||||
res
|
||||
.status(status)
|
||||
.json({ contract, code, requestId: randomBytes(16).toString("hex") });
|
||||
};
|
||||
const exchange = req.originalUrl === "/service/v1/completions/exchange";
|
||||
const read = /^\/service\/v1\/intents\/([a-f0-9]{32})\/receipt$/.exec(
|
||||
req.originalUrl,
|
||||
);
|
||||
if (!auth || (!exchange && !read)) return fail(404, "not-found");
|
||||
if (req.method !== (exchange ? "POST" : "GET"))
|
||||
return fail(405, "invalid-request");
|
||||
if (
|
||||
!req.secure ||
|
||||
[
|
||||
"cookie",
|
||||
"origin",
|
||||
"referer",
|
||||
"sec-fetch-site",
|
||||
"sec-fetch-mode",
|
||||
"sec-fetch-dest",
|
||||
"sec-fetch-user",
|
||||
].some((name) => req.headers[name] !== undefined)
|
||||
)
|
||||
return fail(403, "forbidden");
|
||||
if (req.body !== undefined) return fail(503, "unavailable");
|
||||
if (req.headers["content-encoding"] !== undefined)
|
||||
return fail(400, "invalid-request");
|
||||
for (const name of ["content-type", "content-length", "transfer-encoding"])
|
||||
if (
|
||||
req.headers[name] !== undefined &&
|
||||
singleReviewHeader(req, name) === undefined
|
||||
)
|
||||
return fail(400, "invalid-request");
|
||||
if (exchange) {
|
||||
if (
|
||||
!/^application\/json(?:\s*;\s*charset=utf-8)?$/i.test(
|
||||
singleReviewHeader(req, "content-type") || "",
|
||||
)
|
||||
)
|
||||
return fail(400, "invalid-request");
|
||||
const length = req.headers["content-length"];
|
||||
if (
|
||||
length !== undefined &&
|
||||
(!/^\d+$/.test(String(length)) || Number(length) > 65536)
|
||||
)
|
||||
return fail(400, "invalid-request");
|
||||
} else if (
|
||||
req.headers["transfer-encoding"] !== undefined ||
|
||||
req.headers["content-type"] !== undefined ||
|
||||
(req.headers["content-length"] !== undefined &&
|
||||
req.headers["content-length"] !== "0")
|
||||
)
|
||||
return fail(400, "invalid-request");
|
||||
const principal = auth.authenticate(
|
||||
req,
|
||||
exchange ? "completion.exchange" : "receipt.read",
|
||||
);
|
||||
if (!principal) return fail(401, "unauthorized");
|
||||
const minute = Math.floor(now() / 60000);
|
||||
let rate = rates.get(principal.clientId);
|
||||
if (!rate || rate.minute !== minute) {
|
||||
rate = { minute, count: 0 };
|
||||
rates.set(principal.clientId, rate);
|
||||
}
|
||||
if (rate.count++ >= 60) return fail(429, "rate-limited");
|
||||
if (active >= 4) return fail(503, "unavailable");
|
||||
active++;
|
||||
let cancelBody: (() => void) | undefined;
|
||||
let ended = false;
|
||||
const close = () => {
|
||||
ended = true;
|
||||
cancelBody?.();
|
||||
};
|
||||
res.once("close", close);
|
||||
const timer = setTimeout(() => {
|
||||
ended = true;
|
||||
fail(503, "unavailable");
|
||||
cancelBody?.();
|
||||
}, 15000);
|
||||
try {
|
||||
let intentId = read?.[1] || "";
|
||||
let result: unknown;
|
||||
if (exchange) {
|
||||
const bytes = await new Promise<Buffer>((resolve, reject) => {
|
||||
const chunks: Buffer[] = [];
|
||||
let size = 0;
|
||||
const cleanup = () => {
|
||||
req.off("data", data);
|
||||
req.off("end", end);
|
||||
req.off("aborted", abort);
|
||||
req.off("error", abort);
|
||||
cancelBody = undefined;
|
||||
};
|
||||
const abort = () => {
|
||||
cleanup();
|
||||
reject(new Error("Request unavailable"));
|
||||
};
|
||||
const data = (chunk: Buffer) => {
|
||||
size += chunk.length;
|
||||
if (size > 65536) {
|
||||
fail(400, "invalid-request");
|
||||
abort();
|
||||
} else chunks.push(chunk);
|
||||
};
|
||||
const end = () => {
|
||||
cleanup();
|
||||
resolve(Buffer.concat(chunks));
|
||||
};
|
||||
cancelBody = abort;
|
||||
req.on("data", data);
|
||||
req.once("end", end);
|
||||
req.once("aborted", abort);
|
||||
req.once("error", abort);
|
||||
});
|
||||
if (ended) return;
|
||||
let command: unknown;
|
||||
try {
|
||||
command = decodeReviewJSON(bytes);
|
||||
} catch {
|
||||
return fail(400, "invalid-request");
|
||||
}
|
||||
if (
|
||||
!exact(command, [
|
||||
"contract",
|
||||
"clientId",
|
||||
"intentId",
|
||||
"code",
|
||||
"requestId",
|
||||
]) ||
|
||||
typeof command.contract !== "string" ||
|
||||
![command.clientId, command.intentId, command.requestId].every(
|
||||
(id) => typeof id === "string" && opaque.test(id),
|
||||
) ||
|
||||
typeof command.code !== "string" ||
|
||||
!/^[a-f0-9]{64}$/.test(command.code)
|
||||
)
|
||||
return fail(400, "invalid-request");
|
||||
if (command.contract !== contract)
|
||||
return fail(422, "unsupported-version");
|
||||
if (command.clientId !== principal.clientId)
|
||||
return fail(403, "forbidden");
|
||||
if (!principal.valid()) return fail(401, "unauthorized");
|
||||
intentId = command.intentId as string;
|
||||
result = await backend.exchange(
|
||||
principal.clientId,
|
||||
command as Parameters<typeof backend.exchange>[1],
|
||||
);
|
||||
} else {
|
||||
if (!principal.valid()) return fail(401, "unauthorized");
|
||||
result = await backend.receipt(principal.clientId, intentId);
|
||||
}
|
||||
if (ended) return;
|
||||
if (!principal.valid()) return fail(401, "unauthorized");
|
||||
res.status(200).json(receipt(result, principal.clientId, intentId));
|
||||
} catch (error) {
|
||||
if (ended) return;
|
||||
if (error instanceof ReviewConsentError) {
|
||||
const codes = {
|
||||
invalid: [400, "invalid-request"],
|
||||
forbidden: [403, "forbidden"],
|
||||
expired: [410, "expired"],
|
||||
conflict: [409, "conflict"],
|
||||
unavailable: [503, "unavailable"],
|
||||
} as const;
|
||||
const [status, code] = codes[error.kind];
|
||||
fail(status, code);
|
||||
} else fail(503, "unavailable");
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
res.off("close", close);
|
||||
active--;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
import { ReviewCallbacks } from "./review-callbacks";
|
||||
import { createHmac, timingSafeEqual } from "crypto";
|
||||
import * as express from "express";
|
||||
import { Request, Response } from "express";
|
||||
import { createReviewOwnerConsent } from "./review-owner-consent";
|
||||
|
||||
type BrowserSession = { passport?: { user?: unknown } };
|
||||
type Context = { accountId: string; sessionId: string };
|
||||
const opaque = /^[a-f0-9]{32}$/;
|
||||
const secret = /^[a-f0-9]{64}$/;
|
||||
function fields(
|
||||
value: unknown,
|
||||
names: string[],
|
||||
): value is Record<string, unknown> {
|
||||
return (
|
||||
!!value &&
|
||||
typeof value === "object" &&
|
||||
!Array.isArray(value) &&
|
||||
Object.keys(value).sort().join(",") === names.sort().join(",")
|
||||
);
|
||||
}
|
||||
function reject(res: Response, status: number, code: string) {
|
||||
if (!res.headersSent && !res.destroyed) res.setHeader("Connection", "close");
|
||||
if (!res.headersSent && !res.destroyed)
|
||||
res.status(status).json({ error: { code } });
|
||||
}
|
||||
async function current(req: Request): Promise<Context | undefined> {
|
||||
const id = (req.user as { user?: { _id?: unknown } } | undefined)?.user?._id;
|
||||
const accountId = id === undefined ? "" : String(id);
|
||||
if (
|
||||
!req.isAuthenticated?.() ||
|
||||
!/^[a-f0-9]{24}$/.test(accountId) ||
|
||||
!/^[A-Za-z0-9_-]{24,256}$/.test(req.sessionID || "")
|
||||
)
|
||||
return undefined;
|
||||
const session = await new Promise<BrowserSession | undefined>(
|
||||
(resolve, fail) =>
|
||||
req.sessionStore.get(req.sessionID, (error, value) =>
|
||||
error
|
||||
? fail(new Error("Session unavailable"))
|
||||
: resolve(value as BrowserSession | undefined),
|
||||
),
|
||||
);
|
||||
if (session?.passport?.user !== accountId) return undefined;
|
||||
return { accountId, sessionId: req.sessionID };
|
||||
}
|
||||
|
||||
/** Mount after current Passport/session authentication and BEFORE any JSON
|
||||
* parser. This factory is not registered by application startup. TLS must be
|
||||
* established directly or identified through the configured trusted proxy. */
|
||||
export function createReviewConsentRouter(
|
||||
origin: string,
|
||||
backend: ReturnType<typeof createReviewOwnerConsent>,
|
||||
csrfKey: Buffer,
|
||||
callbacks?: ReviewCallbacks,
|
||||
) {
|
||||
try {
|
||||
if (new URL(origin).origin !== origin || !origin.startsWith("https://"))
|
||||
throw new Error();
|
||||
} catch {
|
||||
throw new Error("Invalid review consent origin");
|
||||
}
|
||||
if (!Buffer.isBuffer(csrfKey) || csrfKey.length !== 32)
|
||||
throw new Error("Invalid review consent CSRF key");
|
||||
const key = Buffer.from(csrfKey);
|
||||
const csrf = (actor: Context) =>
|
||||
createHmac("sha256", key)
|
||||
.update(
|
||||
"4open.review-consent-csrf/1." +
|
||||
actor.accountId +
|
||||
"." +
|
||||
actor.sessionId,
|
||||
)
|
||||
.digest();
|
||||
const router = express.Router({ strict: true, caseSensitive: true });
|
||||
const rates = new Map<string, { until: number; count: number }>();
|
||||
router.use((req, res, next) => {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.setHeader("Referrer-Policy", "no-referrer");
|
||||
res.setHeader("X-Content-Type-Options", "nosniff");
|
||||
res.removeHeader("Access-Control-Allow-Origin");
|
||||
if (!["/csrf", "/preview", "/confirm", ...(callbacks ? ["/handoff"] : [])].includes(req.url))
|
||||
return reject(res, 404, "not-found");
|
||||
if (req.method !== (req.url === "/csrf" ? "GET" : "POST"))
|
||||
return reject(res, 405, "invalid-request");
|
||||
if (
|
||||
req.url === "/csrf" &&
|
||||
(req.headers["transfer-encoding"] !== undefined ||
|
||||
(req.headers["content-length"] !== undefined &&
|
||||
req.headers["content-length"] !== "0"))
|
||||
)
|
||||
return reject(res, 400, "invalid-request");
|
||||
if (
|
||||
!req.secure ||
|
||||
req.headers.authorization !== undefined ||
|
||||
req.headers["content-encoding"] !== undefined
|
||||
)
|
||||
return reject(res, 403, "forbidden");
|
||||
for (const name of [
|
||||
"origin",
|
||||
"content-type",
|
||||
"x-review-csrf",
|
||||
"sec-fetch-site",
|
||||
]) {
|
||||
if (
|
||||
req.rawHeaders.filter((v, i) => i % 2 === 0 && v.toLowerCase() === name)
|
||||
.length > 1
|
||||
)
|
||||
return reject(res, 403, "forbidden");
|
||||
}
|
||||
if (
|
||||
(req.headers.origin !== undefined && req.headers.origin !== origin) ||
|
||||
(req.method === "POST" && req.headers.origin !== origin) ||
|
||||
(req.headers["sec-fetch-site"] !== undefined &&
|
||||
req.headers["sec-fetch-site"] !== "same-origin")
|
||||
)
|
||||
return reject(res, 403, "forbidden");
|
||||
if (
|
||||
req.method === "POST" &&
|
||||
!/^application\/json(?:\s*;\s*charset=utf-8)?$/i.test(
|
||||
req.headers["content-type"] || "",
|
||||
)
|
||||
)
|
||||
return reject(res, 415, "invalid-request");
|
||||
// Fail closed if mounted after a body parser that bypassed our byte limit.
|
||||
if (req.body !== undefined) return reject(res, 503, "unavailable");
|
||||
const controller = new AbortController();
|
||||
res.locals.consentSignal = controller.signal;
|
||||
const timer = setTimeout(() => {
|
||||
controller.abort();
|
||||
reject(res, 408, "expired");
|
||||
req.destroy();
|
||||
}, 15000);
|
||||
const done = () => {
|
||||
clearTimeout(timer);
|
||||
controller.abort();
|
||||
};
|
||||
res.once("close", done);
|
||||
res.once("finish", done);
|
||||
next();
|
||||
});
|
||||
router.use(async (req, res, next) => {
|
||||
try {
|
||||
const actor = await current(req);
|
||||
if (res.destroyed || res.headersSent || res.locals.consentSignal.aborted)
|
||||
return;
|
||||
if (!actor) return reject(res, 401, "unauthorized");
|
||||
if (req.method === "POST") {
|
||||
const supplied = req.headers["x-review-csrf"];
|
||||
if (
|
||||
typeof supplied !== "string" ||
|
||||
!secret.test(supplied) ||
|
||||
!timingSafeEqual(Buffer.from(supplied, "hex"), csrf(actor))
|
||||
)
|
||||
return reject(res, 403, "forbidden");
|
||||
}
|
||||
const now = Date.now();
|
||||
for (const [id, rate] of rates) if (rate.until <= now) rates.delete(id);
|
||||
const rate = rates.get(actor.accountId) || {
|
||||
until: now + 60000,
|
||||
count: 0,
|
||||
};
|
||||
if (
|
||||
rate.count >= 60 ||
|
||||
(!rates.has(actor.accountId) && rates.size >= 1024)
|
||||
) {
|
||||
res.setHeader("Retry-After", "60");
|
||||
return reject(res, 429, "rate-limited");
|
||||
}
|
||||
rate.count++;
|
||||
rates.set(actor.accountId, rate);
|
||||
res.locals.consentActor = actor;
|
||||
next();
|
||||
} catch {
|
||||
reject(res, 503, "unavailable");
|
||||
}
|
||||
});
|
||||
router.get("/csrf", async (req, res) => {
|
||||
try {
|
||||
const fresh = await current(req);
|
||||
if (!fresh || fresh.accountId !== res.locals.consentActor.accountId)
|
||||
return reject(res, 401, "unauthorized");
|
||||
// Derive without saving the session: a late CSRF response must never
|
||||
// resurrect a session that a concurrent logout has destroyed.
|
||||
res.json({ csrf: csrf(fresh).toString("hex"), handoffEnabled: !!callbacks });
|
||||
} catch {
|
||||
reject(res, 503, "unavailable");
|
||||
}
|
||||
});
|
||||
router.use(express.json({ limit: 12288, strict: true, inflate: false }));
|
||||
// Uploads can outlive the session checked before body parsing. Revalidate
|
||||
// before any provider call or consent write, not only before the response.
|
||||
router.use(async (req, res, next) => {
|
||||
try {
|
||||
const actor = res.locals.consentActor as Context;
|
||||
const fresh = await current(req);
|
||||
if (res.destroyed || res.headersSent || res.locals.consentSignal.aborted)
|
||||
return;
|
||||
if (
|
||||
!fresh ||
|
||||
fresh.accountId !== actor.accountId ||
|
||||
fresh.sessionId !== actor.sessionId
|
||||
)
|
||||
return reject(res, 401, "unauthorized");
|
||||
next();
|
||||
} catch {
|
||||
reject(res, 503, "unavailable");
|
||||
}
|
||||
});
|
||||
router.post(["/preview", "/confirm", "/handoff"], async (req, res) => {
|
||||
const actor = res.locals.consentActor as Context;
|
||||
try {
|
||||
let result: unknown;
|
||||
if (req.url === "/handoff") {
|
||||
if (!callbacks || !fields(req.body, ["ticket"]) || typeof req.body.ticket !== "string" || !req.body.ticket || req.body.ticket.length > 8192) return reject(res, 400, "invalid-request");
|
||||
const result = await backend.handoff(actor, req.body.ticket, callbacks.resolve);
|
||||
if (res.destroyed || res.headersSent) return;
|
||||
const fresh = await current(req);
|
||||
if (!fresh || fresh.accountId !== actor.accountId || fresh.sessionId !== actor.sessionId) return reject(res, 401, "unauthorized");
|
||||
res.json({ completion: { contract: result.completion.contract, clientId: result.completion.clientId, intentId: result.completion.intentId, code: result.completion.code, expiresAt: result.completion.expiresAt }, callbackUrl: result.callbackUrl });
|
||||
} else if (req.url === "/preview") {
|
||||
if (
|
||||
!fields(req.body, ["repositoryId", "intent"]) ||
|
||||
typeof req.body.repositoryId !== "string" ||
|
||||
!/^[A-Za-z0-9_-]{3,128}$/.test(req.body.repositoryId) ||
|
||||
!fields(req.body.intent, [
|
||||
"contract",
|
||||
"clientId",
|
||||
"intentId",
|
||||
"token",
|
||||
"requestId",
|
||||
])
|
||||
)
|
||||
return reject(res, 422, "invalid-request");
|
||||
const input = req.body.intent;
|
||||
if (
|
||||
input.contract !== "4open.artifacts/1" ||
|
||||
![input.clientId, input.intentId, input.requestId].every(
|
||||
(v) => typeof v === "string" && opaque.test(v),
|
||||
) ||
|
||||
typeof input.token !== "string" ||
|
||||
!secret.test(input.token)
|
||||
)
|
||||
return reject(res, 422, "invalid-request");
|
||||
const preview = await backend.preview(
|
||||
actor,
|
||||
req.body.repositoryId,
|
||||
{
|
||||
contract: input.contract,
|
||||
clientId: input.clientId as string,
|
||||
intentId: input.intentId as string,
|
||||
requestId: input.requestId as string,
|
||||
token: input.token,
|
||||
},
|
||||
res.locals.consentSignal,
|
||||
);
|
||||
result = {
|
||||
ticket: preview.ticket,
|
||||
repositoryName: preview.repositoryName,
|
||||
policy: {
|
||||
version: preview.policy.version,
|
||||
access: preview.policy.access,
|
||||
retainUntil: preview.policy.retainUntil,
|
||||
},
|
||||
expiresAt: preview.expiresAt,
|
||||
};
|
||||
} else {
|
||||
if (
|
||||
!fields(req.body, [
|
||||
"ticket",
|
||||
"requestId",
|
||||
"acceptAccess",
|
||||
"acceptRetention",
|
||||
]) ||
|
||||
typeof req.body.ticket !== "string" ||
|
||||
req.body.ticket.length > 8192 ||
|
||||
typeof req.body.requestId !== "string" ||
|
||||
!opaque.test(req.body.requestId) ||
|
||||
req.body.acceptAccess !== true ||
|
||||
req.body.acceptRetention !== true
|
||||
)
|
||||
return reject(res, 422, "invalid-request");
|
||||
const saved = await backend.confirm(actor, req.body.ticket, {
|
||||
requestId: req.body.requestId,
|
||||
acceptAccess: true,
|
||||
acceptRetention: true,
|
||||
});
|
||||
result = {
|
||||
requestId: saved.requestId,
|
||||
policy: {
|
||||
version: saved.policy.version,
|
||||
access: saved.policy.access,
|
||||
retainUntil: saved.policy.retainUntil,
|
||||
},
|
||||
confirmedAt: saved.confirmedAt,
|
||||
};
|
||||
}
|
||||
const fresh = await current(req);
|
||||
if (
|
||||
!fresh ||
|
||||
fresh.accountId !== actor.accountId ||
|
||||
fresh.sessionId !== actor.sessionId
|
||||
)
|
||||
return reject(res, 401, "unauthorized");
|
||||
if (!res.headersSent && !res.destroyed) res.json(result);
|
||||
} catch (error) {
|
||||
const kind = (error as { kind?: string })?.kind;
|
||||
const status =
|
||||
kind === "invalid" || kind === "protocol"
|
||||
? 422
|
||||
: kind === "forbidden" || kind === "rejected"
|
||||
? 403
|
||||
: kind === "expired"
|
||||
? 410
|
||||
: kind === "conflict"
|
||||
? 409
|
||||
: 503;
|
||||
reject(
|
||||
res,
|
||||
status,
|
||||
status === 503
|
||||
? "unavailable"
|
||||
: status === 422
|
||||
? "invalid-request"
|
||||
: kind!,
|
||||
);
|
||||
}
|
||||
});
|
||||
router.use(((error, _req, res, _next) =>
|
||||
reject(
|
||||
res,
|
||||
error?.status === 413 ? 413 : 400,
|
||||
"invalid-request",
|
||||
)) as express.ErrorRequestHandler);
|
||||
return router;
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
import { decodeReviewJSON } from "./review-json";
|
||||
import * as https from "https";
|
||||
import { ClientRequest } from "http";
|
||||
|
||||
const contract = "4open.artifacts/1",
|
||||
limit = 65536;
|
||||
const opaque = /^[a-f0-9]{32}$/,
|
||||
secret = /^[a-f0-9]{64}$/;
|
||||
export type IntentClientFailure =
|
||||
| "invalid"
|
||||
| "busy"
|
||||
| "unavailable"
|
||||
| "rejected"
|
||||
| "conflict"
|
||||
| "expired"
|
||||
| "not-found"
|
||||
| "protocol";
|
||||
export class IntentClientError extends Error {
|
||||
constructor(public readonly kind: IntentClientFailure) {
|
||||
super("Review intent consumption " + kind);
|
||||
this.name = "IntentClientError";
|
||||
}
|
||||
}
|
||||
export interface ReviewIntentClientConfig {
|
||||
origin: string;
|
||||
clientId: string;
|
||||
keyId: string;
|
||||
token: string;
|
||||
notBefore: string;
|
||||
notAfter: string;
|
||||
}
|
||||
export interface IntentConsumption {
|
||||
contract: string;
|
||||
clientId: string;
|
||||
intentId: string;
|
||||
token: string;
|
||||
requestId: string;
|
||||
}
|
||||
export type ConsumedReviewIntent = Readonly<{
|
||||
contract: string;
|
||||
clientId: string;
|
||||
intentId: string;
|
||||
submissionRef: string;
|
||||
callbackId: string;
|
||||
entitlementId: string;
|
||||
expiresAt: string;
|
||||
policy: Readonly<{
|
||||
version: number;
|
||||
access: "anonymous-link" | "restricted-review";
|
||||
retainUntil: string;
|
||||
}>;
|
||||
}>;
|
||||
function fail(kind: IntentClientFailure): never {
|
||||
throw new IntentClientError(kind);
|
||||
}
|
||||
function object(value: unknown, fields: string[]): Record<string, unknown> {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value))
|
||||
fail("protocol");
|
||||
const out = value as Record<string, unknown>;
|
||||
if (
|
||||
Object.keys(out).length !== fields.length ||
|
||||
fields.some((k) => !Object.prototype.hasOwnProperty.call(out, k))
|
||||
)
|
||||
fail("protocol");
|
||||
return out;
|
||||
}
|
||||
function instant(value: unknown): number {
|
||||
if (
|
||||
typeof value !== "string" ||
|
||||
!/^2[01][0-9]{2}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/.test(value)
|
||||
)
|
||||
fail("protocol");
|
||||
const time = Date.parse(value);
|
||||
if (
|
||||
!Number.isFinite(time) ||
|
||||
new Date(time).toISOString() !== value.replace("Z", ".000Z")
|
||||
)
|
||||
fail("protocol");
|
||||
return time;
|
||||
}
|
||||
function id(value: unknown): value is string {
|
||||
return typeof value === "string" && opaque.test(value);
|
||||
}
|
||||
|
||||
function decode(bytes: Buffer): unknown {
|
||||
try {
|
||||
return decodeReviewJSON(bytes);
|
||||
} catch {
|
||||
return fail("protocol");
|
||||
}
|
||||
}
|
||||
|
||||
function intent(
|
||||
bytes: Buffer,
|
||||
clientId: string,
|
||||
intentId: string,
|
||||
): ConsumedReviewIntent {
|
||||
const p = object(decode(bytes), [
|
||||
"contract",
|
||||
"clientId",
|
||||
"intentId",
|
||||
"submissionRef",
|
||||
"callbackId",
|
||||
"policy",
|
||||
"expiresAt",
|
||||
"entitlementId",
|
||||
]);
|
||||
const policy = object(p.policy, ["version", "access", "retainUntil"]),
|
||||
now = Date.now();
|
||||
if (
|
||||
p.contract !== contract ||
|
||||
p.clientId !== clientId ||
|
||||
p.intentId !== intentId ||
|
||||
!id(p.submissionRef) ||
|
||||
!id(p.callbackId) ||
|
||||
!id(p.entitlementId)
|
||||
)
|
||||
fail("protocol");
|
||||
if (
|
||||
!Number.isSafeInteger(policy.version) ||
|
||||
(policy.version as number) < 1 ||
|
||||
(policy.access !== "anonymous-link" &&
|
||||
policy.access !== "restricted-review") ||
|
||||
instant(policy.retainUntil) <= now
|
||||
)
|
||||
fail("protocol");
|
||||
const expires = instant(p.expiresAt);
|
||||
if (expires <= now || expires > now + 600000) fail("protocol");
|
||||
return Object.freeze({
|
||||
contract,
|
||||
clientId,
|
||||
intentId,
|
||||
submissionRef: p.submissionRef,
|
||||
callbackId: p.callbackId,
|
||||
entitlementId: p.entitlementId,
|
||||
expiresAt: p.expiresAt as string,
|
||||
policy: Object.freeze({
|
||||
version: policy.version as number,
|
||||
access: policy.access as "anonymous-link" | "restricted-review",
|
||||
retainUntil: policy.retainUntil as string,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
/** Trusted server configuration only. One attempt per call; the caller retains
|
||||
* its request ID for an explicit retry. No browser callback or owner authority
|
||||
* is established by consuming an intent. No credentials are exposed on the
|
||||
* returned object, through serialization, or in transport errors. */
|
||||
export function createReviewIntentConsumer(config: ReviewIntentClientConfig) {
|
||||
let origin: string,
|
||||
clientId: string,
|
||||
keyId: string,
|
||||
token: string,
|
||||
from: number,
|
||||
until: number;
|
||||
try {
|
||||
const c = object(config, [
|
||||
"origin",
|
||||
"clientId",
|
||||
"keyId",
|
||||
"token",
|
||||
"notBefore",
|
||||
"notAfter",
|
||||
]);
|
||||
if (
|
||||
typeof c.origin !== "string" ||
|
||||
c.origin.length > 2048 ||
|
||||
!id(c.clientId) ||
|
||||
typeof c.keyId !== "string" ||
|
||||
!/^[A-Za-z0-9_-]{1,64}$/.test(c.keyId) ||
|
||||
typeof c.token !== "string" ||
|
||||
!secret.test(c.token)
|
||||
)
|
||||
fail("invalid");
|
||||
const url = new URL(c.origin);
|
||||
if (
|
||||
url.protocol !== "https:" ||
|
||||
url.origin !== c.origin ||
|
||||
url.username ||
|
||||
url.password
|
||||
)
|
||||
fail("invalid");
|
||||
origin = c.origin;
|
||||
clientId = c.clientId;
|
||||
keyId = c.keyId;
|
||||
token = c.token;
|
||||
from = instant(c.notBefore);
|
||||
until = instant(c.notAfter);
|
||||
if (until <= from || Date.now() < from || Date.now() >= until)
|
||||
fail("invalid");
|
||||
} catch {
|
||||
return fail("invalid");
|
||||
}
|
||||
let active = 0;
|
||||
return Object.freeze({
|
||||
consume(
|
||||
input: IntentConsumption,
|
||||
signal?: AbortSignal,
|
||||
): Promise<ConsumedReviewIntent> {
|
||||
let body: Buffer, expectedIntent: string;
|
||||
try {
|
||||
const p = object(input, [
|
||||
"contract",
|
||||
"clientId",
|
||||
"intentId",
|
||||
"token",
|
||||
"requestId",
|
||||
]);
|
||||
if (
|
||||
p.contract !== contract ||
|
||||
p.clientId !== clientId ||
|
||||
!id(p.intentId) ||
|
||||
!id(p.requestId) ||
|
||||
typeof p.token !== "string" ||
|
||||
!secret.test(p.token) ||
|
||||
Date.now() < from ||
|
||||
Date.now() >= until
|
||||
)
|
||||
fail("invalid");
|
||||
expectedIntent = p.intentId;
|
||||
body = Buffer.from(
|
||||
JSON.stringify({
|
||||
contract,
|
||||
clientId,
|
||||
intentId: expectedIntent,
|
||||
token: p.token,
|
||||
requestId: p.requestId,
|
||||
}),
|
||||
);
|
||||
} catch {
|
||||
return Promise.reject(new IntentClientError("invalid"));
|
||||
}
|
||||
if (signal?.aborted)
|
||||
return Promise.reject(new IntentClientError("unavailable"));
|
||||
if (active >= 4) return Promise.reject(new IntentClientError("busy"));
|
||||
active++;
|
||||
return new Promise((resolve, reject) => {
|
||||
let request: ClientRequest | undefined,
|
||||
settled = false;
|
||||
const abort = () => finish("unavailable");
|
||||
const timer = setTimeout(abort, 10000);
|
||||
function finish(
|
||||
error?: IntentClientFailure,
|
||||
value?: ConsumedReviewIntent,
|
||||
) {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
signal?.removeEventListener("abort", abort);
|
||||
active--;
|
||||
if (error) {
|
||||
request?.destroy();
|
||||
reject(new IntentClientError(error));
|
||||
} else resolve(value!);
|
||||
}
|
||||
signal?.addEventListener("abort", abort, { once: true });
|
||||
try {
|
||||
request = https.request(
|
||||
origin + "/service/v1/artifact-intents/consume",
|
||||
{
|
||||
method: "POST",
|
||||
agent: false,
|
||||
rejectUnauthorized: true,
|
||||
minVersion: "TLSv1.2",
|
||||
maxHeaderSize: 16384,
|
||||
headers: {
|
||||
Authorization: "Bearer " + token,
|
||||
"X-4open-Artifact-Client-Id": clientId,
|
||||
"X-4open-Artifact-Service-Key-Id": keyId,
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"Accept-Encoding": "identity",
|
||||
"Content-Length": body.length,
|
||||
},
|
||||
},
|
||||
(response) => {
|
||||
response.once("aborted", abort);
|
||||
response.once("error", abort);
|
||||
const status = response.statusCode || 0;
|
||||
if (status !== 200) {
|
||||
const kind: IntentClientFailure =
|
||||
status === 404
|
||||
? "not-found"
|
||||
: status === 409
|
||||
? "conflict"
|
||||
: status === 410
|
||||
? "expired"
|
||||
: [400, 401, 403, 422].includes(status)
|
||||
? "rejected"
|
||||
: [408, 429, 500, 502, 503, 504].includes(status)
|
||||
? "unavailable"
|
||||
: "protocol";
|
||||
finish(kind);
|
||||
return;
|
||||
}
|
||||
const types = response.rawHeaders.filter(
|
||||
(_v, i) =>
|
||||
i % 2 === 0 &&
|
||||
response.rawHeaders[i].toLowerCase() === "content-type",
|
||||
).length;
|
||||
const length = response.headers["content-length"];
|
||||
if (
|
||||
types !== 1 ||
|
||||
!/^application\/json(?:\s*;\s*charset=(?:utf-8|"utf-8"))?\s*$/i.test(
|
||||
response.headers["content-type"] || "",
|
||||
) ||
|
||||
response.headers["content-encoding"] !== undefined ||
|
||||
response.headers["set-cookie"] !== undefined ||
|
||||
(length !== undefined &&
|
||||
(!/^[0-9]+$/.test(length) || Number(length) > limit))
|
||||
) {
|
||||
finish("protocol");
|
||||
return;
|
||||
}
|
||||
let size = 0;
|
||||
const chunks: Buffer[] = [];
|
||||
response.on("data", (chunk: Buffer) => {
|
||||
size += chunk.length;
|
||||
if (size > limit) finish("protocol");
|
||||
else if (!settled) chunks.push(chunk);
|
||||
});
|
||||
response.once("end", () => {
|
||||
if (settled) return;
|
||||
if (
|
||||
signal?.aborted ||
|
||||
Date.now() < from ||
|
||||
Date.now() >= until
|
||||
) {
|
||||
finish("unavailable");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
finish(
|
||||
undefined,
|
||||
intent(
|
||||
Buffer.concat(chunks, size),
|
||||
clientId,
|
||||
expectedIntent,
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
finish("protocol");
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
request.once("error", abort);
|
||||
request.setTimeout(5000, abort);
|
||||
if (signal?.aborted) abort();
|
||||
if (!settled) request.end(body);
|
||||
} catch {
|
||||
finish("unavailable");
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
function invalid(): never {
|
||||
throw new Error("Invalid review JSON");
|
||||
}
|
||||
|
||||
// JSON.parse checks syntax; this bounded walk additionally rejects duplicate
|
||||
// decoded keys, including escaped spellings, before typed field validation.
|
||||
export function decodeReviewJSON(bytes: Buffer): unknown {
|
||||
if (bytes.length > 65536) invalid();
|
||||
const text = bytes.toString("utf8");
|
||||
if (!Buffer.from(text, "utf8").equals(bytes)) invalid();
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(text);
|
||||
} catch {
|
||||
invalid();
|
||||
}
|
||||
let at = 0;
|
||||
const space = () => {
|
||||
while (/\s/.test(text[at] || "") && at < text.length) at++;
|
||||
};
|
||||
const string = (): string => {
|
||||
const start = at++;
|
||||
while (at < text.length) {
|
||||
if (text[at] === "\\") {
|
||||
at += 2;
|
||||
continue;
|
||||
}
|
||||
if (text[at++] === '"') return JSON.parse(text.slice(start, at));
|
||||
}
|
||||
return invalid();
|
||||
};
|
||||
const walk = (depth: number): void => {
|
||||
if (depth > 8) invalid();
|
||||
space();
|
||||
if (text[at] === '"') {
|
||||
string();
|
||||
return;
|
||||
}
|
||||
if (text[at] === "{" || text[at] === "[") {
|
||||
const isObject = text[at++] === "{",
|
||||
end = isObject ? "}" : "]",
|
||||
keys = new Set<string>();
|
||||
space();
|
||||
if (text[at] === end) {
|
||||
at++;
|
||||
return;
|
||||
}
|
||||
for (;;) {
|
||||
space();
|
||||
if (isObject) {
|
||||
const key = string();
|
||||
if (keys.has(key)) invalid();
|
||||
keys.add(key);
|
||||
space();
|
||||
at++; // colon; syntax already checked
|
||||
}
|
||||
walk(depth + 1);
|
||||
space();
|
||||
if (text[at++] === end) return;
|
||||
}
|
||||
}
|
||||
while (at < text.length && !/[\s,}\]]/.test(text[at])) at++;
|
||||
};
|
||||
walk(0);
|
||||
space();
|
||||
if (at !== text.length) invalid();
|
||||
return parsed;
|
||||
}
|
||||
@@ -0,0 +1,602 @@
|
||||
import { createHash, createHmac, timingSafeEqual, randomBytes } from "crypto";
|
||||
import { Connection, ClientSession, Types } from "mongoose";
|
||||
import {
|
||||
ConsumedReviewIntent,
|
||||
IntentConsumption,
|
||||
createReviewIntentConsumer,
|
||||
} from "./review-intent-client";
|
||||
|
||||
type Principal = { accountId: string; sessionId: string };
|
||||
type Authority = {
|
||||
accountId: string;
|
||||
repositoryId: string;
|
||||
repositoryName: string;
|
||||
role: "owner" | "coauthor";
|
||||
};
|
||||
type Quote = {
|
||||
version: 1;
|
||||
accountId: string;
|
||||
sessionHash: string;
|
||||
repositoryId: string;
|
||||
intent: ConsumedReviewIntent;
|
||||
};
|
||||
export type ConsentReceipt = Readonly<{
|
||||
clientId: string;
|
||||
intentId: string;
|
||||
accountId: string;
|
||||
repositoryId: string;
|
||||
role: "owner" | "coauthor";
|
||||
policy: ConsumedReviewIntent["policy"];
|
||||
requestId: string;
|
||||
confirmedAt: string;
|
||||
}>;
|
||||
type SavedConsent = {
|
||||
_id: string;
|
||||
accountId: string;
|
||||
repositoryId: string;
|
||||
requestId: string;
|
||||
ticketHash: string;
|
||||
receipt: ConsentReceipt;
|
||||
intent: ConsumedReviewIntent;
|
||||
};
|
||||
type BindingReceipt = {
|
||||
contract: "4open.artifacts/1";
|
||||
clientId: string;
|
||||
intentId: string;
|
||||
bindingId: string;
|
||||
submissionRef: string;
|
||||
policy: ConsumedReviewIntent["policy"];
|
||||
entitlementId: string;
|
||||
};
|
||||
type SavedCompletion = {
|
||||
_id: string;
|
||||
accountId: string;
|
||||
repositoryId: string;
|
||||
ticketHash: string;
|
||||
intent: ConsumedReviewIntent;
|
||||
nonce: string;
|
||||
codeHash: string;
|
||||
expiresAt: string;
|
||||
exchange?: { requestId: string; receipt: BindingReceipt };
|
||||
};
|
||||
export class ReviewConsentError extends Error {
|
||||
constructor(
|
||||
public readonly kind:
|
||||
"invalid" | "forbidden" | "expired" | "conflict" | "unavailable",
|
||||
) {
|
||||
super("Review owner consent " + kind);
|
||||
this.name = "ReviewConsentError";
|
||||
}
|
||||
}
|
||||
const deny = (kind: ReviewConsentError["kind"]): never => {
|
||||
throw new ReviewConsentError(kind);
|
||||
};
|
||||
const hash = (value: string) =>
|
||||
createHash("sha256").update(value).digest("hex");
|
||||
const objectId = (value: unknown): value is string =>
|
||||
typeof value === "string" && /^[a-f0-9]{24}$/.test(value);
|
||||
function principal(value: Principal): {
|
||||
accountId: string;
|
||||
sessionHash: string;
|
||||
} {
|
||||
if (
|
||||
!value ||
|
||||
!objectId(value.accountId) ||
|
||||
typeof value.sessionId !== "string" ||
|
||||
!/^[A-Za-z0-9_-]{24,256}$/.test(value.sessionId)
|
||||
)
|
||||
deny("invalid");
|
||||
return { accountId: value.accountId, sessionHash: hash(value.sessionId) };
|
||||
}
|
||||
|
||||
/** Server-side consent storage. Principals must come from current authenticated
|
||||
* browser sessions, never request JSON. A future HTTP adapter must apply its
|
||||
* own same-origin/CSRF checks. No route or provider is enabled by this factory. */
|
||||
export function createReviewOwnerConsent(
|
||||
connection: Connection,
|
||||
consumer: ReturnType<typeof createReviewIntentConsumer>,
|
||||
signingKey: Buffer,
|
||||
) {
|
||||
if (!Buffer.isBuffer(signingKey) || signingKey.length !== 32) deny("invalid");
|
||||
const key = Buffer.from(signingKey);
|
||||
let activeTransactions = 0;
|
||||
let activeReads = 0;
|
||||
const signature = (body: string) =>
|
||||
createHmac("sha256", key)
|
||||
.update("4open.review-consent/1." + body)
|
||||
.digest();
|
||||
function decode(ticket: string, who: ReturnType<typeof principal>): Quote {
|
||||
if (
|
||||
typeof ticket !== "string" ||
|
||||
ticket.length > 8192 ||
|
||||
!/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]{43}$/.test(ticket)
|
||||
)
|
||||
deny("invalid");
|
||||
const [body, encoded] = ticket.split(".");
|
||||
const supplied = Buffer.from(encoded, "base64url");
|
||||
if (
|
||||
supplied.length !== 32 ||
|
||||
supplied.toString("base64url") !== encoded ||
|
||||
!timingSafeEqual(supplied, signature(body))
|
||||
)
|
||||
deny("invalid");
|
||||
let quote: Quote;
|
||||
try {
|
||||
quote = JSON.parse(Buffer.from(body, "base64url").toString("utf8"));
|
||||
} catch {
|
||||
return deny("invalid");
|
||||
}
|
||||
if (
|
||||
quote.version !== 1 ||
|
||||
quote.accountId !== who.accountId ||
|
||||
quote.sessionHash !== who.sessionHash ||
|
||||
!objectId(quote.repositoryId)
|
||||
)
|
||||
deny("forbidden");
|
||||
if (
|
||||
Date.parse(quote.intent.expiresAt) <= Date.now() ||
|
||||
Date.parse(quote.intent.policy.retainUntil) <= Date.now()
|
||||
)
|
||||
deny("expired");
|
||||
return quote;
|
||||
}
|
||||
async function transaction<T>(
|
||||
who: ReturnType<typeof principal>,
|
||||
repository: { repoId: string } | { _id: Types.ObjectId },
|
||||
work: (authority: Authority, session: ClientSession) => Promise<T>,
|
||||
): Promise<T> {
|
||||
if (connection.readyState !== 1 || activeTransactions >= 4)
|
||||
return deny("unavailable");
|
||||
activeTransactions++;
|
||||
const session = await connection.startSession().catch(() => {
|
||||
activeTransactions--;
|
||||
return deny("unavailable");
|
||||
});
|
||||
try {
|
||||
session.startTransaction({
|
||||
readConcern: { level: "snapshot" },
|
||||
writeConcern: { w: "majority", wtimeoutMS: 3000 },
|
||||
maxCommitTimeMS: 5000,
|
||||
});
|
||||
// Touch the authority documents in the same transaction as the receipt.
|
||||
// Concurrent disablement, ownership changes and coauthor removal then
|
||||
// conflict with this transaction instead of producing a stale consent.
|
||||
const user = (
|
||||
await connection.db.collection("users").findOneAndUpdate(
|
||||
{
|
||||
_id: new Types.ObjectId(who.accountId),
|
||||
$or: [{ status: "active" }, { status: { $exists: false } }],
|
||||
},
|
||||
{ $inc: { reviewConsentRevision: 1 } },
|
||||
{
|
||||
session,
|
||||
returnDocument: "after",
|
||||
maxTimeMS: 3000,
|
||||
projection: { externalIDs: 1 },
|
||||
},
|
||||
)
|
||||
).value;
|
||||
if (!user) return deny("forbidden");
|
||||
const githubId = user.externalIDs?.github;
|
||||
const roles: Record<string, unknown>[] = [
|
||||
{ owner: new Types.ObjectId(who.accountId) },
|
||||
];
|
||||
if (typeof githubId === "string" && /^[1-9][0-9]{0,19}$/.test(githubId))
|
||||
roles.push({ "coauthors.githubId": githubId });
|
||||
const repo = (
|
||||
await connection.db
|
||||
.collection("anonymizedrepositories")
|
||||
.findOneAndUpdate(
|
||||
{ ...repository, status: "ready", $or: roles },
|
||||
{ $inc: { reviewConsentRevision: 1 } },
|
||||
{
|
||||
session,
|
||||
returnDocument: "after",
|
||||
maxTimeMS: 3000,
|
||||
projection: { repoId: 1, owner: 1, options: 1 },
|
||||
},
|
||||
)
|
||||
).value;
|
||||
if (!repo) return deny("forbidden");
|
||||
if (
|
||||
repo.options?.expirationMode !== "never" &&
|
||||
repo.options?.expirationDate &&
|
||||
(!Number.isFinite(new Date(repo.options.expirationDate).getTime()) ||
|
||||
new Date(repo.options.expirationDate).getTime() <= Date.now())
|
||||
)
|
||||
return deny("expired");
|
||||
const authority: Authority = {
|
||||
accountId: who.accountId,
|
||||
repositoryId: String(repo._id),
|
||||
repositoryName: repo.repoId,
|
||||
role: String(repo.owner) === who.accountId ? "owner" : "coauthor",
|
||||
};
|
||||
const result = await work(authority, session);
|
||||
await session.commitTransaction();
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (session.inTransaction())
|
||||
await session.abortTransaction().catch(() => undefined);
|
||||
if (error instanceof ReviewConsentError) throw error;
|
||||
return deny("unavailable");
|
||||
} finally {
|
||||
activeTransactions--;
|
||||
await session.endSession().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
const completionCode = (id: string, nonce: string) =>
|
||||
createHmac("sha256", key)
|
||||
.update("4open.review-completion/1." + id + "." + nonce)
|
||||
.digest("hex");
|
||||
const api = {
|
||||
async handoff(actor: Principal, ticket: string, resolveCallback: (clientId: string, callbackId: string) => string | undefined) {
|
||||
const quote = decode(ticket, principal(actor));
|
||||
const callbackUrl = resolveCallback(quote.intent.clientId, quote.intent.callbackId);
|
||||
if (!callbackUrl) return deny("forbidden");
|
||||
const completion = await api.completion(actor, ticket);
|
||||
return Object.freeze({ completion, callbackUrl });
|
||||
},
|
||||
// Called only with the current authenticated browser principal. The HTTP
|
||||
// adapter must retain its session reload, same-origin and CSRF checks.
|
||||
async completion(actor: Principal, ticket: string) {
|
||||
const who = principal(actor),
|
||||
quote = decode(ticket, who);
|
||||
return transaction(
|
||||
who,
|
||||
{ _id: new Types.ObjectId(quote.repositoryId) },
|
||||
async (authority, session) => {
|
||||
decode(ticket, who);
|
||||
const id = quote.intent.clientId + ":" + quote.intent.intentId;
|
||||
const consent = await connection.db
|
||||
.collection<SavedConsent>("review_owner_consents")
|
||||
.findOne({ _id: id }, { session, maxTimeMS: 3000 });
|
||||
if (
|
||||
!consent ||
|
||||
consent.ticketHash !== hash(ticket) ||
|
||||
consent.accountId !== who.accountId ||
|
||||
consent.repositoryId !== authority.repositoryId ||
|
||||
!consent.intent
|
||||
)
|
||||
return deny("forbidden");
|
||||
const collection =
|
||||
connection.db.collection<SavedCompletion>("review_completions");
|
||||
let saved = await collection.findOne(
|
||||
{ _id: id },
|
||||
{ session, maxTimeMS: 3000 },
|
||||
);
|
||||
if (
|
||||
saved &&
|
||||
(saved.ticketHash !== consent.ticketHash ||
|
||||
saved.accountId !== who.accountId ||
|
||||
saved.repositoryId !== authority.repositoryId)
|
||||
)
|
||||
return deny("conflict");
|
||||
if (!saved) {
|
||||
const nonce = randomBytes(32).toString("hex");
|
||||
saved = {
|
||||
_id: id,
|
||||
accountId: who.accountId,
|
||||
repositoryId: authority.repositoryId,
|
||||
ticketHash: consent.ticketHash,
|
||||
intent: consent.intent,
|
||||
nonce,
|
||||
codeHash: hash(completionCode(id, nonce)),
|
||||
expiresAt: new Date(
|
||||
Math.floor(
|
||||
Math.min(
|
||||
Date.now() + 300000,
|
||||
Date.parse(consent.intent.expiresAt),
|
||||
Date.parse(consent.intent.policy.retainUntil),
|
||||
) / 1000,
|
||||
) * 1000,
|
||||
)
|
||||
.toISOString()
|
||||
.replace(/\.000Z$/, "Z"),
|
||||
};
|
||||
await collection.insertOne(saved, { session, maxTimeMS: 3000 });
|
||||
}
|
||||
if (Date.parse(saved.expiresAt) <= Date.now()) return deny("expired");
|
||||
const code = completionCode(id, saved.nonce);
|
||||
// A key rotation must not silently issue a different code for a saved
|
||||
// intent. Keep the issuing key available for the short code lifetime.
|
||||
if (hash(code) !== saved.codeHash) return deny("conflict");
|
||||
return Object.freeze({
|
||||
contract: "4open.artifacts/1",
|
||||
clientId: saved.intent.clientId,
|
||||
intentId: saved.intent.intentId,
|
||||
code,
|
||||
expiresAt: saved.expiresAt,
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
// authenticatedClientId must come from service credential verification,
|
||||
// never browser identity or the request body's clientId alone.
|
||||
async exchange(
|
||||
authenticatedClientId: string,
|
||||
input: {
|
||||
contract: string;
|
||||
clientId: string;
|
||||
intentId: string;
|
||||
code: string;
|
||||
requestId: string;
|
||||
},
|
||||
): Promise<BindingReceipt> {
|
||||
if (
|
||||
!input ||
|
||||
Object.keys(input).sort().join(",") !==
|
||||
"clientId,code,contract,intentId,requestId" ||
|
||||
input.contract !== "4open.artifacts/1" ||
|
||||
![input.clientId, input.intentId, input.requestId].every(
|
||||
(value) => typeof value === "string" && /^[a-f0-9]{32}$/.test(value),
|
||||
) ||
|
||||
typeof input.code !== "string" ||
|
||||
!/^[a-f0-9]{64}$/.test(input.code)
|
||||
)
|
||||
return deny("invalid");
|
||||
const command = { ...input };
|
||||
if (authenticatedClientId !== command.clientId) return deny("forbidden");
|
||||
if (connection.readyState !== 1 || activeReads >= 4)
|
||||
return deny("unavailable");
|
||||
const id = command.clientId + ":" + command.intentId;
|
||||
const collection =
|
||||
connection.db.collection<SavedCompletion>("review_completions");
|
||||
const read = async (session?: ClientSession) => {
|
||||
const saved = await collection.findOne(
|
||||
{ _id: id },
|
||||
{ session, maxTimeMS: 3000, ...(session ? {} : { readConcern: { level: "majority" as const } }) },
|
||||
);
|
||||
if (!saved) return deny("forbidden");
|
||||
if (
|
||||
!timingSafeEqual(
|
||||
Buffer.from(saved.codeHash, "hex"),
|
||||
Buffer.from(hash(command.code), "hex"),
|
||||
)
|
||||
)
|
||||
return deny(
|
||||
saved.exchange?.requestId === command.requestId
|
||||
? "conflict"
|
||||
: "forbidden",
|
||||
);
|
||||
return saved;
|
||||
};
|
||||
activeReads++;
|
||||
try {
|
||||
const before = await read();
|
||||
if (before.exchange) {
|
||||
if (before.exchange.requestId !== command.requestId)
|
||||
return deny("conflict");
|
||||
return Object.freeze(before.exchange.receipt);
|
||||
}
|
||||
return await transaction(
|
||||
{ accountId: before.accountId, sessionHash: "" },
|
||||
{ _id: new Types.ObjectId(before.repositoryId) },
|
||||
async (_authority, session) => {
|
||||
const saved = await read(session);
|
||||
if (saved.exchange) {
|
||||
if (saved.exchange.requestId !== command.requestId)
|
||||
return deny("conflict");
|
||||
return Object.freeze(saved.exchange.receipt);
|
||||
}
|
||||
if (
|
||||
Date.parse(saved.expiresAt) <= Date.now() ||
|
||||
Date.parse(saved.intent.policy.retainUntil) <= Date.now()
|
||||
)
|
||||
return deny("expired");
|
||||
// The unique request document detects a reused key on another intent.
|
||||
const requests = connection.db.collection<{
|
||||
_id: string;
|
||||
intentId: string;
|
||||
}>("review_completion_requests");
|
||||
const requestKey = command.clientId + ":" + command.requestId;
|
||||
if (
|
||||
await requests.findOne(
|
||||
{ _id: requestKey },
|
||||
{ session, maxTimeMS: 3000 },
|
||||
)
|
||||
)
|
||||
return deny("conflict");
|
||||
const receipt: BindingReceipt = {
|
||||
contract: "4open.artifacts/1",
|
||||
clientId: saved.intent.clientId,
|
||||
intentId: saved.intent.intentId,
|
||||
bindingId: randomBytes(16).toString("hex"),
|
||||
submissionRef: saved.intent.submissionRef,
|
||||
policy: { ...saved.intent.policy },
|
||||
entitlementId: saved.intent.entitlementId,
|
||||
};
|
||||
await connection.db
|
||||
.collection<{
|
||||
_id: string;
|
||||
clientId: string;
|
||||
intentId: string;
|
||||
accountId: string;
|
||||
repositoryId: string;
|
||||
state: "pending";
|
||||
revision: number;
|
||||
receipt: BindingReceipt;
|
||||
}>("review_artifact_bindings")
|
||||
.insertOne(
|
||||
{
|
||||
_id: receipt.bindingId,
|
||||
clientId: command.clientId,
|
||||
intentId: command.intentId,
|
||||
accountId: saved.accountId,
|
||||
repositoryId: saved.repositoryId,
|
||||
state: "pending",
|
||||
revision: 1,
|
||||
receipt,
|
||||
},
|
||||
{ session, maxTimeMS: 3000 },
|
||||
);
|
||||
await requests.insertOne(
|
||||
{ _id: requestKey, intentId: command.intentId },
|
||||
{ session, maxTimeMS: 3000 },
|
||||
);
|
||||
await collection.updateOne(
|
||||
{ _id: id },
|
||||
{ $set: { exchange: { requestId: command.requestId, receipt } } },
|
||||
{ session, maxTimeMS: 3000 },
|
||||
);
|
||||
return Object.freeze(receipt);
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof ReviewConsentError) throw error;
|
||||
return deny("unavailable");
|
||||
} finally {
|
||||
activeReads--;
|
||||
}
|
||||
},
|
||||
async receipt(
|
||||
authenticatedClientId: string,
|
||||
intentId: string,
|
||||
): Promise<BindingReceipt> {
|
||||
if (
|
||||
![authenticatedClientId, intentId].every(
|
||||
(value) => typeof value === "string" && /^[a-f0-9]{32}$/.test(value),
|
||||
)
|
||||
)
|
||||
return deny("invalid");
|
||||
if (connection.readyState !== 1 || activeReads >= 4)
|
||||
return deny("unavailable");
|
||||
activeReads++;
|
||||
try {
|
||||
const saved = await connection.db
|
||||
.collection<SavedCompletion>("review_completions")
|
||||
.findOne(
|
||||
{ _id: authenticatedClientId + ":" + intentId },
|
||||
{ maxTimeMS: 3000, readConcern: { level: "majority" }, projection: { exchange: 1 } },
|
||||
);
|
||||
if (!saved?.exchange) return deny("forbidden");
|
||||
return Object.freeze(saved.exchange.receipt);
|
||||
} catch (error) {
|
||||
if (error instanceof ReviewConsentError) throw error;
|
||||
return deny("unavailable");
|
||||
} finally {
|
||||
activeReads--;
|
||||
}
|
||||
},
|
||||
async preview(
|
||||
actor: Principal,
|
||||
repositoryName: string,
|
||||
input: IntentConsumption,
|
||||
signal?: AbortSignal,
|
||||
) {
|
||||
const who = principal(actor);
|
||||
const command = { ...input };
|
||||
if (
|
||||
typeof repositoryName !== "string" ||
|
||||
!/^[A-Za-z0-9_-]{3,128}$/.test(repositoryName)
|
||||
)
|
||||
return deny("invalid");
|
||||
const before = await transaction(
|
||||
who,
|
||||
{ repoId: repositoryName },
|
||||
async (authority) => authority,
|
||||
);
|
||||
// The provider call holds no database transaction or authority lock.
|
||||
const intent = await consumer.consume(command, signal);
|
||||
const current = await transaction(
|
||||
who,
|
||||
{ _id: new Types.ObjectId(before.repositoryId) },
|
||||
async (authority) => authority,
|
||||
);
|
||||
if (
|
||||
signal?.aborted ||
|
||||
Date.parse(intent.expiresAt) <= Date.now() ||
|
||||
Date.parse(intent.policy.retainUntil) <= Date.now()
|
||||
)
|
||||
return deny("expired");
|
||||
const quote: Quote = {
|
||||
version: 1,
|
||||
accountId: who.accountId,
|
||||
sessionHash: who.sessionHash,
|
||||
repositoryId: current.repositoryId,
|
||||
intent,
|
||||
};
|
||||
const body = Buffer.from(JSON.stringify(quote)).toString("base64url");
|
||||
return Object.freeze({
|
||||
ticket: body + "." + signature(body).toString("base64url"),
|
||||
repositoryName: current.repositoryName,
|
||||
policy: intent.policy,
|
||||
expiresAt: intent.expiresAt,
|
||||
});
|
||||
},
|
||||
async confirm(
|
||||
actor: Principal,
|
||||
ticket: string,
|
||||
input: {
|
||||
requestId: string;
|
||||
acceptAccess: boolean;
|
||||
acceptRetention: boolean;
|
||||
},
|
||||
): Promise<ConsentReceipt> {
|
||||
const who = principal(actor);
|
||||
if (
|
||||
!input ||
|
||||
Object.keys(input).sort().join(",") !==
|
||||
"acceptAccess,acceptRetention,requestId" ||
|
||||
input.acceptAccess !== true ||
|
||||
input.acceptRetention !== true ||
|
||||
typeof input.requestId !== "string" ||
|
||||
!/^[a-f0-9]{32}$/.test(input.requestId)
|
||||
)
|
||||
return deny("invalid");
|
||||
const quote = decode(ticket, who),
|
||||
requestId = input.requestId;
|
||||
return transaction(
|
||||
who,
|
||||
{ _id: new Types.ObjectId(quote.repositoryId) },
|
||||
async (authority, session) => {
|
||||
decode(ticket, who); // Recheck expiry after waiting for authority locks.
|
||||
const collection = connection.db.collection<SavedConsent>(
|
||||
"review_owner_consents",
|
||||
);
|
||||
const id = quote.intent.clientId + ":" + quote.intent.intentId;
|
||||
const ticketHash = hash(ticket);
|
||||
// A string _id binds each intent to at most one accepted repository and
|
||||
// session. No raw session ID, intent token or service key is persisted.
|
||||
const existing = await collection.findOne(
|
||||
{ _id: id },
|
||||
{ session, maxTimeMS: 3000 },
|
||||
);
|
||||
if (existing) {
|
||||
if (
|
||||
existing.ticketHash !== ticketHash ||
|
||||
existing.requestId !== requestId ||
|
||||
existing.accountId !== who.accountId ||
|
||||
existing.repositoryId !== authority.repositoryId
|
||||
)
|
||||
return deny("conflict");
|
||||
return Object.freeze(existing.receipt as ConsentReceipt);
|
||||
}
|
||||
const receipt: ConsentReceipt = Object.freeze({
|
||||
clientId: quote.intent.clientId,
|
||||
intentId: quote.intent.intentId,
|
||||
accountId: who.accountId,
|
||||
repositoryId: authority.repositoryId,
|
||||
role: authority.role,
|
||||
policy: quote.intent.policy,
|
||||
requestId,
|
||||
confirmedAt: new Date().toISOString(),
|
||||
});
|
||||
await collection.insertOne(
|
||||
{
|
||||
_id: id,
|
||||
accountId: who.accountId,
|
||||
repositoryId: authority.repositoryId,
|
||||
requestId,
|
||||
ticketHash,
|
||||
receipt,
|
||||
intent: quote.intent,
|
||||
},
|
||||
{ session, maxTimeMS: 3000 },
|
||||
);
|
||||
return receipt;
|
||||
},
|
||||
);
|
||||
},
|
||||
};
|
||||
return Object.freeze(api);
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import { createHash, timingSafeEqual } from "crypto";
|
||||
import { Request } from "express";
|
||||
import { decodeReviewJSON } from "./review-json";
|
||||
|
||||
type Scope = "completion.exchange" | "receipt.read";
|
||||
type Key = {
|
||||
clientId: string;
|
||||
digest: Buffer;
|
||||
from: number;
|
||||
until: number;
|
||||
scopes: Scope[];
|
||||
};
|
||||
const opaque = /^[a-f0-9]{32}$/;
|
||||
const secret = /^[a-f0-9]{64}$/;
|
||||
const invalid = (): never => {
|
||||
throw new Error("Invalid review exchange credentials");
|
||||
};
|
||||
function fields(value: unknown, names: string[]): Record<string, unknown> {
|
||||
if (
|
||||
!value ||
|
||||
typeof value !== "object" ||
|
||||
Array.isArray(value) ||
|
||||
Object.keys(value).sort().join(",") !== names.sort().join(",")
|
||||
)
|
||||
return invalid();
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
function date(value: unknown): number {
|
||||
if (
|
||||
typeof value !== "string" ||
|
||||
!/^20[0-9]{2}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/.test(value)
|
||||
)
|
||||
return invalid();
|
||||
const number = Date.parse(value);
|
||||
if (
|
||||
!Number.isFinite(number) ||
|
||||
new Date(number).toISOString() !== value.replace("Z", ".000Z")
|
||||
)
|
||||
return invalid();
|
||||
return number;
|
||||
}
|
||||
export function singleReviewHeader(
|
||||
req: Request,
|
||||
name: string,
|
||||
): string | undefined {
|
||||
let count = 0;
|
||||
for (let i = 0; i < req.rawHeaders.length; i += 2)
|
||||
if (req.rawHeaders[i].toLowerCase() === name) count++;
|
||||
const value = req.headers[name];
|
||||
return count === 1 && typeof value === "string" ? value : undefined;
|
||||
}
|
||||
|
||||
/** Separate scoped registry: the capabilities-only registry is not accepted. */
|
||||
export function createReviewServiceAuth(
|
||||
raw: string,
|
||||
now: () => number = Date.now,
|
||||
) {
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = decodeReviewJSON(Buffer.from(raw, "utf8"));
|
||||
} catch {
|
||||
return invalid();
|
||||
}
|
||||
const config = fields(parsed, ["version", "keys"]);
|
||||
if (
|
||||
config.version !== 1 ||
|
||||
!Array.isArray(config.keys) ||
|
||||
config.keys.length < 1 ||
|
||||
config.keys.length > 64
|
||||
)
|
||||
return invalid();
|
||||
const keys = new Map<string, Key>(),
|
||||
digests = new Set<string>(),
|
||||
clients = new Map<string, number>();
|
||||
for (const entry of config.keys) {
|
||||
const k = fields(entry, [
|
||||
"clientId",
|
||||
"keyId",
|
||||
"tokenSHA256",
|
||||
"notBefore",
|
||||
"notAfter",
|
||||
"scopes",
|
||||
]);
|
||||
if (
|
||||
typeof k.clientId !== "string" ||
|
||||
!opaque.test(k.clientId) ||
|
||||
typeof k.keyId !== "string" ||
|
||||
!/^[A-Za-z0-9_-]{1,64}$/.test(k.keyId) ||
|
||||
typeof k.tokenSHA256 !== "string" ||
|
||||
!secret.test(k.tokenSHA256) ||
|
||||
!Array.isArray(k.scopes) ||
|
||||
k.scopes.length < 1 ||
|
||||
k.scopes.length > 2 ||
|
||||
k.scopes.some(
|
||||
(scope) => scope !== "completion.exchange" && scope !== "receipt.read",
|
||||
) ||
|
||||
new Set(k.scopes).size !== k.scopes.length
|
||||
)
|
||||
return invalid();
|
||||
const from = date(k.notBefore),
|
||||
until = date(k.notAfter),
|
||||
id = k.clientId + ":" + k.keyId;
|
||||
const count = (clients.get(k.clientId) || 0) + 1;
|
||||
if (
|
||||
until <= from ||
|
||||
keys.has(id) ||
|
||||
digests.has(k.tokenSHA256) ||
|
||||
count > 16
|
||||
)
|
||||
return invalid();
|
||||
clients.set(k.clientId, count);
|
||||
if (clients.size > 32) return invalid();
|
||||
digests.add(k.tokenSHA256);
|
||||
keys.set(id, {
|
||||
clientId: k.clientId,
|
||||
digest: Buffer.from(k.tokenSHA256, "hex"),
|
||||
from,
|
||||
until,
|
||||
scopes: [...k.scopes] as Scope[],
|
||||
});
|
||||
}
|
||||
const dummy = Buffer.alloc(32);
|
||||
return Object.freeze({
|
||||
authenticate(
|
||||
req: Request,
|
||||
scope: Scope,
|
||||
): { clientId: string; valid: () => boolean } | undefined {
|
||||
const client = singleReviewHeader(req, "x-4open-artifact-client-id"),
|
||||
keyId = singleReviewHeader(req, "x-4open-artifact-service-key-id"),
|
||||
authorization = singleReviewHeader(req, "authorization");
|
||||
if (
|
||||
!client ||
|
||||
!opaque.test(client) ||
|
||||
!keyId ||
|
||||
!/^[A-Za-z0-9_-]{1,64}$/.test(keyId) ||
|
||||
!authorization ||
|
||||
!/^Bearer [a-f0-9]{64}$/i.test(authorization) ||
|
||||
!secret.test(authorization.slice(7))
|
||||
)
|
||||
return undefined;
|
||||
const key = keys.get(client + ":" + keyId);
|
||||
const matches = timingSafeEqual(
|
||||
createHash("sha256").update(authorization.slice(7), "ascii").digest(),
|
||||
key?.digest || dummy,
|
||||
);
|
||||
const valid = () => {
|
||||
const time = now();
|
||||
return (
|
||||
!!key &&
|
||||
matches &&
|
||||
key.scopes.includes(scope) &&
|
||||
Number.isFinite(time) &&
|
||||
time >= key.from &&
|
||||
time < key.until
|
||||
);
|
||||
};
|
||||
return valid() ? Object.freeze({ clientId: client, valid }) : undefined;
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user