Files
puter/src/backend/services/metering/enforcement.ts
Daniel Salazar 589a98b12c feat(kv): share KV prefixes with other users at watch/read/write, and bound what apps can be granted (#4331)
* feat(kv): share key-value prefixes with other users, and bound what apps can be granted

- KV share handles move out of Events into KvShareService, served at /kv/shares
  (the /events/kv-handles routes stay as an alias).
- Shares have watch, read and write levels, encoded in the permission root
  (kv-share, kv-share-read, kv-share-write) with write implying read and watch.
- Every puter.kv method takes optConfig.handle; keys are relative to the shared
  prefix, cursors are sealed, and private entries stay hidden.
- New puter.kv.share, unshare, listShared and listSharedByMe; optional label;
  kv.share.received notice in the app's mailbox only; recipient blocks apply.
- puter.kv.set gains onlyIfAbsent.
- fs and kv events gain viaWorker, and kv events report self from the writer.
- /auth/grant-user-app only accepts canonical permissions from a fixed set of
  families; the consent dialog, puter.perms and docs follow.
- Migrations: kv_share_handles gains revoked_by, label, and grantee and app
  indexes (mysql_mig_49, postgres_mig_38, sqlite 0095).

* fix(kv): never name a private key in a cross-app or handle cursor, and bound flat-permission scans

- Listings of another namespace (cross-app or through a handle) resume after
  the last entry they returned, and their cursors are sealed, bound to the
  namespace or handle, and padded to one length. Old plain cross-app cursors
  are still read when they name the target namespace; other namespaces get 400.
- PermissionStore scans flat permissions a few roots at a time and skips
  roots nested under another root in the same call.
2026-10-25 08:16:19 -07:00

314 lines
11 KiB
TypeScript

/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { Request } from 'express';
import type { EventClient } from '../../clients/event/EventClient';
import type { Actor } from '../../core/actor';
import { isSystemActor } from '../../core/actor';
import { HttpError } from '../../core/http/HttpError.js';
import type { IConfig } from '../../types';
import { isFreeSubscription } from './consts.js';
import type { MeteringService } from './MeteringService';
// -- Credit enforcement ----------------------------------------------
//
// Storage and KV usage is recorded per request and settles seconds behind
// the traffic, so there is nothing to enforce at the point it is measured.
// What can be enforced is the state that usage produced: an account with
// nothing left of its budget is turned away from the operations that spend
// it, on the way in.
//
// Which operations those are is a per-route/per-method decision made where
// the surface is declared (`RouteOptions.requireCredits`, the KV driver's
// exempt list). Two rules hold across all of them:
//
// - Only spending is gated. Listing, stat-ing and deleting stay open: an
// account that has run out still has to be able to see what it has and
// get rid of it, and turning away the operations that free resources
// leaves no way back other than paying.
// - Only the account's own traffic is gated. Serving a hosted site is
// billed to the account hosting it but driven by visitors who have no
// say in its balance, so it is metered and never blocked.
/**
* The part of the metering service the credit check calls. Narrowed from the
* service itself so a gate can't drift from it, and optional at the call site:
* a deployment without metering enforces nothing.
*/
export type CreditMetering = Pick<MeteringService, 'hasAnyUsageCached'>;
/** Config knobs; see `IConfig.meteringEnforcement`. */
type EnforcementConfig = Pick<IConfig, 'meteringEnforcement'>;
export const enforcementEnabled = (config: EnforcementConfig): boolean =>
config.meteringEnforcement?.enabled !== false;
/**
* Actors whose usage is recorded but never blocked.
*
* A worker is a deployed program rather than someone sitting in front of a
* screen: it finds out it has been cut off by failing mid-run, with no prompt
* to read and nobody to act on it. Workers are exempt until that failure has
* somewhere to surface — `meteringEnforcement.workers` turns it on.
*/
export const creditEnforcementExempt = (
actor: Actor | undefined,
config: EnforcementConfig,
): boolean => {
if (!actor?.user?.uuid) return true;
if (isSystemActor(actor)) return true;
if (
actor.session?.kind === 'worker' &&
config.meteringEnforcement?.workers !== true
) {
return true;
}
return false;
};
/**
* Reject an actor with nothing left to spend. Same status and code the AI
* surfaces use, so a client that already handles one handles this.
*/
export const assertActorHasCredits = async (
metering: CreditMetering | undefined,
actor: Actor | undefined,
config: EnforcementConfig,
): Promise<void> => {
if (!metering) return;
if (!enforcementEnabled(config)) return;
if (creditEnforcementExempt(actor, config)) return;
if (!(await metering.hasAnyUsageCached(actor!))) {
throw insufficientCreditsError();
}
};
/** The 402 for a request the account's remaining credits can't cover. */
export const insufficientCreditsError = (): HttpError =>
new HttpError(
402,
"Your account doesn't have enough credits left for this request. Upgrade your plan at https://puter.com/#billing to continue.",
{ legacyCode: 'insufficient_funds' },
);
/** The part of the metering service the subscription check calls. */
export type SubscriptionMetering = Pick<
MeteringService,
'getActorSubscription'
>;
/**
* `true`: any plan `isFreeSubscription` doesn't consider free. An array: only
* those policy ids. `false`: no requirement.
*/
export type SubscriptionRequirement = boolean | readonly string[];
/**
* Validate a declared requirement at boot. An empty array is an error: it reads
* as "subscribers only" while admitting everybody.
*/
export const validateSubscriptionRequirement = (
value: unknown,
label: string,
): SubscriptionRequirement => {
if (typeof value === 'boolean') return value;
if (Array.isArray(value)) {
if (value.length === 0) {
throw new Error(
`${label}: expected at least one subscription id, or true`,
);
}
for (const id of value) {
if (typeof id !== 'string' || id.length === 0) {
throw new Error(`${label}: subscription ids must be strings`);
}
}
return value as readonly string[];
}
throw new Error(`${label}: expected true/false or an array of ids`);
};
/** Whether a resolved policy id satisfies a requirement. */
export const subscriptionSatisfies = (
id: string,
requirement: SubscriptionRequirement,
): boolean => {
// No requirement is satisfied by every plan — `false` and `[]` mean the
// surface asked for nothing, not that nothing passes.
if (requirement === false) return true;
if (Array.isArray(requirement)) {
return requirement.length === 0 || requirement.includes(id);
}
return !isFreeSubscription(id);
};
/**
* Plan gates ride on the same master switch as the rest of enforcement, so
* `meteringEnforcement.enabled: false` is the one knob that stops metering
* turning traffic away. `subscriptions` narrows it to the plan gates alone.
*/
export const subscriptionEnforcementEnabled = (
config: EnforcementConfig,
): boolean =>
enforcementEnabled(config) &&
config.meteringEnforcement?.subscriptions !== false;
/**
* The cases in which no plan is asked about at all: nothing was required, there
* is no metering to ask, plan gates are switched off, or the caller is the
* system. Shared by the check and the assertion so the two cannot drift.
*/
const subscriptionCheckWaived = (
metering: SubscriptionMetering | undefined,
actor: Actor | undefined,
requirement: SubscriptionRequirement,
config: EnforcementConfig,
): boolean =>
requirement === false ||
(Array.isArray(requirement) && requirement.length === 0) ||
!metering ||
!subscriptionEnforcementEnabled(config) ||
Boolean(actor && isSystemActor(actor));
/**
* Whether an actor's plan covers a plan-gated surface right now. The same
* question {@link assertActorHasSubscription} asks of a caller, as a yes or no —
* for an account that is not the caller (an owner whose link share is being
* read or listed), where there is no request to refuse.
*/
export const actorHasSubscription = async (
metering: SubscriptionMetering | undefined,
actor: Actor | undefined,
requirement: SubscriptionRequirement,
config: EnforcementConfig,
): Promise<boolean> => {
if (subscriptionCheckWaived(metering, actor, requirement, config)) {
return true;
}
if (!actor?.user?.uuid) return false;
const subscription = await metering!.getActorSubscription(actor);
return subscriptionSatisfies(subscription.id, requirement);
};
/**
* Whether the actor is on a paid plan — the fact, not the gate. Unlike
* {@link actorHasSubscription} this ignores the enforcement switches: it is for
* a caller reading the plan as evidence (a paying account has a card on file
* with the billing provider) rather than as an entitlement to enforce. False
* with no metering to ask, or no account behind the actor.
*/
export const actorOnPaidPlan = async (
metering: SubscriptionMetering | undefined,
actor: Actor | undefined,
): Promise<boolean> => {
if (!metering || !actor?.user?.uuid) return false;
const subscription = await metering.getActorSubscription(actor);
return subscriptionSatisfies(subscription.id, true);
};
/**
* The actor's plan id, for tiered limits. `null` with no metering to ask, or
* when the lookup fails, which the tier tables read as their default.
*/
export const actorPlanId = async (
metering: SubscriptionMetering | undefined,
actor: Actor,
): Promise<string | null> => {
if (!metering) return null;
try {
return (await metering.getActorSubscription(actor)).id;
} catch (err) {
console.warn('[metering] could not resolve a plan', err);
return null;
}
};
/**
* Where a plan gate sits, so a `subscription.gate.<surface>` listener can waive
* it for a call it recognizes. `surface` is the key segment after
* `subscription.gate.`, e.g. `route.post.puterai.openai.v1.chat.completions`.
*/
export interface SubscriptionGateHook {
events: Pick<EventClient, 'emitAndWait'> | undefined;
surface: string;
req?: Request;
}
const waivedByHook = async (
hook: SubscriptionGateHook | undefined,
actor: Actor,
requirement: SubscriptionRequirement,
): Promise<boolean> => {
if (!hook?.events) return false;
const event = {
actor,
surface: hook.surface,
requirement,
req: hook.req,
allow: false,
};
await hook.events.emitAndWait(
`subscription.gate.${hook.surface}`,
event,
{},
);
return event.allow === true;
};
/**
* Reject a caller whose plan doesn't cover the surface they're calling.
*
* Unlike the credit check, a worker session is not exempt: a worker acts for an
* account, and an account's entitlements don't widen because a program is
* driving them. The system actor still passes, and so does a deployment with no
* metering wired — there are no plans to be on.
*
* The answer comes from the metering service's per-actor subscription cache, so
* this normally costs a map lookup. A caller the plan doesn't cover is offered
* to `hook` listeners before being refused.
*/
export const assertActorHasSubscription = async (
metering: SubscriptionMetering,
actor: Actor | undefined,
requirement: SubscriptionRequirement,
config: EnforcementConfig,
hook?: SubscriptionGateHook,
): Promise<void> => {
if (subscriptionCheckWaived(metering, actor, requirement, config)) return;
if (!actor?.user?.uuid) {
throw new HttpError(403, 'A subscription is required for this action', {
legacyCode: 'subscription_required',
});
}
const subscription = await metering!.getActorSubscription(actor);
if (subscriptionSatisfies(subscription.id, requirement)) return;
if (await waivedByHook(hook, actor, requirement)) return;
throw new HttpError(402, 'A subscription is required for this action', {
legacyCode: 'subscription_required',
fields: {
subscription: subscription.id,
...(Array.isArray(requirement) ? { required: requirement } : {}),
},
});
};