@getstrata/core 0.5.42 → 0.5.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/entries/admin/formatValue.js +32 -0
- package/dist/entries/admin/registry.js +32 -0
- package/dist/entries/audit/siemFormatter.js +37 -0
- package/dist/entries/auth/abilityChecker.js +1 -0
- package/dist/entries/auth/membershipMiddleware.js +298 -0
- package/dist/entries/auth/scimAuthMiddleware.js +251 -0
- package/dist/entries/auth/sessionGuard.js +72 -0
- package/dist/entries/database/migrations/types.js +1 -0
- package/dist/entries/database/migrations.js +127 -0
- package/dist/entries/database/schema.js +1054 -0
- package/dist/entries/database/seeders/types.js +1 -0
- package/dist/entries/http/conditionalResponse.js +192 -0
- package/dist/entries/http/corsMiddleware.js +54 -0
- package/dist/entries/http/csrfMiddleware.js +236 -0
- package/dist/entries/http/csrfToken.js +3 -0
- package/dist/entries/http/flashMiddleware.js +143 -0
- package/dist/entries/http/formRequest.js +152 -0
- package/dist/entries/http/loginThrottleMiddleware.js +46 -0
- package/dist/entries/http/memoryThrottleMiddleware.js +30 -0
- package/dist/entries/http/requireAbilityMiddleware.js +110 -0
- package/dist/entries/http/requireAuthMiddleware.js +80 -0
- package/dist/entries/http/requireGlobalAdminMiddleware.js +147 -0
- package/dist/entries/http/requireWebAuthMiddleware.js +107 -0
- package/dist/entries/http/route.js +8 -0
- package/dist/entries/http/routeMiddleware.js +32 -0
- package/dist/entries/http/routeModelBinding.js +141 -0
- package/dist/entries/http/scimThrottleMiddleware.js +23 -0
- package/dist/entries/http/securedRouteModelBinding.js +10 -0
- package/dist/entries/http/securityHeadersMiddleware.js +77 -0
- package/dist/entries/http/throttleMiddleware.js +87 -0
- package/dist/entries/http/webErrorResponse.js +72 -0
- package/dist/entries/http/webFormRequest.js +10 -0
- package/dist/entries/logging/requestLoggingMiddleware.js +89 -0
- package/dist/entries/mail/mailer.js +208 -0
- package/dist/entries/mail/markdownMail.js +63 -0
- package/dist/entries/mail/markdownMailable.js +78 -0
- package/dist/entries/notifications.js +152 -0
- package/dist/entries/openapi/generator.js +178 -0
- package/dist/entries/openapi/validate.js +28 -0
- package/dist/entries/queue/createAppQueue.js +58 -0
- package/dist/entries/queue/failedJobRepository.js +58 -0
- package/dist/entries/queue/publicQueue.js +58 -0
- package/dist/entries/queue/queueMetrics.js +58 -0
- package/dist/entries/runtime/asyncContextStore.js +17 -0
- package/dist/entries/security/safeFetch.js +211 -0
- package/dist/entries/security/timingSafeCompare.js +14 -0
- package/dist/entries/tenant/databaseTenantContext.js +116 -0
- package/dist/entries/tenant/tenantDatabaseScope.js +10 -0
- package/dist/entries/tracing/tracingMiddleware.js +103 -0
- package/dist/entries/view.js +72 -0
- package/package.json +202 -7
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/openapi/validate.ts
|
|
3
|
+
function validateOpenApiSpec(spec) {
|
|
4
|
+
const errors = [];
|
|
5
|
+
if (!spec.openapi.startsWith("3.")) {
|
|
6
|
+
errors.push("OpenAPI version must be 3.x.");
|
|
7
|
+
}
|
|
8
|
+
if (Object.keys(spec.paths).length === 0) {
|
|
9
|
+
errors.push("OpenAPI spec must include at least one path.");
|
|
10
|
+
}
|
|
11
|
+
for (const [path, methods] of Object.entries(spec.paths)) {
|
|
12
|
+
if (!path.startsWith("/")) {
|
|
13
|
+
errors.push(`Path must start with '/': ${path}`);
|
|
14
|
+
}
|
|
15
|
+
for (const [method, operation] of Object.entries(methods)) {
|
|
16
|
+
if (!("responses" in operation)) {
|
|
17
|
+
errors.push(`Operation ${method.toUpperCase()} ${path} is missing responses.`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (!spec.components.securitySchemes.bearerAuth) {
|
|
22
|
+
errors.push("Missing bearerAuth security scheme.");
|
|
23
|
+
}
|
|
24
|
+
return errors;
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
validateOpenApiSpec
|
|
28
|
+
};
|
|
@@ -1925,6 +1925,45 @@ class Blueprint {
|
|
|
1925
1925
|
});
|
|
1926
1926
|
}
|
|
1927
1927
|
}
|
|
1928
|
+
// ../../src/core/database/schema/driver.ts
|
|
1929
|
+
function normalizeConnectionName(connection) {
|
|
1930
|
+
const normalized = connection.trim().toLowerCase();
|
|
1931
|
+
if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
|
|
1932
|
+
return "pgsql";
|
|
1933
|
+
}
|
|
1934
|
+
if (normalized === "mysql" || normalized === "mariadb") {
|
|
1935
|
+
return "mysql";
|
|
1936
|
+
}
|
|
1937
|
+
if (normalized === "sqlite") {
|
|
1938
|
+
return "sqlite";
|
|
1939
|
+
}
|
|
1940
|
+
throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
|
|
1941
|
+
}
|
|
1942
|
+
function resolveDriverFromUrl(url) {
|
|
1943
|
+
const normalized = url.trim().toLowerCase();
|
|
1944
|
+
if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
|
|
1945
|
+
return "pgsql";
|
|
1946
|
+
}
|
|
1947
|
+
if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
|
|
1948
|
+
return "mysql";
|
|
1949
|
+
}
|
|
1950
|
+
if (normalized.startsWith("sqlite:")) {
|
|
1951
|
+
return "sqlite";
|
|
1952
|
+
}
|
|
1953
|
+
return null;
|
|
1954
|
+
}
|
|
1955
|
+
function resolveDatabaseDriver(options = {}) {
|
|
1956
|
+
const connection = options.connection ?? process.env.DB_CONNECTION;
|
|
1957
|
+
if (connection) {
|
|
1958
|
+
return normalizeConnectionName(connection);
|
|
1959
|
+
}
|
|
1960
|
+
const url = options.url ?? process.env.DATABASE_URL ?? "";
|
|
1961
|
+
const fromUrl = resolveDriverFromUrl(url);
|
|
1962
|
+
if (fromUrl) {
|
|
1963
|
+
return fromUrl;
|
|
1964
|
+
}
|
|
1965
|
+
return "pgsql";
|
|
1966
|
+
}
|
|
1928
1967
|
// ../../src/core/database/schema/errors.ts
|
|
1929
1968
|
class UnsupportedSchemaFeatureError extends Error {
|
|
1930
1969
|
constructor(feature, driver) {
|
|
@@ -2269,6 +2308,25 @@ class SchemaBuilder {
|
|
|
2269
2308
|
}
|
|
2270
2309
|
}
|
|
2271
2310
|
}
|
|
2311
|
+
|
|
2312
|
+
class Schema {
|
|
2313
|
+
static builder(driver) {
|
|
2314
|
+
return new SchemaBuilder(driver ?? resolveDatabaseDriver());
|
|
2315
|
+
}
|
|
2316
|
+
static async run(db, driver, callback) {
|
|
2317
|
+
const schema = Schema.builder(driver);
|
|
2318
|
+
await callback(schema);
|
|
2319
|
+
await schema.execute(db);
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
function createSchemaBuilder(db, driver) {
|
|
2323
|
+
const builder = Schema.builder(driver);
|
|
2324
|
+
return Object.assign(builder, {
|
|
2325
|
+
async commit() {
|
|
2326
|
+
await builder.execute(db);
|
|
2327
|
+
}
|
|
2328
|
+
});
|
|
2329
|
+
}
|
|
2272
2330
|
// ../../src/core/database/table.ts
|
|
2273
2331
|
function defineTable(definition) {
|
|
2274
2332
|
return definition;
|
|
@@ -1925,6 +1925,45 @@ class Blueprint {
|
|
|
1925
1925
|
});
|
|
1926
1926
|
}
|
|
1927
1927
|
}
|
|
1928
|
+
// ../../src/core/database/schema/driver.ts
|
|
1929
|
+
function normalizeConnectionName(connection) {
|
|
1930
|
+
const normalized = connection.trim().toLowerCase();
|
|
1931
|
+
if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
|
|
1932
|
+
return "pgsql";
|
|
1933
|
+
}
|
|
1934
|
+
if (normalized === "mysql" || normalized === "mariadb") {
|
|
1935
|
+
return "mysql";
|
|
1936
|
+
}
|
|
1937
|
+
if (normalized === "sqlite") {
|
|
1938
|
+
return "sqlite";
|
|
1939
|
+
}
|
|
1940
|
+
throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
|
|
1941
|
+
}
|
|
1942
|
+
function resolveDriverFromUrl(url) {
|
|
1943
|
+
const normalized = url.trim().toLowerCase();
|
|
1944
|
+
if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
|
|
1945
|
+
return "pgsql";
|
|
1946
|
+
}
|
|
1947
|
+
if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
|
|
1948
|
+
return "mysql";
|
|
1949
|
+
}
|
|
1950
|
+
if (normalized.startsWith("sqlite:")) {
|
|
1951
|
+
return "sqlite";
|
|
1952
|
+
}
|
|
1953
|
+
return null;
|
|
1954
|
+
}
|
|
1955
|
+
function resolveDatabaseDriver(options = {}) {
|
|
1956
|
+
const connection = options.connection ?? process.env.DB_CONNECTION;
|
|
1957
|
+
if (connection) {
|
|
1958
|
+
return normalizeConnectionName(connection);
|
|
1959
|
+
}
|
|
1960
|
+
const url = options.url ?? process.env.DATABASE_URL ?? "";
|
|
1961
|
+
const fromUrl = resolveDriverFromUrl(url);
|
|
1962
|
+
if (fromUrl) {
|
|
1963
|
+
return fromUrl;
|
|
1964
|
+
}
|
|
1965
|
+
return "pgsql";
|
|
1966
|
+
}
|
|
1928
1967
|
// ../../src/core/database/schema/errors.ts
|
|
1929
1968
|
class UnsupportedSchemaFeatureError extends Error {
|
|
1930
1969
|
constructor(feature, driver) {
|
|
@@ -2269,6 +2308,25 @@ class SchemaBuilder {
|
|
|
2269
2308
|
}
|
|
2270
2309
|
}
|
|
2271
2310
|
}
|
|
2311
|
+
|
|
2312
|
+
class Schema {
|
|
2313
|
+
static builder(driver) {
|
|
2314
|
+
return new SchemaBuilder(driver ?? resolveDatabaseDriver());
|
|
2315
|
+
}
|
|
2316
|
+
static async run(db, driver, callback) {
|
|
2317
|
+
const schema = Schema.builder(driver);
|
|
2318
|
+
await callback(schema);
|
|
2319
|
+
await schema.execute(db);
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
function createSchemaBuilder(db, driver) {
|
|
2323
|
+
const builder = Schema.builder(driver);
|
|
2324
|
+
return Object.assign(builder, {
|
|
2325
|
+
async commit() {
|
|
2326
|
+
await builder.execute(db);
|
|
2327
|
+
}
|
|
2328
|
+
});
|
|
2329
|
+
}
|
|
2272
2330
|
// ../../src/core/database/table.ts
|
|
2273
2331
|
function defineTable(definition) {
|
|
2274
2332
|
return definition;
|
|
@@ -1925,6 +1925,45 @@ class Blueprint {
|
|
|
1925
1925
|
});
|
|
1926
1926
|
}
|
|
1927
1927
|
}
|
|
1928
|
+
// ../../src/core/database/schema/driver.ts
|
|
1929
|
+
function normalizeConnectionName(connection) {
|
|
1930
|
+
const normalized = connection.trim().toLowerCase();
|
|
1931
|
+
if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
|
|
1932
|
+
return "pgsql";
|
|
1933
|
+
}
|
|
1934
|
+
if (normalized === "mysql" || normalized === "mariadb") {
|
|
1935
|
+
return "mysql";
|
|
1936
|
+
}
|
|
1937
|
+
if (normalized === "sqlite") {
|
|
1938
|
+
return "sqlite";
|
|
1939
|
+
}
|
|
1940
|
+
throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
|
|
1941
|
+
}
|
|
1942
|
+
function resolveDriverFromUrl(url) {
|
|
1943
|
+
const normalized = url.trim().toLowerCase();
|
|
1944
|
+
if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
|
|
1945
|
+
return "pgsql";
|
|
1946
|
+
}
|
|
1947
|
+
if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
|
|
1948
|
+
return "mysql";
|
|
1949
|
+
}
|
|
1950
|
+
if (normalized.startsWith("sqlite:")) {
|
|
1951
|
+
return "sqlite";
|
|
1952
|
+
}
|
|
1953
|
+
return null;
|
|
1954
|
+
}
|
|
1955
|
+
function resolveDatabaseDriver(options = {}) {
|
|
1956
|
+
const connection = options.connection ?? process.env.DB_CONNECTION;
|
|
1957
|
+
if (connection) {
|
|
1958
|
+
return normalizeConnectionName(connection);
|
|
1959
|
+
}
|
|
1960
|
+
const url = options.url ?? process.env.DATABASE_URL ?? "";
|
|
1961
|
+
const fromUrl = resolveDriverFromUrl(url);
|
|
1962
|
+
if (fromUrl) {
|
|
1963
|
+
return fromUrl;
|
|
1964
|
+
}
|
|
1965
|
+
return "pgsql";
|
|
1966
|
+
}
|
|
1928
1967
|
// ../../src/core/database/schema/errors.ts
|
|
1929
1968
|
class UnsupportedSchemaFeatureError extends Error {
|
|
1930
1969
|
constructor(feature, driver) {
|
|
@@ -2269,6 +2308,25 @@ class SchemaBuilder {
|
|
|
2269
2308
|
}
|
|
2270
2309
|
}
|
|
2271
2310
|
}
|
|
2311
|
+
|
|
2312
|
+
class Schema {
|
|
2313
|
+
static builder(driver) {
|
|
2314
|
+
return new SchemaBuilder(driver ?? resolveDatabaseDriver());
|
|
2315
|
+
}
|
|
2316
|
+
static async run(db, driver, callback) {
|
|
2317
|
+
const schema = Schema.builder(driver);
|
|
2318
|
+
await callback(schema);
|
|
2319
|
+
await schema.execute(db);
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
function createSchemaBuilder(db, driver) {
|
|
2323
|
+
const builder = Schema.builder(driver);
|
|
2324
|
+
return Object.assign(builder, {
|
|
2325
|
+
async commit() {
|
|
2326
|
+
await builder.execute(db);
|
|
2327
|
+
}
|
|
2328
|
+
});
|
|
2329
|
+
}
|
|
2272
2330
|
// ../../src/core/database/table.ts
|
|
2273
2331
|
function defineTable(definition) {
|
|
2274
2332
|
return definition;
|
|
@@ -1946,6 +1946,45 @@ class Blueprint {
|
|
|
1946
1946
|
});
|
|
1947
1947
|
}
|
|
1948
1948
|
}
|
|
1949
|
+
// ../../src/core/database/schema/driver.ts
|
|
1950
|
+
function normalizeConnectionName(connection) {
|
|
1951
|
+
const normalized = connection.trim().toLowerCase();
|
|
1952
|
+
if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
|
|
1953
|
+
return "pgsql";
|
|
1954
|
+
}
|
|
1955
|
+
if (normalized === "mysql" || normalized === "mariadb") {
|
|
1956
|
+
return "mysql";
|
|
1957
|
+
}
|
|
1958
|
+
if (normalized === "sqlite") {
|
|
1959
|
+
return "sqlite";
|
|
1960
|
+
}
|
|
1961
|
+
throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
|
|
1962
|
+
}
|
|
1963
|
+
function resolveDriverFromUrl(url) {
|
|
1964
|
+
const normalized = url.trim().toLowerCase();
|
|
1965
|
+
if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
|
|
1966
|
+
return "pgsql";
|
|
1967
|
+
}
|
|
1968
|
+
if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
|
|
1969
|
+
return "mysql";
|
|
1970
|
+
}
|
|
1971
|
+
if (normalized.startsWith("sqlite:")) {
|
|
1972
|
+
return "sqlite";
|
|
1973
|
+
}
|
|
1974
|
+
return null;
|
|
1975
|
+
}
|
|
1976
|
+
function resolveDatabaseDriver(options = {}) {
|
|
1977
|
+
const connection = options.connection ?? process.env.DB_CONNECTION;
|
|
1978
|
+
if (connection) {
|
|
1979
|
+
return normalizeConnectionName(connection);
|
|
1980
|
+
}
|
|
1981
|
+
const url = options.url ?? process.env.DATABASE_URL ?? "";
|
|
1982
|
+
const fromUrl = resolveDriverFromUrl(url);
|
|
1983
|
+
if (fromUrl) {
|
|
1984
|
+
return fromUrl;
|
|
1985
|
+
}
|
|
1986
|
+
return "pgsql";
|
|
1987
|
+
}
|
|
1949
1988
|
// ../../src/core/database/schema/errors.ts
|
|
1950
1989
|
class UnsupportedSchemaFeatureError extends Error {
|
|
1951
1990
|
constructor(feature, driver) {
|
|
@@ -2290,6 +2329,25 @@ class SchemaBuilder {
|
|
|
2290
2329
|
}
|
|
2291
2330
|
}
|
|
2292
2331
|
}
|
|
2332
|
+
|
|
2333
|
+
class Schema {
|
|
2334
|
+
static builder(driver) {
|
|
2335
|
+
return new SchemaBuilder(driver ?? resolveDatabaseDriver());
|
|
2336
|
+
}
|
|
2337
|
+
static async run(db, driver, callback) {
|
|
2338
|
+
const schema = Schema.builder(driver);
|
|
2339
|
+
await callback(schema);
|
|
2340
|
+
await schema.execute(db);
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
function createSchemaBuilder(db, driver) {
|
|
2344
|
+
const builder = Schema.builder(driver);
|
|
2345
|
+
return Object.assign(builder, {
|
|
2346
|
+
async commit() {
|
|
2347
|
+
await builder.execute(db);
|
|
2348
|
+
}
|
|
2349
|
+
});
|
|
2350
|
+
}
|
|
2293
2351
|
// ../../src/core/database/table.ts
|
|
2294
2352
|
function defineTable(definition) {
|
|
2295
2353
|
return definition;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
3
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
4
|
+
function createAsyncContextStore(key) {
|
|
5
|
+
const symbol = Symbol.for(key);
|
|
6
|
+
const globalRecord = globalThis;
|
|
7
|
+
const existing = globalRecord[symbol];
|
|
8
|
+
if (existing) {
|
|
9
|
+
return existing;
|
|
10
|
+
}
|
|
11
|
+
const store = new AsyncLocalStorage;
|
|
12
|
+
globalRecord[symbol] = store;
|
|
13
|
+
return store;
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
createAsyncContextStore
|
|
17
|
+
};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/config/app.ts
|
|
3
|
+
var appConfig = {
|
|
4
|
+
name: "WorkHub",
|
|
5
|
+
env: process.env.APP_ENV ?? "local",
|
|
6
|
+
debug: (process.env.APP_DEBUG ?? "true") !== "false",
|
|
7
|
+
url: process.env.APP_URL ?? "http://localhost:3000",
|
|
8
|
+
apiPrefix: process.env.API_PREFIX ?? "/api/v1"
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// ../../src/core/security/safeUrl.ts
|
|
12
|
+
import { lookup as dnsLookupImpl } from "dns/promises";
|
|
13
|
+
|
|
14
|
+
// ../../src/core/errors/http.ts
|
|
15
|
+
class HttpError extends Error {
|
|
16
|
+
status;
|
|
17
|
+
details;
|
|
18
|
+
constructor(status, message, details) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = new.target.name;
|
|
21
|
+
this.status = status;
|
|
22
|
+
this.details = details;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class BadRequestError extends HttpError {
|
|
27
|
+
constructor(message = "Bad Request", details) {
|
|
28
|
+
super(400, message, details);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class NotFoundError extends HttpError {
|
|
33
|
+
constructor(message = "Not Found", details) {
|
|
34
|
+
super(404, message, details);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
class ConflictError extends HttpError {
|
|
39
|
+
constructor(message = "Conflict", details) {
|
|
40
|
+
super(409, message, details);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class UnprocessableEntityError extends HttpError {
|
|
45
|
+
constructor(message = "Unprocessable Entity", details) {
|
|
46
|
+
super(422, message, details);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class ValidationError extends HttpError {
|
|
51
|
+
constructor(message = "Validation failed", details) {
|
|
52
|
+
super(422, message, details);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
class ForbiddenError extends HttpError {
|
|
57
|
+
constructor(message = "Forbidden", details) {
|
|
58
|
+
super(403, message, details);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
class UnauthorizedError extends HttpError {
|
|
63
|
+
constructor(message = "Unauthorized", details) {
|
|
64
|
+
super(401, message, details);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
class PayloadTooLargeError extends HttpError {
|
|
69
|
+
constructor(message = "Payload Too Large", details) {
|
|
70
|
+
super(413, message, details);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
class PreconditionFailedError extends HttpError {
|
|
75
|
+
constructor(message = "Precondition Failed", details) {
|
|
76
|
+
super(412, message, details);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ../../src/core/security/safeUrl.ts
|
|
81
|
+
var dnsLookup = dnsLookupImpl;
|
|
82
|
+
var BLOCKED_HOSTNAMES = new Set([
|
|
83
|
+
"localhost",
|
|
84
|
+
"127.0.0.1",
|
|
85
|
+
"0.0.0.0",
|
|
86
|
+
"::1",
|
|
87
|
+
"metadata.google.internal"
|
|
88
|
+
]);
|
|
89
|
+
function isPrivateIpv4(hostname) {
|
|
90
|
+
const match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(hostname);
|
|
91
|
+
if (!match) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
const octets = match.slice(1, 5).map((part) => Number.parseInt(part, 10));
|
|
95
|
+
if (octets.some((octet) => octet < 0 || octet > 255)) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
const [a = 0, b = 0] = octets;
|
|
99
|
+
if (a === 10) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
if (a === 127) {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
if (a === 0) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
if (a === 169 && b === 254) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
if (a === 172 && b >= 16 && b <= 31) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
if (a === 192 && b === 168) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
function isBlockedHostname(hostname) {
|
|
120
|
+
const normalized = hostname.trim().toLowerCase();
|
|
121
|
+
if (normalized.length === 0) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
if (BLOCKED_HOSTNAMES.has(normalized)) {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
if (normalized.endsWith(".local") || normalized.endsWith(".internal")) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
if (normalized.includes(":")) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
return isPrivateIpv4(normalized);
|
|
134
|
+
}
|
|
135
|
+
function assertSafeOutboundUrl(rawUrl, options = {}) {
|
|
136
|
+
let parsed;
|
|
137
|
+
try {
|
|
138
|
+
parsed = new URL(rawUrl);
|
|
139
|
+
} catch {
|
|
140
|
+
throw new BadRequestError("Webhook URL is invalid.");
|
|
141
|
+
}
|
|
142
|
+
if (parsed.protocol !== "https:" && !(options.allowHttp && parsed.protocol === "http:")) {
|
|
143
|
+
throw new BadRequestError("Webhook URL must use HTTPS.");
|
|
144
|
+
}
|
|
145
|
+
if (parsed.username || parsed.password) {
|
|
146
|
+
throw new BadRequestError("Webhook URL must not include credentials.");
|
|
147
|
+
}
|
|
148
|
+
if (isBlockedHostname(parsed.hostname)) {
|
|
149
|
+
throw new BadRequestError("Webhook URL targets a blocked host.");
|
|
150
|
+
}
|
|
151
|
+
return parsed;
|
|
152
|
+
}
|
|
153
|
+
function isBlockedIpAddress(address) {
|
|
154
|
+
return isBlockedHostname(address.trim().toLowerCase());
|
|
155
|
+
}
|
|
156
|
+
async function assertSafeOutboundUrlResolved(rawUrl, options = {}) {
|
|
157
|
+
const parsed = assertSafeOutboundUrl(rawUrl, options);
|
|
158
|
+
if (options.resolveDns === false) {
|
|
159
|
+
return parsed;
|
|
160
|
+
}
|
|
161
|
+
const hostname = parsed.hostname.trim().toLowerCase();
|
|
162
|
+
const results = await dnsLookup(hostname, { all: true, verbatim: true });
|
|
163
|
+
if (results.some((result) => isBlockedIpAddress(result.address))) {
|
|
164
|
+
throw new BadRequestError("Webhook URL targets a blocked host.");
|
|
165
|
+
}
|
|
166
|
+
return parsed;
|
|
167
|
+
}
|
|
168
|
+
function setDnsLookupForTests(lookupFn) {
|
|
169
|
+
dnsLookup = lookupFn;
|
|
170
|
+
}
|
|
171
|
+
function resetDnsLookupForTests() {
|
|
172
|
+
dnsLookup = dnsLookupImpl;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ../../src/core/security/safeFetch.ts
|
|
176
|
+
var DEFAULT_FETCH_TIMEOUT_MS = 1e4;
|
|
177
|
+
async function safeFetch(input, init = {}, options = {}) {
|
|
178
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_FETCH_TIMEOUT_MS;
|
|
179
|
+
const maxRedirects = options.maxRedirects ?? 0;
|
|
180
|
+
const resolveDns = options.resolveDns ?? appConfig.env === "production";
|
|
181
|
+
const urlOptions = { allowHttp: options.allowHttp, resolveDns };
|
|
182
|
+
const controller = new AbortController;
|
|
183
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
184
|
+
try {
|
|
185
|
+
let currentUrl = (await assertSafeOutboundUrlResolved(input, urlOptions)).toString();
|
|
186
|
+
let redirectCount = 0;
|
|
187
|
+
while (true) {
|
|
188
|
+
const response = await fetch(currentUrl, {
|
|
189
|
+
...init,
|
|
190
|
+
signal: controller.signal,
|
|
191
|
+
redirect: "manual"
|
|
192
|
+
});
|
|
193
|
+
if (response.status >= 300 && response.status < 400) {
|
|
194
|
+
const location = response.headers.get("location");
|
|
195
|
+
if (!location || redirectCount >= maxRedirects) {
|
|
196
|
+
return response;
|
|
197
|
+
}
|
|
198
|
+
currentUrl = (await assertSafeOutboundUrlResolved(new URL(location, currentUrl).toString(), urlOptions)).toString();
|
|
199
|
+
redirectCount += 1;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
return response;
|
|
203
|
+
}
|
|
204
|
+
} finally {
|
|
205
|
+
clearTimeout(timeout);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
export {
|
|
209
|
+
safeFetch,
|
|
210
|
+
DEFAULT_FETCH_TIMEOUT_MS
|
|
211
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/security/timingSafeCompare.ts
|
|
3
|
+
import { timingSafeEqual } from "crypto";
|
|
4
|
+
function timingSafeCompareString(left, right) {
|
|
5
|
+
const leftBuffer = Buffer.from(left);
|
|
6
|
+
const rightBuffer = Buffer.from(right);
|
|
7
|
+
if (leftBuffer.length !== rightBuffer.length) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
return timingSafeEqual(leftBuffer, rightBuffer);
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
timingSafeCompareString
|
|
14
|
+
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/database/boundConnection.ts
|
|
3
|
+
var boundConnectionHolder = {
|
|
4
|
+
connection: null
|
|
5
|
+
};
|
|
6
|
+
function bindDatabaseConnection(connection) {
|
|
7
|
+
boundConnectionHolder.connection = connection;
|
|
8
|
+
}
|
|
9
|
+
function getBoundDatabaseConnection() {
|
|
10
|
+
return boundConnectionHolder.connection;
|
|
11
|
+
}
|
|
12
|
+
function resetBoundDatabaseConnection() {
|
|
13
|
+
boundConnectionHolder.connection = null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
17
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
18
|
+
function createAsyncContextStore(key) {
|
|
19
|
+
const symbol = Symbol.for(key);
|
|
20
|
+
const globalRecord = globalThis;
|
|
21
|
+
const existing = globalRecord[symbol];
|
|
22
|
+
if (existing) {
|
|
23
|
+
return existing;
|
|
24
|
+
}
|
|
25
|
+
const store = new AsyncLocalStorage;
|
|
26
|
+
globalRecord[symbol] = store;
|
|
27
|
+
return store;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ../../src/core/database/connectionContext.ts
|
|
31
|
+
var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
|
|
32
|
+
function runWithDatabaseConnection(connection, callback) {
|
|
33
|
+
return activeConnection.run(connection, callback);
|
|
34
|
+
}
|
|
35
|
+
function getActiveDatabaseConnection(fallback) {
|
|
36
|
+
return activeConnection.getStore() ?? fallback;
|
|
37
|
+
}
|
|
38
|
+
function hasActiveDatabaseConnection() {
|
|
39
|
+
return activeConnection.getStore() !== undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ../../src/core/database/queryProxy.ts
|
|
43
|
+
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
44
|
+
function createDatabaseQueryProxy(pool) {
|
|
45
|
+
function resolveDatabase() {
|
|
46
|
+
return getActiveDatabaseConnection(pool);
|
|
47
|
+
}
|
|
48
|
+
function resolveDatabaseForProperty(property) {
|
|
49
|
+
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
50
|
+
return pool;
|
|
51
|
+
}
|
|
52
|
+
return resolveDatabase();
|
|
53
|
+
}
|
|
54
|
+
return new Proxy(function database() {}, {
|
|
55
|
+
apply(_target, _thisArg, args) {
|
|
56
|
+
return resolveDatabase()(...args);
|
|
57
|
+
},
|
|
58
|
+
get(_target, property) {
|
|
59
|
+
const connection = resolveDatabaseForProperty(property);
|
|
60
|
+
const value = connection[property];
|
|
61
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ../../src/core/database/defaultConnection.ts
|
|
67
|
+
var defaultPool = {
|
|
68
|
+
connection: null
|
|
69
|
+
};
|
|
70
|
+
var defaultQuery = {
|
|
71
|
+
connection: null
|
|
72
|
+
};
|
|
73
|
+
function registerDefaultDatabasePool(connection) {
|
|
74
|
+
defaultPool.connection = connection;
|
|
75
|
+
defaultQuery.connection = createDatabaseQueryProxy(connection);
|
|
76
|
+
}
|
|
77
|
+
function getDefaultDatabasePool() {
|
|
78
|
+
if (!defaultPool.connection) {
|
|
79
|
+
throw new Error("Default database pool is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
80
|
+
}
|
|
81
|
+
return defaultPool.connection;
|
|
82
|
+
}
|
|
83
|
+
function getDefaultDatabaseQuery() {
|
|
84
|
+
if (!defaultQuery.connection) {
|
|
85
|
+
throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
|
|
86
|
+
}
|
|
87
|
+
return defaultQuery.connection;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ../../src/core/database/repositoryConnection.ts
|
|
91
|
+
function resolveRepositoryConnection() {
|
|
92
|
+
return getBoundDatabaseConnection() ?? getDefaultDatabaseQuery();
|
|
93
|
+
}
|
|
94
|
+
var repositoryConnection = new Proxy(function repositoryConnection2() {}, {
|
|
95
|
+
apply(_target, _thisArg, args) {
|
|
96
|
+
return resolveRepositoryConnection()(...args);
|
|
97
|
+
},
|
|
98
|
+
get(_target, property) {
|
|
99
|
+
const connection = resolveRepositoryConnection();
|
|
100
|
+
const value = connection[property];
|
|
101
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// ../../src/core/tenant/databaseTenantContext.ts
|
|
106
|
+
async function runWithMigrationBypass(callback) {
|
|
107
|
+
await repositoryConnection`SELECT set_config('app.bypass_rls', 'true', false)`;
|
|
108
|
+
try {
|
|
109
|
+
return await callback();
|
|
110
|
+
} finally {
|
|
111
|
+
await repositoryConnection`SELECT set_config('app.bypass_rls', 'false', false)`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export {
|
|
115
|
+
runWithMigrationBypass
|
|
116
|
+
};
|
|
@@ -84,6 +84,16 @@ function currentTenant() {
|
|
|
84
84
|
function currentTenantId() {
|
|
85
85
|
return currentTenant()?.id ?? 1;
|
|
86
86
|
}
|
|
87
|
+
function rateLimitMultiplierForPlan(plan) {
|
|
88
|
+
switch (plan) {
|
|
89
|
+
case "enterprise":
|
|
90
|
+
return 4;
|
|
91
|
+
case "pro":
|
|
92
|
+
return 2;
|
|
93
|
+
default:
|
|
94
|
+
return 1;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
87
97
|
|
|
88
98
|
// ../../src/core/tenant/tenantDatabaseScope.ts
|
|
89
99
|
async function applyTenantContextToTransaction(transaction, tenantId) {
|