Files
puter/extensions/installedApps.ts
T
Daniel Salazar bfcacc8469 fix(db): bound numbers must be safe integers; path, cursor and lookup input checks (#4708)
* fix(db): bound numbers must be safe integers; path and cursor input checks

Bound numbers outside ±2^53 (including NaN and Infinity) are refused with a
400 by every database client.

- Paths longer than 4096 characters are refused with a 400.
- The path lineage lookup returns null for paths deeper than its walk
  without querying.
- The read batcher's circuit breaker counts a failed fallback flush only
  when an item could not reach the database.
- App, subdomain and worker list cursors must carry a non-negative integer
  id.
- App stats grouping accepts only its own keys.
- App batch lookups skip values of the wrong type, and app writes refuse
  keys that are not column names.
- User, notification, durable subscription and KV share handle lookups
  return no row for values of the wrong type.
- OIDC links are looked up by the sub as a string.
- User-to-user and group permission reads keep exact matches only.
- revoke-dev-app validates its parameters like grant-dev-app.
- A durable subscription's expiresAt must be before the year 10000.
- Installed apps paging reads page and limit as whole numbers.

* fix(db): review follow-ups for input checks

- The path length check measures the canonical absolute path, after the
  leading slash is added and a trailing one dropped.
- The lineage lookup handles up to 500 segments, SQLite's compound select
  limit.
- Group permission reads keep matching by the column's collation.
- A server error code is never read as a lost connection from its message.
- Cursor ids must be integers or digit strings, and a cursor that is
  neither a string nor an object is refused.
- OIDC links store the sub as a string, matching lookups.
- findEmailOwner returns no row for a value that is not a string.
- Invalid stats groupings and non-string or non-number expiresAt values
  return a 400.
- Installed apps limit clamps before rounding down.
2026-11-19 15:19:28 -07:00

81 lines
2.6 KiB
TypeScript

import type { Request, Response } from 'express';
import { Context } from '@heyputer/backend/src/core';
import { HttpError } from '@heyputer/backend/src/core/http';
import { extension } from '@heyputer/backend/src/extensions';
import {
INSTALLED_APPS_ORDERS,
type InstalledAppsOrder,
} from '@heyputer/backend/src/stores/permission/PermissionStore.js';
import {
getAppIconCdnUrl,
getAppIconUrl,
} from '@heyputer/backend/src/util/appIcon.js';
const stores = extension.import('store');
export const handleInstalledApps = async (
req: Request,
res: Response,
): Promise<void> => {
// Behind `requireUserActor`, so there is always an actor.
const actor = Context.get('actor')!;
const orderBy = String(
req.query.orderBy ?? 'installed_at',
) as InstalledAppsOrder;
if (!INSTALLED_APPS_ORDERS.includes(orderBy)) {
throw new HttpError(
400,
`Invalid orderBy. Allowed: ${INSTALLED_APPS_ORDERS.join(', ')}`,
);
}
const requestedPage = Math.floor(Number(req.query.page));
// Far past any real list; keeps the offset an exact integer.
const page = Number.isSafeInteger(requestedPage)
? Math.min(Math.max(requestedPage, 1), 1_000_000)
: 1;
const limit = Math.floor(
Math.min(Math.max(Number(req.query.limit) || 100, 1), 100),
);
const offset = (page - 1) * limit;
const installedApps = await stores.permission.listAppsGrantedByUser(
actor.user.id!,
{ orderBy, descending: Boolean(req.query.desc), limit, offset },
);
const apiBaseUrl = extension.config.api_base_url as string | undefined;
res.json(
installedApps.map((app) => {
// An app with no owner_user_id (null/empty) isn't owned by a Puter
// user — it's an "external" app. Derive a flag and don't leak the
// raw owner id to the client.
const { owner_user_id, ...rest } = app;
const external = owner_user_id == null || owner_user_id === '';
return {
...rest,
iconUrl: getAppIconUrl(app, { apiBaseUrl }),
// Direct subdomain URL for the client to try before iconUrl.
iconCdnUrl: getAppIconCdnUrl(app, extension.config),
external,
};
}),
);
};
extension.get(
'/installedApps',
{
subdomain: 'api',
requireUserActor: true,
allowFullAccessToken: true,
rateLimit: {
scope: 'installed-apps',
limit: 120,
window: 60_000,
key: 'user',
},
},
handleInstalledApps,
);