mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-26 07:37:54 +00:00
* perf(workers): skip hot-reload lookups for files no worker is bound to
The fs.write.file / fs.remove.node / fs.move.node listeners listed every
workers.puter.* row of the file's owner on every event, for every user.
The event's FSEntry already carries the worker rows bound to it, so a file
with none returns before any I/O; a bound file reads just its own rows,
batched, from the primary.
* perf(apps): read the uid alias only after a direct lookup misses
Every AppDriver uid lookup fired an uncached system-KV GetItem for the
merge alias in parallel with the direct read, even when the direct read
hit. The alias is now read only on a miss.
* refactor(apps): one app view with the launch gates, built by AppService
AppDriver's serializer and its private-app / hosted-backing gates move to
AppService.views, built on a pure toAppView that is the one place
index_url is withheld. AppDriver, the subdomains driver's embedded apps and
the GUI shell view all use it. Visibility of protected apps
(filterReadable) moves with it.
Subdomain embeds (associated_app, app_owner) used to return index_url with
no gate; they now withhold it from a viewer who may not open a private app
or whose hosted backing is gone, and carry privateAccess, godmode and
feedback_enabled like a direct app read.
* refactor(apps): one toAppSummary; batch the recommended and taskbar lookups
SuggestedApps, RecommendedApps, /get-launch-apps and the taskbar each built
their own launch summary; they now share toAppSummary (the extra
feedback_enabled / external flags appear on the lists that lacked them).
getRecommendedApps awaited getByName once per recommended app (29) and
getTaskbarItems did the same per taskbar entry on every whoami; both now
make one batched call per key through the new AppStore.getByNames, which
keeps getByName's case-insensitive and old-name matching. The taskbar's
raw UPDATE of the user row goes through UserStore.update, and
/get-launch-apps checks hosted backings in one batch and reaches the
stores without casts.
* refactor(apps): app routes read through AppService; /delete-site calls the subdomains driver
AppController used the global driversContainers through a getter whose
boot-order comment no longer held; it uses the injected drivers and
AppService. GET /apps/:name looked each name up, read it again through the
driver and resolved private access a second time, emitting
app.privateAccess.resolveLaunch twice per private app; it now batches the
names and resolves once. POST /query/app did a lookup, a driver read and a
raw app_filetype_association SELECT per selector (up to 200, serially); it
now makes one batched lookup per key, one grant check and one filetype read.
The driver's read shape is a pure toAppReadView both use.
HostingController's /delete-site re-implemented the subdomains driver's
delete (protected check, delete, subdomain.delete event); it keeps its
owner-scoped 404 and calls the driver. Hosting and Desktop controllers drop
constructors that discarded the drivers argument, and trivial getters.
* refactor(drivers): one per-method policy validator and resolver; drop @Driver
Four policy blocks (rateLimit, concurrent, requireSubscription,
requireReputation) share the { default, methods } shape but each had its
own validator and resolver. They become validatePerMethod(value, label,
policy) and resolvePerMethod(cfg, method); error messages are unchanged.
The @Driver class decorator and the prototype keys it wrote had no users
outside its own tests; drivers declare their fields, and resolveDriverMeta
reads only those. DriverController takes the driver name from the meta it
already resolved instead of probing the prototype.
resolveDriverMethodRateLimit/Concurrent stay as deprecated aliases for the
email extension.
* refactor(drivers): shared actor guards and crud-q concurrency policy
The apps, subdomains, workers and notifications drivers each carried a
private #requireActor, two carried the same #requireVerified, and apps and
subdomains the same owner/app_owner write predicate. These become
requireContextActor, requireContextUserActor, assertActorEmailVerified and
actorOwnsRow in core/actor.ts; each driver keeps its own error for the
cases that differed (system actors still read apps; notifications still
answer 403 for an actor without a user). The identical crud-q concurrency
block is one CRUD_CONCURRENT constant.
The metering, installedApps and appTelemetry extensions re-checked an actor
their routes already require; those checks and the tests for them go.
* refactor(extensions): store methods instead of raw SQL in OSS extensions
installedApps and appTelemetry queried user_to_app_permissions directly
and metering read apps by name with its own SELECT. PermissionStore gains
listAppsGrantedByUser, listAppAuthenticatedUsers (with the email-share
check folded in) and countAppUsers; metering uses AppStore.getByName.
* refactor(server): one layer builder and one lifecycle hook runner
PuterServer built each of its five layers with its own copy of the same
loop (built-ins, then extension additions, into the layer and its export
container), and ran each lifecycle hook with five near-identical loops per
hook. Both are now one helper each: #instantiateLayer and
#runHook(order, hook). Construction order, the args each layer receives,
and hook order (start and prepare bottom-up, shutdown top-down) are
unchanged.
* fix(workers): tear down a deleted source's worker after its FK nulls root_dir_id
118 lines
3.9 KiB
TypeScript
118 lines
3.9 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 { describe, expect, it } from 'vitest';
|
||
import {
|
||
actorOwnsRow,
|
||
assertActorEmailVerified,
|
||
makeActor,
|
||
requireContextActor,
|
||
requireContextUserActor,
|
||
SYSTEM_ACTOR,
|
||
type Actor,
|
||
} from './actor.js';
|
||
import { runWithContext } from './context.js';
|
||
|
||
const user = { id: 7, uuid: 'u-7', username: 'dev', email_confirmed: true };
|
||
const plain = makeActor({ user });
|
||
const asApp = (appId: number) =>
|
||
makeActor({ user, app: { id: appId, uid: `app-${appId}` } });
|
||
|
||
describe('requireContextActor', () => {
|
||
it('returns the request actor and 401s without one', () => {
|
||
expect(runWithContext({ actor: plain }, requireContextActor)).toBe(
|
||
plain,
|
||
);
|
||
expect(() =>
|
||
runWithContext({ actor: undefined }, requireContextActor),
|
||
).toThrow(expect.objectContaining({ statusCode: 401 }));
|
||
});
|
||
|
||
it('accepts an actor without a user id', () => {
|
||
expect(
|
||
runWithContext({ actor: SYSTEM_ACTOR }, requireContextActor),
|
||
).toBe(SYSTEM_ACTOR);
|
||
});
|
||
});
|
||
|
||
describe('requireContextUserActor', () => {
|
||
it('returns an actor acting for a user', () => {
|
||
expect(runWithContext({ actor: plain }, requireContextUserActor)).toBe(
|
||
plain,
|
||
);
|
||
});
|
||
|
||
it('401s without an actor or without a user id', () => {
|
||
for (const actor of [undefined, SYSTEM_ACTOR]) {
|
||
expect(() =>
|
||
runWithContext({ actor }, requireContextUserActor),
|
||
).toThrow(
|
||
expect.objectContaining({
|
||
statusCode: 401,
|
||
legacyCode: 'unauthorized',
|
||
}),
|
||
);
|
||
}
|
||
});
|
||
});
|
||
|
||
describe('assertActorEmailVerified', () => {
|
||
const unverified: Actor = makeActor({
|
||
user: { ...user, email_confirmed: false },
|
||
});
|
||
|
||
it('is inert unless strict verification is configured', () => {
|
||
expect(() => assertActorEmailVerified(unverified, {})).not.toThrow();
|
||
});
|
||
|
||
it('rejects an unverified actor with 400 under strict verification', () => {
|
||
const strict = { strict_email_verification_required: true };
|
||
expect(() => assertActorEmailVerified(plain, strict)).not.toThrow();
|
||
expect(() => assertActorEmailVerified(unverified, strict)).toThrow(
|
||
expect.objectContaining({
|
||
statusCode: 400,
|
||
legacyCode: 'account_is_not_verified',
|
||
}),
|
||
);
|
||
});
|
||
});
|
||
|
||
describe('actorOwnsRow', () => {
|
||
it('grants the owning user acting as themselves', () => {
|
||
expect(actorOwnsRow(plain, { ownerUserId: 7, appOwnerId: 3 })).toBe(
|
||
true,
|
||
);
|
||
expect(actorOwnsRow(plain, { ownerUserId: 8, appOwnerId: null })).toBe(
|
||
false,
|
||
);
|
||
});
|
||
|
||
it('grants an app actor only the owner’s rows that app created', () => {
|
||
expect(actorOwnsRow(asApp(3), { ownerUserId: 7, appOwnerId: 3 })).toBe(
|
||
true,
|
||
);
|
||
expect(actorOwnsRow(asApp(4), { ownerUserId: 7, appOwnerId: 3 })).toBe(
|
||
false,
|
||
);
|
||
expect(actorOwnsRow(asApp(3), { ownerUserId: 8, appOwnerId: 3 })).toBe(
|
||
false,
|
||
);
|
||
});
|
||
});
|