mirror of
https://github.com/fosrl/pangolin.git
synced 2026-10-29 06:59:06 +01:00
Merge branch 'dev' into feat/redirect-tables
This commit is contained in:
+77
-48
@@ -4,42 +4,18 @@
|
||||
"iPad2,2": "iPad 2",
|
||||
"iPad2,3": "iPad 2",
|
||||
"iPad2,4": "iPad 2",
|
||||
"iPad3,1": "iPad 3rd Gen",
|
||||
"iPad3,3": "iPad 3rd Gen",
|
||||
"iPad3,2": "iPad 3rd Gen",
|
||||
"iPad3,4": "iPad 4th Gen",
|
||||
"iPad3,5": "iPad 4th Gen",
|
||||
"iPad3,6": "iPad 4th Gen",
|
||||
"iPad6,11": "iPad 9.7 5th Gen",
|
||||
"iPad6,12": "iPad 9.7 5th Gen",
|
||||
"iPad7,5": "iPad 9.7 6th Gen",
|
||||
"iPad7,6": "iPad 9.7 6th Gen",
|
||||
"iPad7,11": "iPad 10.2 7th Gen",
|
||||
"iPad7,12": "iPad 10.2 7th Gen",
|
||||
"iPad11,6": "iPad 10.2 8th Gen",
|
||||
"iPad11,7": "iPad 10.2 8th Gen",
|
||||
"iPad12,1": "iPad 10.2 9th Gen",
|
||||
"iPad12,2": "iPad 10.2 9th Gen",
|
||||
"iPad13,18": "iPad 10.9 10th Gen",
|
||||
"iPad13,19": "iPad 10.9 10th Gen",
|
||||
"iPad4,1": "iPad Air",
|
||||
"iPad4,2": "iPad Air",
|
||||
"iPad4,3": "iPad Air",
|
||||
"iPad5,3": "iPad Air 2",
|
||||
"iPad5,4": "iPad Air 2",
|
||||
"iPad11,3": "iPad Air 3rd Gen",
|
||||
"iPad11,4": "iPad Air 3rd Gen",
|
||||
"iPad13,1": "iPad Air 4th Gen",
|
||||
"iPad13,2": "iPad Air 4th Gen",
|
||||
"iPad13,16": "iPad Air 5th Gen",
|
||||
"iPad13,17": "iPad Air 5th Gen",
|
||||
"iPad14,8": "iPad Air M2 11",
|
||||
"iPad14,9": "iPad Air M2 11",
|
||||
"iPad14,10": "iPad Air M2 13",
|
||||
"iPad14,11": "iPad Air M2 13",
|
||||
"iPad2,5": "iPad mini",
|
||||
"iPad2,6": "iPad mini",
|
||||
"iPad2,7": "iPad mini",
|
||||
"iPad3,1": "iPad 3rd Gen",
|
||||
"iPad3,2": "iPad 3rd Gen",
|
||||
"iPad3,3": "iPad 3rd Gen",
|
||||
"iPad3,4": "iPad 4th Gen",
|
||||
"iPad3,5": "iPad 4th Gen",
|
||||
"iPad3,6": "iPad 4th Gen",
|
||||
"iPad4,1": "iPad Air",
|
||||
"iPad4,2": "iPad Air",
|
||||
"iPad4,3": "iPad Air",
|
||||
"iPad4,4": "iPad mini 2",
|
||||
"iPad4,5": "iPad mini 2",
|
||||
"iPad4,6": "iPad mini 2",
|
||||
@@ -48,18 +24,22 @@
|
||||
"iPad4,9": "iPad mini 3",
|
||||
"iPad5,1": "iPad mini 4",
|
||||
"iPad5,2": "iPad mini 4",
|
||||
"iPad11,1": "iPad mini 5th Gen",
|
||||
"iPad11,2": "iPad mini 5th Gen",
|
||||
"iPad14,1": "iPad mini 6th Gen",
|
||||
"iPad14,2": "iPad mini 6th Gen",
|
||||
"iPad6,7": "iPad Pro 12.9",
|
||||
"iPad6,8": "iPad Pro 12.9",
|
||||
"iPad5,3": "iPad Air 2",
|
||||
"iPad5,4": "iPad Air 2",
|
||||
"iPad6,3": "iPad Pro 9.7",
|
||||
"iPad6,4": "iPad Pro 9.7",
|
||||
"iPad7,3": "iPad Pro 10.5",
|
||||
"iPad7,4": "iPad Pro 10.5",
|
||||
"iPad6,7": "iPad Pro 12.9",
|
||||
"iPad6,8": "iPad Pro 12.9",
|
||||
"iPad6,11": "iPad 9.7 5th Gen",
|
||||
"iPad6,12": "iPad 9.7 5th Gen",
|
||||
"iPad7,1": "iPad Pro 12.9",
|
||||
"iPad7,2": "iPad Pro 12.9",
|
||||
"iPad7,3": "iPad Pro 10.5",
|
||||
"iPad7,4": "iPad Pro 10.5",
|
||||
"iPad7,5": "iPad 9.7 6th Gen",
|
||||
"iPad7,6": "iPad 9.7 6th Gen",
|
||||
"iPad7,11": "iPad 10.2 7th Gen",
|
||||
"iPad7,12": "iPad 10.2 7th Gen",
|
||||
"iPad8,1": "iPad Pro 11",
|
||||
"iPad8,2": "iPad Pro 11",
|
||||
"iPad8,3": "iPad Pro 11",
|
||||
@@ -72,6 +52,16 @@
|
||||
"iPad8,10": "iPad Pro 11",
|
||||
"iPad8,11": "iPad Pro 12.9",
|
||||
"iPad8,12": "iPad Pro 12.9",
|
||||
"iPad11,1": "iPad mini 5th Gen",
|
||||
"iPad11,2": "iPad mini 5th Gen",
|
||||
"iPad11,3": "iPad Air 3rd Gen",
|
||||
"iPad11,4": "iPad Air 3rd Gen",
|
||||
"iPad11,6": "iPad 10.2 8th Gen",
|
||||
"iPad11,7": "iPad 10.2 8th Gen",
|
||||
"iPad12,1": "iPad 10.2 9th Gen",
|
||||
"iPad12,2": "iPad 10.2 9th Gen",
|
||||
"iPad13,1": "iPad Air 4th Gen",
|
||||
"iPad13,2": "iPad Air 4th Gen",
|
||||
"iPad13,4": "iPad Pro 11",
|
||||
"iPad13,5": "iPad Pro 11",
|
||||
"iPad13,6": "iPad Pro 11",
|
||||
@@ -80,14 +70,40 @@
|
||||
"iPad13,9": "iPad Pro 12.9",
|
||||
"iPad13,10": "iPad Pro 12.9",
|
||||
"iPad13,11": "iPad Pro 12.9",
|
||||
"iPad13,16": "iPad Air M1 5th Gen",
|
||||
"iPad13,17": "iPad Air M1 5th Gen",
|
||||
"iPad13,18": "iPad 10.9 10th Gen",
|
||||
"iPad13,19": "iPad 10.9 10th Gen",
|
||||
"iPad14,1": "iPad mini 6th Gen",
|
||||
"iPad14,2": "iPad mini 6th Gen",
|
||||
"iPad14,3": "iPad Pro 11",
|
||||
"iPad14,4": "iPad Pro 11",
|
||||
"iPad14,5": "iPad Pro 12.9",
|
||||
"iPad14,6": "iPad Pro 12.9",
|
||||
"iPad14,8": "iPad Air M2 11",
|
||||
"iPad14,9": "iPad Air M2 11",
|
||||
"iPad14,10": "iPad Air M2 13",
|
||||
"iPad14,11": "iPad Air M2 13",
|
||||
"iPad15,3": "iPad Air M3 11",
|
||||
"iPad15,4": "iPad Air M3 11",
|
||||
"iPad15,5": "iPad Air M3 13",
|
||||
"iPad15,6": "iPad Air M3 13",
|
||||
"iPad15,7": "iPad A16 - 11th Gen",
|
||||
"iPad15,8": "iPad A16 - 11th Gen",
|
||||
"iPad16,1": "iPad mini A17 Pro - 7th Gen",
|
||||
"iPad16,2": "iPad mini A17 Pro - 7th Gen",
|
||||
"iPad16,3": "iPad Pro M4 11",
|
||||
"iPad16,4": "iPad Pro M4 11",
|
||||
"iPad16,5": "iPad Pro M4 13",
|
||||
"iPad16,6": "iPad Pro M4 13",
|
||||
"iPad16,8": "iPad Air M4 11",
|
||||
"iPad16,9": "iPad Air M4 11",
|
||||
"iPad16,10": "iPad Air M4 13",
|
||||
"iPad16,11": "iPad Air M4 13",
|
||||
"iPad17,1": "iPad Pro M5 11",
|
||||
"iPad17,2": "iPad Pro M5 11",
|
||||
"iPad17,3": "iPad Pro M5 13",
|
||||
"iPad17,4": "iPad Pro M5 13",
|
||||
"iPhone1,1": "iPhone",
|
||||
"iPhone1,2": "iPhone 3G",
|
||||
"iPhone2,1": "iPhone 3GS",
|
||||
@@ -101,20 +117,20 @@
|
||||
"iPhone5,4": "iPhone 5c",
|
||||
"iPhone6,1": "iPhone 5s",
|
||||
"iPhone6,2": "iPhone 5s",
|
||||
"iPhone7,2": "iPhone 6",
|
||||
"iPhone7,1": "iPhone 6 Plus",
|
||||
"iPhone7,2": "iPhone 6",
|
||||
"iPhone8,1": "iPhone 6s",
|
||||
"iPhone8,2": "iPhone 6s Plus",
|
||||
"iPhone8,4": "iPhone SE",
|
||||
"iPhone9,1": "iPhone 7",
|
||||
"iPhone9,3": "iPhone 7",
|
||||
"iPhone9,2": "iPhone 7 Plus",
|
||||
"iPhone9,3": "iPhone 7",
|
||||
"iPhone9,4": "iPhone 7 Plus",
|
||||
"iPhone10,1": "iPhone 8",
|
||||
"iPhone10,4": "iPhone 8",
|
||||
"iPhone10,2": "iPhone 8 Plus",
|
||||
"iPhone10,5": "iPhone 8 Plus",
|
||||
"iPhone10,3": "iPhone X",
|
||||
"iPhone10,4": "iPhone 8",
|
||||
"iPhone10,5": "iPhone 8 Plus",
|
||||
"iPhone10,6": "iPhone X",
|
||||
"iPhone11,2": "iPhone Xs",
|
||||
"iPhone11,6": "iPhone Xs Max",
|
||||
@@ -127,10 +143,10 @@
|
||||
"iPhone13,2": "iPhone 12",
|
||||
"iPhone13,3": "iPhone 12 Pro",
|
||||
"iPhone13,4": "iPhone 12 Pro Max",
|
||||
"iPhone14,4": "iPhone 13 mini",
|
||||
"iPhone14,5": "iPhone 13",
|
||||
"iPhone14,2": "iPhone 13 Pro",
|
||||
"iPhone14,3": "iPhone 13 Pro Max",
|
||||
"iPhone14,4": "iPhone 13 mini",
|
||||
"iPhone14,5": "iPhone 13",
|
||||
"iPhone14,6": "iPhone SE",
|
||||
"iPhone14,7": "iPhone 14",
|
||||
"iPhone14,8": "iPhone 14 Plus",
|
||||
@@ -140,6 +156,19 @@
|
||||
"iPhone15,5": "iPhone 15 Plus",
|
||||
"iPhone16,1": "iPhone 15 Pro",
|
||||
"iPhone16,2": "iPhone 15 Pro Max",
|
||||
"iPhone17,1": "iPhone 16 Pro",
|
||||
"iPhone17,2": "iPhone 16 Pro Max",
|
||||
"iPhone17,3": "iPhone 16",
|
||||
"iPhone17,4": "iPhone 16 Plus",
|
||||
"iPhone17,5": "iPhone 16e",
|
||||
"iPhone18,1": "iPhone 17 Pro",
|
||||
"iPhone18,2": "iPhone 17 Pro Max",
|
||||
"iPhone18,3": "iPhone 17",
|
||||
"iPhone18,4": "iPhone Air",
|
||||
"iPhone18,5": "iPhone 17e",
|
||||
"iPhone19,2": "iPhone 18 Pro",
|
||||
"iPhone19,3": "iPhone 18 Pro Max",
|
||||
"iPhone19,7": "iPhone 18 Pro Max",
|
||||
"iPod1,1": "iPod touch Original",
|
||||
"iPod2,1": "iPod touch 2nd",
|
||||
"iPod3,1": "iPod touch 3rd Gen",
|
||||
@@ -147,4 +176,4 @@
|
||||
"iPod5,1": "iPod touch 5th",
|
||||
"iPod7,1": "iPod touch 6th Gen",
|
||||
"iPod9,1": "iPod touch 7th Gen"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,7 +186,8 @@ export const resources = pgTable(
|
||||
skipToIdpId: integer("skipToIdpId").references(() => idp.idpId, {
|
||||
onDelete: "set null"
|
||||
}),
|
||||
headers: text("headers"), // comma-separated list of headers to add to the request
|
||||
requestHeaders: text("requestHeaders"),
|
||||
responseHeaders: text("responseHeaders"),
|
||||
proxyProtocol: boolean("proxyProtocol").notNull().default(false),
|
||||
proxyProtocolVersion: integer("proxyProtocolVersion").default(1),
|
||||
maintenanceModeEnabled: boolean("maintenanceModeEnabled")
|
||||
@@ -1172,6 +1173,7 @@ export const resourceRules = pgTable("resourceRules", {
|
||||
| "COUNTRY_IS_NOT"
|
||||
| "ASN"
|
||||
| "REGION"
|
||||
| "METHOD"
|
||||
>()
|
||||
.notNull(), // CIDR, PATH, IP
|
||||
value: varchar("value").notNull()
|
||||
@@ -1196,6 +1198,7 @@ export const resourcePolicyRules = pgTable("resourcePolicyRules", {
|
||||
| "COUNTRY_IS_NOT"
|
||||
| "ASN"
|
||||
| "REGION"
|
||||
| "METHOD"
|
||||
>()
|
||||
.notNull(),
|
||||
value: varchar("value").notNull()
|
||||
|
||||
@@ -202,7 +202,8 @@ export const resources = sqliteTable(
|
||||
skipToIdpId: integer("skipToIdpId").references(() => idp.idpId, {
|
||||
onDelete: "set null"
|
||||
}),
|
||||
headers: text("headers"), // comma-separated list of headers to add to the request
|
||||
requestHeaders: text("requestHeaders"),
|
||||
responseHeaders: text("responseHeaders"),
|
||||
proxyProtocol: integer("proxyProtocol", { mode: "boolean" })
|
||||
.notNull()
|
||||
.default(false),
|
||||
@@ -1446,6 +1447,7 @@ export const resourceRules = sqliteTable("resourceRules", {
|
||||
| "COUNTRY_IS_NOT"
|
||||
| "ASN"
|
||||
| "REGION"
|
||||
| "METHOD"
|
||||
>()
|
||||
.notNull(), // CIDR, PATH, IP
|
||||
value: text("value").notNull()
|
||||
@@ -1502,6 +1504,7 @@ export const resourcePolicyRules = sqliteTable("resourcePolicyRules", {
|
||||
| "COUNTRY_IS_NOT"
|
||||
| "ASN"
|
||||
| "REGION"
|
||||
| "METHOD"
|
||||
>()
|
||||
.notNull(),
|
||||
value: text("value").notNull()
|
||||
|
||||
@@ -235,7 +235,7 @@ export const AlertNotification = (props: AlertNotificationProps) => {
|
||||
}
|
||||
]
|
||||
: []),
|
||||
{ label: "Time", value: new Date().toUTCString() },
|
||||
{ label: "Time (UTC)", value: new Date().toUTCString() },
|
||||
...dataItems
|
||||
];
|
||||
|
||||
@@ -265,8 +265,8 @@ export const AlertNotification = (props: AlertNotificationProps) => {
|
||||
</EmailText>
|
||||
{isTestAlert && (
|
||||
<EmailText>
|
||||
This is a test alert. No action is required,
|
||||
and no real event has occurred.
|
||||
This is a test alert. No action is required, and
|
||||
no real event has occurred.
|
||||
</EmailText>
|
||||
)}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { formatBackupTimestamp } from "./backupFileName";
|
||||
import { formatBackupFileName, formatBackupTimestamp } from "./backupFileName";
|
||||
import { assertEquals } from "@test/assert";
|
||||
|
||||
// Local-time constructors are used throughout, matching formatBackupTimestamp,
|
||||
@@ -29,7 +29,9 @@ function testMonthIsOneIndexed() {
|
||||
}
|
||||
|
||||
{
|
||||
const result = formatBackupTimestamp(new Date(2026, 11, 31, 23, 59, 59));
|
||||
const result = formatBackupTimestamp(
|
||||
new Date(2026, 11, 31, 23, 59, 59)
|
||||
);
|
||||
assertEquals(
|
||||
result,
|
||||
"2026-12-31_23-59-59",
|
||||
@@ -73,9 +75,7 @@ function testNamesSortChronologically() {
|
||||
new Date(2026, 11, 31, 23, 59, 59)
|
||||
];
|
||||
|
||||
const sorted = taken
|
||||
.map((date) => formatBackupTimestamp(date))
|
||||
.sort();
|
||||
const sorted = taken.map((date) => formatBackupTimestamp(date)).sort();
|
||||
|
||||
assertEquals(
|
||||
sorted.join(","),
|
||||
@@ -89,11 +89,48 @@ function testNamesSortChronologically() {
|
||||
);
|
||||
}
|
||||
|
||||
function testFormatBackupFileName() {
|
||||
console.log("Running backup file name formatting tests...");
|
||||
|
||||
const date = new Date(2026, 8, 12, 20, 35, 56);
|
||||
|
||||
// With semver version string without leading 'v'
|
||||
assertEquals(
|
||||
formatBackupFileName("1.22.0", date),
|
||||
"db_2026-09-12_20-35-56_v1.22.0.sqlite",
|
||||
"Filename must include timestamp and prefixed version tag"
|
||||
);
|
||||
|
||||
// With version string already containing 'v'
|
||||
assertEquals(
|
||||
formatBackupFileName("v1.22.0", date),
|
||||
"db_2026-09-12_20-35-56_v1.22.0.sqlite",
|
||||
"Filename must not duplicate 'v' prefix if already present"
|
||||
);
|
||||
|
||||
// Without version (fallback/default)
|
||||
assertEquals(
|
||||
formatBackupFileName(undefined, date),
|
||||
"db_2026-09-12_20-35-56.sqlite",
|
||||
"Filename without version must match default timestamped format"
|
||||
);
|
||||
|
||||
// Distinct versions within the exact same second do not collide
|
||||
const sameSecondFile1 = formatBackupFileName("1.21.0", date);
|
||||
const sameSecondFile2 = formatBackupFileName("1.22.0", date);
|
||||
if (sameSecondFile1 === sameSecondFile2) {
|
||||
throw new Error(
|
||||
"Backup file names for different versions in the same second must not collide"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Run all tests
|
||||
try {
|
||||
testMonthIsOneIndexed();
|
||||
testEveryFieldIsZeroPadded();
|
||||
testNamesSortChronologically();
|
||||
testFormatBackupFileName();
|
||||
console.log("All tests passed successfully!");
|
||||
} catch (error) {
|
||||
console.error("Test failed:", error);
|
||||
|
||||
@@ -26,3 +26,26 @@ export function formatBackupTimestamp(date: Date = new Date()): string {
|
||||
|
||||
return `${datePart}_${timePart}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the full database backup file name, including timestamp and optional version tag.
|
||||
*
|
||||
* When a migration version is provided, the filename includes `_v<version>`,
|
||||
* preventing collisions between multiple migrations running in the same second and making it easy
|
||||
* to identify the migration state contained in the backup.
|
||||
*
|
||||
* @param version Optional migration version being run.
|
||||
* @param date The moment the backup is being taken. Defaults to now.
|
||||
* @returns A filename of the form `db_YYYY-MM-DD_HH-MM-SS_v<version>.sqlite` or `db_YYYY-MM-DD_HH-MM-SS.sqlite`.
|
||||
*/
|
||||
export function formatBackupFileName(
|
||||
version?: string,
|
||||
date: Date = new Date()
|
||||
): string {
|
||||
const timestamp = formatBackupTimestamp(date);
|
||||
if (version) {
|
||||
const versionTag = version.startsWith("v") ? version : `v${version}`;
|
||||
return `db_${timestamp}_${versionTag}.sqlite`;
|
||||
}
|
||||
return `db_${timestamp}.sqlite`;
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@ export * from "./features";
|
||||
export * from "./limitsService";
|
||||
export * from "./getOrgTierData";
|
||||
export * from "./createCustomer";
|
||||
export * from "./linkEmailOrg";
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export async function linkEmailOrg(
|
||||
orgId: string,
|
||||
email: string | null | undefined
|
||||
): Promise<void> {
|
||||
return;
|
||||
}
|
||||
@@ -48,7 +48,13 @@ import { defaultRoleAllowedActions } from "@server/routers/role/createRole";
|
||||
import { pickPort } from "@server/routers/target/helpers";
|
||||
import { and, asc, eq, isNotNull, ne } from "drizzle-orm";
|
||||
import { tierMatrix } from "../billing/tierMatrix";
|
||||
import { isValidCIDR, isValidIP, isValidUrlGlobPattern } from "../validators";
|
||||
import {
|
||||
isValidCIDR,
|
||||
isValidHttpMethodList,
|
||||
isValidIP,
|
||||
isValidUrlGlobPattern,
|
||||
parseHttpMethodList
|
||||
} from "../validators";
|
||||
import { Config, isTargetsOnlyResource, TargetData } from "./types";
|
||||
import { getOrCreateLabelIds, syncResourceLabels } from "./labels";
|
||||
import { findOrgUsersByIdentifier } from "./findOrgUser";
|
||||
@@ -255,10 +261,18 @@ export async function updatePublicResources(
|
||||
resourceData.ssl == undefined || resourceData.ssl == null
|
||||
? true
|
||||
: resourceData.ssl;
|
||||
let headers = "";
|
||||
if (resourceData.headers) {
|
||||
headers = JSON.stringify(resourceData.headers);
|
||||
}
|
||||
// `headers` is a deprecated alias for `requestHeaders`
|
||||
const mergedRequestHeaders = [
|
||||
...(resourceData.headers ?? []),
|
||||
...(resourceData.requestHeaders ?? [])
|
||||
];
|
||||
const requestHeaders =
|
||||
mergedRequestHeaders.length > 0
|
||||
? JSON.stringify(mergedRequestHeaders)
|
||||
: null;
|
||||
const responseHeaders = resourceData.responseHeaders?.length
|
||||
? JSON.stringify(resourceData.responseHeaders)
|
||||
: null;
|
||||
|
||||
if (resourceData.policy) {
|
||||
const isLicensed = await isLicensedOrSubscribed(
|
||||
@@ -397,7 +411,8 @@ export async function updatePublicResources(
|
||||
? resourceData.auth["whitelist-users"].length >
|
||||
0
|
||||
: false,
|
||||
headers: headers || null,
|
||||
requestHeaders,
|
||||
responseHeaders,
|
||||
applyRules:
|
||||
resourceData.rules &&
|
||||
resourceData.rules.length > 0,
|
||||
@@ -593,7 +608,8 @@ export async function updatePublicResources(
|
||||
setHostHeader: resourceData["host-header"] || null,
|
||||
tlsServerName:
|
||||
resourceData["tls-server-name"] || null,
|
||||
headers: headers || null,
|
||||
requestHeaders,
|
||||
responseHeaders,
|
||||
maintenanceModeEnabled:
|
||||
resourceData.maintenance?.enabled,
|
||||
maintenanceModeType: resourceData.maintenance?.type,
|
||||
@@ -1216,7 +1232,8 @@ export async function updatePublicResources(
|
||||
setHostHeader: resourceData["host-header"] || null,
|
||||
tlsServerName: resourceData["tls-server-name"] || null,
|
||||
ssl: resourceSsl,
|
||||
headers: headers || null,
|
||||
requestHeaders,
|
||||
responseHeaders,
|
||||
applyRules:
|
||||
resourceData.rules && resourceData.rules.length > 0,
|
||||
pamMode: resourceData["auth-daemon"]?.pam || "passthrough",
|
||||
@@ -1453,6 +1470,10 @@ function getRuleValue(match: string, value: string) {
|
||||
if (match === "COUNTRY" || match === "COUNTRY_IS_NOT") {
|
||||
return value.toUpperCase();
|
||||
}
|
||||
// normalize the method list so it is stored as "POST,PUT"
|
||||
if (match === "METHOD") {
|
||||
return parseHttpMethodList(value).join(",");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -1473,6 +1494,10 @@ function validateRule(rule: any) {
|
||||
if (!isValidRegionId(rule.value)) {
|
||||
throw new Error(`Invalid region ID provided: ${rule.value}`);
|
||||
}
|
||||
} else if (rule.match === "method") {
|
||||
if (!isValidHttpMethodList(rule.value)) {
|
||||
throw new Error(`Invalid HTTP method provided: ${rule.value}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,13 @@ import logger from "@server/logger";
|
||||
import { getUniqueResourcePolicyName } from "@server/db/names";
|
||||
import { hashPassword } from "@server/auth/password";
|
||||
import { idpExistsForOrg } from "@server/lib/idp/idpExistsForOrg";
|
||||
import { isValidCIDR, isValidIP, isValidUrlGlobPattern } from "../validators";
|
||||
import {
|
||||
isValidCIDR,
|
||||
isValidHttpMethodList,
|
||||
isValidIP,
|
||||
isValidUrlGlobPattern,
|
||||
ResourceRuleMatchType
|
||||
} from "../validators";
|
||||
import { isLicensedOrSubscribed } from "#dynamic/lib/isLicencedOrSubscribed";
|
||||
import { tierMatrix } from "../billing/tierMatrix";
|
||||
import { findOrgUsersByIdentifier } from "./findOrgUser";
|
||||
@@ -66,6 +72,13 @@ export async function updateResourcePolicies(
|
||||
throw new Error(
|
||||
`Invalid URL glob pattern provided in resource policy '${policyNiceId}': ${rule.value}`
|
||||
);
|
||||
} else if (
|
||||
rule.match === "method" &&
|
||||
!isValidHttpMethodList(rule.value)
|
||||
) {
|
||||
throw new Error(
|
||||
`Invalid HTTP method provided in resource policy '${policyNiceId}': ${rule.value}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,17 +352,8 @@ function getRuleAction(input: string): "ACCEPT" | "DROP" | "PASS" {
|
||||
return "PASS";
|
||||
}
|
||||
|
||||
function getRuleMatch(
|
||||
input: string
|
||||
): "CIDR" | "IP" | "PATH" | "COUNTRY" | "COUNTRY_IS_NOT" | "ASN" | "REGION" {
|
||||
return input.toUpperCase() as
|
||||
| "CIDR"
|
||||
| "IP"
|
||||
| "PATH"
|
||||
| "COUNTRY"
|
||||
| "COUNTRY_IS_NOT"
|
||||
| "ASN"
|
||||
| "REGION";
|
||||
function getRuleMatch(input: string): ResourceRuleMatchType {
|
||||
return input.toUpperCase() as ResourceRuleMatchType;
|
||||
}
|
||||
|
||||
async function syncRolePolicies(
|
||||
|
||||
@@ -3,6 +3,7 @@ import { existsSync } from "node:fs";
|
||||
import { portRangeStringSchema } from "@server/lib/ip";
|
||||
import { MaintenanceSchema } from "#dynamic/lib/blueprints/MaintenanceSchema";
|
||||
import { isValidRegionId } from "@server/db/regions";
|
||||
import { isValidHttpMethodList } from "@server/lib/validators";
|
||||
import { wildcardSubdomainSchema } from "@server/lib/schemas";
|
||||
import config from "@server/lib/config";
|
||||
import {
|
||||
@@ -127,7 +128,16 @@ export const AuthSchema = z.object({
|
||||
export const RuleSchema = z
|
||||
.object({
|
||||
action: z.enum(["allow", "deny", "pass"]),
|
||||
match: z.enum(["cidr", "path", "ip", "country", "country_is_not", "asn", "region"]),
|
||||
match: z.enum([
|
||||
"cidr",
|
||||
"path",
|
||||
"ip",
|
||||
"country",
|
||||
"country_is_not",
|
||||
"asn",
|
||||
"region",
|
||||
"method"
|
||||
]),
|
||||
value: z.coerce.string(),
|
||||
priority: z.int().optional(),
|
||||
enabled: z.boolean().optional().default(true)
|
||||
@@ -207,6 +217,19 @@ export const RuleSchema = z
|
||||
message:
|
||||
"Value must be a valid UN M.49 region or subregion ID when match is 'region'"
|
||||
}
|
||||
)
|
||||
.refine(
|
||||
(rule) => {
|
||||
if (rule.match === "method") {
|
||||
return isValidHttpMethodList(rule.value);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
{
|
||||
path: ["value"],
|
||||
message:
|
||||
"Value must be a comma-separated list of HTTP methods when match is 'method', e.g. 'POST,PUT'"
|
||||
}
|
||||
);
|
||||
|
||||
export const HeaderSchema = z.object({
|
||||
@@ -303,7 +326,9 @@ export const PublicResourceSchema = z
|
||||
auth: AuthSchema.optional(),
|
||||
"host-header": z.string().optional(),
|
||||
"tls-server-name": z.string().optional(),
|
||||
headers: z.array(HeaderSchema).optional(),
|
||||
headers: z.array(HeaderSchema).optional(), // deprecated alias for requestHeaders
|
||||
requestHeaders: z.array(HeaderSchema).optional(),
|
||||
responseHeaders: z.array(HeaderSchema).optional(),
|
||||
rules: z.array(RuleSchema).optional(),
|
||||
maintenance: MaintenanceSchema.optional(),
|
||||
"auth-daemon": AuthDaemonSchema.optional(),
|
||||
|
||||
@@ -20,6 +20,11 @@ function getSegmentRegex(patternPart: string): RegExp {
|
||||
// resolves `.` / `..` segments, so a request like `/public%2F..%2Fadmin/`
|
||||
// or `/public/../admin/` is matched as `/admin/`, not as a literal segment
|
||||
// or a wildcard-swallowed sequence under `/public/*`.
|
||||
//
|
||||
// Applied to both the request path and the rule pattern: the pattern
|
||||
// validator only accepts spaces / non-ASCII in percent-encoded form, so a
|
||||
// rule like `/my%20docs/*` must be compared against the decoded segment
|
||||
// `my docs`, not the literal text `my%20docs`.
|
||||
function decodeAndResolvePath(p: string): string[] {
|
||||
const rawParts = p.split("/").filter(Boolean);
|
||||
|
||||
@@ -48,7 +53,7 @@ function decodeAndResolvePath(p: string): string[] {
|
||||
}
|
||||
|
||||
export function isPathAllowed(pattern: string, path: string): boolean {
|
||||
const patternParts = pattern.split("/").filter(Boolean);
|
||||
const patternParts = decodeAndResolvePath(pattern);
|
||||
const pathParts = decodeAndResolvePath(path);
|
||||
|
||||
function matchSegments(
|
||||
|
||||
@@ -97,7 +97,8 @@ export async function getTraefikConfig(
|
||||
tlsServerName: resources.tlsServerName,
|
||||
setHostHeader: resources.setHostHeader,
|
||||
enableProxy: resources.enableProxy,
|
||||
headers: resources.headers,
|
||||
requestHeaders: resources.requestHeaders,
|
||||
responseHeaders: resources.responseHeaders,
|
||||
proxyProtocol: resources.proxyProtocol,
|
||||
proxyProtocolVersion: resources.proxyProtocolVersion,
|
||||
wildcard: resources.wildcard,
|
||||
@@ -227,7 +228,8 @@ export async function getTraefikConfig(
|
||||
setHostHeader: row.setHostHeader,
|
||||
enableProxy: row.enableProxy,
|
||||
targets: [],
|
||||
headers: row.headers,
|
||||
requestHeaders: row.requestHeaders,
|
||||
responseHeaders: row.responseHeaders,
|
||||
proxyProtocol: row.proxyProtocol,
|
||||
proxyProtocolVersion: row.proxyProtocolVersion ?? 1,
|
||||
path: row.path, // the targets will all have the same path
|
||||
@@ -482,7 +484,8 @@ export async function getTraefikConfig(
|
||||
|
||||
// Handle custom headers middleware
|
||||
const customHeadersMiddleware = buildCustomHeadersMiddleware(
|
||||
resource.headers,
|
||||
resource.requestHeaders,
|
||||
resource.responseHeaders,
|
||||
resource.setHostHeader,
|
||||
resource.resourceId
|
||||
);
|
||||
|
||||
@@ -1,46 +1,78 @@
|
||||
import logger from "@server/logger";
|
||||
|
||||
function parseHeaders(
|
||||
headers: string,
|
||||
label: string,
|
||||
resourceId: number
|
||||
): { name: string; value: string }[] {
|
||||
try {
|
||||
return JSON.parse(headers) as {
|
||||
name: string;
|
||||
value: string;
|
||||
}[];
|
||||
} catch (e) {
|
||||
logger.warn(
|
||||
`Failed to parse ${label} for resource ${resourceId}: ${e}`
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the customRequestHeaders middleware definition for a resource's
|
||||
* custom headers + setHostHeader config. Returns null when there are no
|
||||
* headers to set, so the caller can skip attaching the middleware.
|
||||
* Build the custom headers middleware definition for a resource's
|
||||
* custom request/response headers + setHostHeader config. Returns null when
|
||||
* there are no headers to set, so the caller can skip attaching the
|
||||
* middleware.
|
||||
*/
|
||||
export function buildCustomHeadersMiddleware(
|
||||
headers: string | null | undefined,
|
||||
requestHeaders: string | null | undefined,
|
||||
responseHeaders: string | null | undefined,
|
||||
setHostHeader: string | null | undefined,
|
||||
resourceId: number
|
||||
): { headers: { customRequestHeaders: { [key: string]: string } } } | null {
|
||||
const headersObj: { [key: string]: string } = {};
|
||||
): {
|
||||
headers: {
|
||||
customRequestHeaders?: { [key: string]: string };
|
||||
customResponseHeaders?: { [key: string]: string };
|
||||
};
|
||||
} | null {
|
||||
const requestHeadersObj: { [key: string]: string } = {};
|
||||
const responseHeadersObj: { [key: string]: string } = {};
|
||||
|
||||
if (headers) {
|
||||
let headersArr: { name: string; value: string }[] = [];
|
||||
try {
|
||||
headersArr = JSON.parse(headers) as {
|
||||
name: string;
|
||||
value: string;
|
||||
}[];
|
||||
} catch (e) {
|
||||
logger.warn(
|
||||
`Failed to parse headers for resource ${resourceId}: ${e}`
|
||||
);
|
||||
}
|
||||
|
||||
headersArr.forEach((header) => {
|
||||
headersObj[header.name] = header.value;
|
||||
});
|
||||
if (requestHeaders) {
|
||||
parseHeaders(requestHeaders, "requestHeaders", resourceId).forEach(
|
||||
(header) => {
|
||||
requestHeadersObj[header.name] = header.value;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (setHostHeader) {
|
||||
headersObj["Host"] = setHostHeader;
|
||||
requestHeadersObj["Host"] = setHostHeader;
|
||||
}
|
||||
|
||||
if (Object.keys(headersObj).length === 0) {
|
||||
if (responseHeaders) {
|
||||
parseHeaders(responseHeaders, "responseHeaders", resourceId).forEach(
|
||||
(header) => {
|
||||
responseHeadersObj[header.name] = header.value;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const hasRequestHeaders = Object.keys(requestHeadersObj).length > 0;
|
||||
const hasResponseHeaders = Object.keys(responseHeadersObj).length > 0;
|
||||
|
||||
if (!hasRequestHeaders && !hasResponseHeaders) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
headers: {
|
||||
customRequestHeaders: headersObj
|
||||
...(hasRequestHeaders && {
|
||||
customRequestHeaders: requestHeadersObj
|
||||
}),
|
||||
...(hasResponseHeaders && {
|
||||
customResponseHeaders: responseHeadersObj
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import {
|
||||
getResourceRuleValueValidationError,
|
||||
isValidDomain,
|
||||
isValidUrlGlobPattern
|
||||
isValidUrlGlobPattern,
|
||||
parseHttpMethodList
|
||||
} from "./validators";
|
||||
import { assertEquals } from "@test/assert";
|
||||
import { assertEquals, assertEqualsObj } from "@test/assert";
|
||||
|
||||
function runTests() {
|
||||
console.log("Running domain validation tests...");
|
||||
@@ -295,6 +296,44 @@ function runTests() {
|
||||
"Invalid ASN should return an error"
|
||||
);
|
||||
|
||||
// HTTP method validation tests
|
||||
assertEquals(
|
||||
getResourceRuleValueValidationError("METHOD", "POST"),
|
||||
null,
|
||||
"Single HTTP method should be valid"
|
||||
);
|
||||
assertEquals(
|
||||
getResourceRuleValueValidationError("METHOD", " post , Put "),
|
||||
null,
|
||||
"Method list should be valid with mixed case and whitespace"
|
||||
);
|
||||
assertEquals(
|
||||
getResourceRuleValueValidationError("METHOD", "PROPFIND"),
|
||||
null,
|
||||
"Extension methods such as the WebDAV verbs should be valid"
|
||||
);
|
||||
assertEquals(
|
||||
getResourceRuleValueValidationError("METHOD", ""),
|
||||
"Invalid HTTP method provided",
|
||||
"Empty method list should return an error"
|
||||
);
|
||||
assertEquals(
|
||||
getResourceRuleValueValidationError("METHOD", ",,"),
|
||||
"Invalid HTTP method provided",
|
||||
"Method list of only separators should return an error"
|
||||
);
|
||||
assertEquals(
|
||||
getResourceRuleValueValidationError("METHOD", "GET POST"),
|
||||
"Invalid HTTP method provided",
|
||||
"Space separated methods should return an error"
|
||||
);
|
||||
|
||||
assertEqualsObj(
|
||||
parseHttpMethodList(" get ,post, "),
|
||||
["GET", "POST"],
|
||||
"Method list should be normalized to uppercase without empty entries"
|
||||
);
|
||||
|
||||
console.log("All tests passed!");
|
||||
}
|
||||
|
||||
|
||||
@@ -76,9 +76,46 @@ export const RESOURCE_RULE_MATCH_TYPES = [
|
||||
"COUNTRY",
|
||||
"COUNTRY_IS_NOT",
|
||||
"ASN",
|
||||
"REGION"
|
||||
"REGION",
|
||||
"METHOD"
|
||||
] as const;
|
||||
|
||||
// The methods offered in the UI: the eight from RFC 9110 plus PATCH (RFC 5789)
|
||||
// and QUERY (RFC 10008). A METHOD rule is not limited to these, since
|
||||
// isValidHttpMethodList accepts any method token, so blueprints and the API can
|
||||
// also target extension methods such as the WebDAV verbs.
|
||||
export const HTTP_METHODS = [
|
||||
"GET",
|
||||
"HEAD",
|
||||
"POST",
|
||||
"PUT",
|
||||
"PATCH",
|
||||
"DELETE",
|
||||
"OPTIONS",
|
||||
"TRACE",
|
||||
"CONNECT",
|
||||
"QUERY"
|
||||
] as const;
|
||||
|
||||
// RFC 9110 token, minus the characters that would collide with the
|
||||
// comma-separated list encoding.
|
||||
const HTTP_METHOD_REGEX = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
||||
|
||||
export function parseHttpMethodList(value: string): string[] {
|
||||
return value
|
||||
.split(",")
|
||||
.map((method) => method.trim().toUpperCase())
|
||||
.filter((method) => method.length > 0);
|
||||
}
|
||||
|
||||
export function isValidHttpMethodList(value: string): boolean {
|
||||
const methods = parseHttpMethodList(value);
|
||||
return (
|
||||
methods.length > 0 &&
|
||||
methods.every((method) => HTTP_METHOD_REGEX.test(method))
|
||||
);
|
||||
}
|
||||
|
||||
export type ResourceRuleMatchType = (typeof RESOURCE_RULE_MATCH_TYPES)[number];
|
||||
|
||||
export function getResourceRuleValueValidationError(
|
||||
@@ -101,6 +138,10 @@ export function getResourceRuleValueValidationError(
|
||||
return COUNTRIES.some((country) => country.code === value)
|
||||
? null
|
||||
: "Invalid country code provided";
|
||||
case "METHOD":
|
||||
return isValidHttpMethodList(value)
|
||||
? null
|
||||
: "Invalid HTTP method provided";
|
||||
case "ASN":
|
||||
const normalizedValue = value.trim().toUpperCase();
|
||||
return /^AS\d+$/.test(normalizedValue) ||
|
||||
|
||||
@@ -70,13 +70,8 @@ export async function verifyApiKeyAccess(
|
||||
);
|
||||
}
|
||||
|
||||
if (!apiKeyOrg.orgId) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
`API key with ID ${apiKeyId} does not have an organization ID`
|
||||
)
|
||||
);
|
||||
if (!apiKey.apiKeyOrg?.orgId) {
|
||||
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, `API key with ID ${apiKeyId} does not have an organization ID`));
|
||||
}
|
||||
|
||||
if (!req.userOrg) {
|
||||
@@ -86,7 +81,7 @@ export async function verifyApiKeyAccess(
|
||||
.where(
|
||||
and(
|
||||
eq(userOrgs.userId, userId),
|
||||
eq(userOrgs.orgId, apiKeyOrg.orgId)
|
||||
eq(userOrgs.orgId, apiKey.apiKeyOrg.orgId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
@@ -13,3 +13,4 @@
|
||||
|
||||
export * from "./getOrgTierData";
|
||||
export * from "./createCustomer";
|
||||
export * from "./linkEmailOrg";
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* This file is part of a proprietary work.
|
||||
*
|
||||
* Copyright (c) 2025-2026 Fossorial, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is licensed under the Fossorial Commercial License.
|
||||
* You may not use this file except in compliance with the License.
|
||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
||||
*
|
||||
* This file is not licensed under the AGPLv3.
|
||||
*/
|
||||
|
||||
import logger from "@server/logger";
|
||||
import privateConfig from "#private/lib/config";
|
||||
import { build } from "@server/build";
|
||||
|
||||
export async function linkEmailOrg(
|
||||
orgId: string,
|
||||
email: string | null | undefined
|
||||
): Promise<void> {
|
||||
if (build !== "saas") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!email) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${privateConfig.getRawPrivateConfig().server.fossorial_api}/api/v1/license-internal/enterprise/link-email-org`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"api-key":
|
||||
privateConfig.getRawPrivateConfig().server
|
||||
.fossorial_api_key!,
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({ email, orgId })
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok && response.status !== 404) {
|
||||
logger.error(
|
||||
`Fossorial API returned ${response.status} when linking email ${email} to orgId ${orgId}: ${await response.text()}`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Error notifying Fossorial API of email/org link for orgId ${orgId}:`,
|
||||
error
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -138,7 +138,8 @@ export async function getTraefikConfig(
|
||||
tlsServerName: resources.tlsServerName,
|
||||
setHostHeader: resources.setHostHeader,
|
||||
enableProxy: resources.enableProxy,
|
||||
headers: resources.headers,
|
||||
requestHeaders: resources.requestHeaders,
|
||||
responseHeaders: resources.responseHeaders,
|
||||
proxyProtocol: resources.proxyProtocol,
|
||||
proxyProtocolVersion: resources.proxyProtocolVersion,
|
||||
wildcard: resources.wildcard,
|
||||
@@ -280,7 +281,8 @@ export async function getTraefikConfig(
|
||||
setHostHeader: row.setHostHeader,
|
||||
enableProxy: row.enableProxy,
|
||||
targets: [],
|
||||
headers: row.headers,
|
||||
requestHeaders: row.requestHeaders,
|
||||
responseHeaders: row.responseHeaders,
|
||||
proxyProtocol: row.proxyProtocol,
|
||||
proxyProtocolVersion: row.proxyProtocolVersion ?? 1,
|
||||
path: row.path, // the targets will all have the same path
|
||||
@@ -811,7 +813,8 @@ export async function getTraefikConfig(
|
||||
);
|
||||
|
||||
const customHeadersMiddleware = buildCustomHeadersMiddleware(
|
||||
resource.headers,
|
||||
resource.requestHeaders,
|
||||
resource.responseHeaders,
|
||||
resource.setHostHeader,
|
||||
resource.resourceId
|
||||
);
|
||||
|
||||
@@ -50,6 +50,27 @@ type ValidateLicenseAPIResponse = {
|
||||
status: number;
|
||||
};
|
||||
|
||||
// Ranks license tiers so that when multiple license keys are active, the
|
||||
// highest tier among them wins. Order: personal < tier1 < tier2 < ... <
|
||||
// tier[n] < enterprise. Tier numbers are parsed so this scales to any
|
||||
// tier[n] without needing updates here.
|
||||
function tierRank(tier?: LicenseKeyTier): number {
|
||||
if (!tier) {
|
||||
return -1;
|
||||
}
|
||||
if (tier === "enterprise") {
|
||||
return Number.MAX_SAFE_INTEGER;
|
||||
}
|
||||
if (tier === "personal") {
|
||||
return 0;
|
||||
}
|
||||
const match = /^tier(\d+)$/.exec(tier);
|
||||
if (match) {
|
||||
return parseInt(match[1], 10);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
type TokenPayload = {
|
||||
valid: boolean;
|
||||
type: LicenseKeyType;
|
||||
@@ -276,6 +297,11 @@ LQIDAQAB
|
||||
if (!apiResponse?.success) {
|
||||
throw new Error(apiResponse?.error);
|
||||
}
|
||||
|
||||
logger.debug(
|
||||
`License server response: ${JSON.stringify(apiResponse)}`
|
||||
);
|
||||
|
||||
// Reset failure count on success
|
||||
this.phoneHomeFailureCount = 0;
|
||||
} catch (e) {
|
||||
@@ -338,6 +364,11 @@ LQIDAQAB
|
||||
licenseKeyRes,
|
||||
this.publicKey
|
||||
);
|
||||
|
||||
logger.debug(
|
||||
`Decoded license key ${key.licenseKey}: ${JSON.stringify(payload)}`
|
||||
);
|
||||
|
||||
cached.valid = payload.valid;
|
||||
cached.type = payload.type;
|
||||
cached.tier = payload.tier;
|
||||
@@ -370,13 +401,52 @@ LQIDAQAB
|
||||
}
|
||||
}
|
||||
|
||||
// Personal-tier licenses cannot coexist with a paid tier: if any
|
||||
// valid host key is above personal, personal-tier keys are
|
||||
// invalidated so they don't contribute to the totals below.
|
||||
const hasHigherTierValidKey = keys.some((key) => {
|
||||
const cached = newCache.get(key.licenseKey)!;
|
||||
return (
|
||||
cached.type === "host" &&
|
||||
cached.valid &&
|
||||
tierRank(cached.tier) > tierRank("personal")
|
||||
);
|
||||
});
|
||||
|
||||
if (hasHigherTierValidKey) {
|
||||
for (const key of keys) {
|
||||
const cached = newCache.get(key.licenseKey)!;
|
||||
if (
|
||||
cached.type === "host" &&
|
||||
cached.valid &&
|
||||
cached.tier === "personal"
|
||||
) {
|
||||
logger.debug(
|
||||
`Invalidating personal license key ${key.licenseKey} because a higher tier license is present`
|
||||
);
|
||||
cached.valid = false;
|
||||
newCache.set(key.licenseKey, cached);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compute host status: quantity = users, quantity_2 = sites
|
||||
// When multiple host keys are active, prefer a valid key over an
|
||||
// invalid one, and among equally-valid keys prefer the highest tier.
|
||||
let selectedHostKey: LicenseKeyCache | undefined;
|
||||
for (const key of keys) {
|
||||
const cached = newCache.get(key.licenseKey)!;
|
||||
|
||||
if (cached.type === "host") {
|
||||
status.isLicenseValid = cached.valid;
|
||||
status.tier = cached.tier;
|
||||
if (
|
||||
!selectedHostKey ||
|
||||
(cached.valid && !selectedHostKey.valid) ||
|
||||
(cached.valid === selectedHostKey.valid &&
|
||||
tierRank(cached.tier) >
|
||||
tierRank(selectedHostKey.tier))
|
||||
) {
|
||||
selectedHostKey = cached;
|
||||
}
|
||||
}
|
||||
|
||||
if (!cached.valid) {
|
||||
@@ -393,6 +463,11 @@ LQIDAQAB
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedHostKey) {
|
||||
status.isLicenseValid = selectedHostKey.valid;
|
||||
status.tier = selectedHostKey.tier;
|
||||
}
|
||||
|
||||
// Invalidate license if over user or site limits
|
||||
if (
|
||||
(status.maxSites !== undefined &&
|
||||
@@ -415,6 +490,8 @@ LQIDAQAB
|
||||
this.checkInProgress = false;
|
||||
}
|
||||
|
||||
logger.debug(`Computed license status: ${JSON.stringify(status)}`);
|
||||
|
||||
this.statusCache.set(this.statusKey, status, 0);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ export async function exportConnectionAuditLogs(
|
||||
|
||||
const baseQuery = queryConnection(data);
|
||||
|
||||
const log = await baseQuery.limit(data.limit).offset(data.offset);
|
||||
const log = await baseQuery.limit(MAX_EXPORT_LIMIT);
|
||||
|
||||
const csvData = generateCSV(log);
|
||||
|
||||
|
||||
@@ -386,6 +386,38 @@ function runSpecialCharacterTests() {
|
||||
console.log("All special character tests passed!");
|
||||
}
|
||||
|
||||
function runEncodedPatternTests() {
|
||||
console.log("\nRunning percent-encoded pattern tests...");
|
||||
|
||||
// isValidUrlGlobPattern accepts percent-encoded sequences and rejects
|
||||
// raw spaces / non-ASCII, so `%20` and `%C3%A9` are the only way to write
|
||||
// a PATH rule for such a path. Badger sends the request path already
|
||||
// decoded (Go's req.URL.Path), and isPathAllowed decodes it again, so the
|
||||
// rule pattern must be decoded the same way or it can never match.
|
||||
assertEquals(
|
||||
isPathAllowed("/my%20docs/*", "/my docs/report.pdf"),
|
||||
true,
|
||||
"Percent-encoded space in pattern should match decoded request path"
|
||||
);
|
||||
assertEquals(
|
||||
isPathAllowed("/my%20docs/*", "/my%20docs/report.pdf"),
|
||||
true,
|
||||
"Percent-encoded space in pattern should match raw-encoded request path"
|
||||
);
|
||||
assertEquals(
|
||||
isPathAllowed("/caf%C3%A9", "/café"),
|
||||
true,
|
||||
"Percent-encoded UTF-8 in pattern should match decoded request path"
|
||||
);
|
||||
assertEquals(
|
||||
isPathAllowed("/my%20docs/*", "/my-docs/report.pdf"),
|
||||
false,
|
||||
"Decoded pattern must still reject a different path"
|
||||
);
|
||||
|
||||
console.log("All percent-encoded pattern tests passed!");
|
||||
}
|
||||
|
||||
function runRegionTests() {
|
||||
console.log("\nRunning isIpInRegion tests...");
|
||||
|
||||
@@ -446,6 +478,7 @@ function runRegionTests() {
|
||||
try {
|
||||
runTests();
|
||||
runSpecialCharacterTests();
|
||||
runEncodedPatternTests();
|
||||
runRegionTests();
|
||||
console.log("\n✅ All tests passed!");
|
||||
} catch (error) {
|
||||
|
||||
@@ -41,9 +41,10 @@ import {
|
||||
} from "@server/db";
|
||||
import config from "@server/lib/config";
|
||||
import { isIpInCidr, stripPortFromHost } from "@server/lib/ip";
|
||||
import { isPathAllowed } from "@server/lib/pathMatch";
|
||||
import { isPathAllowed, type isPathAllowed } from "@server/lib/pathMatch";
|
||||
import { matchesPath } from "@server/lib/traefik/rule";
|
||||
import { rewriteRequestPath } from "@server/lib/traefik/middleware";
|
||||
import { parseHttpMethodList } from "@server/lib/validators";
|
||||
import { response } from "@server/lib/response";
|
||||
import logger from "@server/logger";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
@@ -171,6 +172,7 @@ export async function verifyResourceSession(
|
||||
path,
|
||||
headers,
|
||||
query,
|
||||
method,
|
||||
badgerVersion
|
||||
} = parsedBody.data;
|
||||
|
||||
@@ -323,7 +325,8 @@ export async function verifyResourceSession(
|
||||
clientIp,
|
||||
path,
|
||||
ipCC,
|
||||
ipAsn
|
||||
ipAsn,
|
||||
method
|
||||
);
|
||||
|
||||
if (action == "ACCEPT") {
|
||||
@@ -1524,7 +1527,8 @@ async function checkRules(
|
||||
clientIp: string | undefined,
|
||||
path: string | undefined,
|
||||
ipCC?: string,
|
||||
ipAsn?: number
|
||||
ipAsn?: number,
|
||||
method?: string
|
||||
): Promise<"ACCEPT" | "DROP" | "PASS" | undefined> {
|
||||
const ruleCacheKey = `rules:${resourceId}`;
|
||||
|
||||
@@ -1599,12 +1603,24 @@ async function checkRules(
|
||||
(await isIpInRegion(ipCC, rule.value))
|
||||
) {
|
||||
return rule.action as any;
|
||||
} else if (
|
||||
method &&
|
||||
rule.match == "METHOD" &&
|
||||
isMethodAllowed(rule.value, method)
|
||||
) {
|
||||
return rule.action as any;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// rule.value holds a comma-separated list of HTTP methods, e.g. "POST,PUT".
|
||||
function isMethodAllowed(ruleValue: string, method: string): boolean {
|
||||
const requestMethod = method.toUpperCase();
|
||||
return parseHttpMethodList(ruleValue).includes(requestMethod);
|
||||
}
|
||||
|
||||
export { isPathAllowed };
|
||||
|
||||
async function isIpInGeoIP(
|
||||
|
||||
@@ -31,7 +31,7 @@ export async function addPeer(
|
||||
.where(eq(newts.siteId, siteId))
|
||||
.limit(1);
|
||||
if (!newt) {
|
||||
throw new Error(`Site found for site ${siteId}`);
|
||||
throw new Error(`Newt not found for site ${siteId}`);
|
||||
}
|
||||
newtId = newt.newtId;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import { fromError } from "zod-validation-error";
|
||||
import { defaultRoleAllowedActions } from "../role";
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
import { isValidCIDR } from "@server/lib/validators";
|
||||
import { createCustomer } from "#dynamic/lib/billing";
|
||||
import { createCustomer, linkEmailOrg } from "#dynamic/lib/billing";
|
||||
import { usageService } from "@server/lib/billing/usageService";
|
||||
import { LimitId, limitsService, freeLimitSet } from "@server/lib/billing";
|
||||
import { build } from "@server/build";
|
||||
@@ -425,6 +425,7 @@ export async function createOrg(
|
||||
customerId
|
||||
); // Only 1 because we are creating the org
|
||||
}
|
||||
await linkEmailOrg(orgId, req.user?.email);
|
||||
}
|
||||
|
||||
if (numOrgs) {
|
||||
|
||||
@@ -53,9 +53,10 @@ async function queryInlinePolicy(resourcePolicyId: number) {
|
||||
|
||||
export type GetResourceResponse = Omit<
|
||||
NonNullable<Awaited<ReturnType<typeof query>>>,
|
||||
"headers"
|
||||
"requestHeaders" | "responseHeaders"
|
||||
> & {
|
||||
headers: { name: string; value: string }[] | null;
|
||||
requestHeaders: { name: string; value: string }[] | null;
|
||||
responseHeaders: { name: string; value: string }[] | null;
|
||||
};
|
||||
|
||||
registry.registerPath({
|
||||
@@ -186,9 +187,12 @@ export async function getResource(
|
||||
return response<GetResourceResponse>(res, {
|
||||
data: {
|
||||
...returnData,
|
||||
headers: returnData.headers
|
||||
? JSON.parse(returnData.headers)
|
||||
: returnData.headers
|
||||
requestHeaders: returnData.requestHeaders
|
||||
? JSON.parse(returnData.requestHeaders)
|
||||
: returnData.requestHeaders,
|
||||
responseHeaders: returnData.responseHeaders
|
||||
? JSON.parse(returnData.responseHeaders)
|
||||
: returnData.responseHeaders
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
|
||||
@@ -100,6 +100,14 @@ const updateHttpResourceBodySchema = z
|
||||
"When no shared resource policy is assigned (resourcePolicyId is null), updates the resource's inline policy. When a shared policy is assigned, this value overrides the shared policy for this resource."
|
||||
),
|
||||
headers: z
|
||||
.array(z.strictObject({ name: z.string(), value: z.string() }))
|
||||
.nullable()
|
||||
.optional(), // deprecated alias for requestHeaders
|
||||
requestHeaders: z
|
||||
.array(z.strictObject({ name: z.string(), value: z.string() }))
|
||||
.nullable()
|
||||
.optional(),
|
||||
responseHeaders: z
|
||||
.array(z.strictObject({ name: z.string(), value: z.string() }))
|
||||
.nullable()
|
||||
.optional(),
|
||||
@@ -163,12 +171,13 @@ const updateHttpResourceBodySchema = z
|
||||
)
|
||||
.refine(
|
||||
(data) => {
|
||||
if (data.headers) {
|
||||
// HTTP header names must be valid token characters (RFC 7230)
|
||||
const validHeaderName = /^[a-zA-Z0-9!#$%&'*+\-.^_`|~]+$/;
|
||||
return data.headers.every((h) => validHeaderName.test(h.name));
|
||||
}
|
||||
return true;
|
||||
const validHeaderName = /^[a-zA-Z0-9!#$%&'*+\-.^_`|~]+$/;
|
||||
const allHeaders = [
|
||||
...(data.headers ?? []),
|
||||
...(data.requestHeaders ?? []),
|
||||
...(data.responseHeaders ?? [])
|
||||
];
|
||||
return allHeaders.every((h) => validHeaderName.test(h.name));
|
||||
},
|
||||
{
|
||||
error: "Header names may only contain valid HTTP token characters (letters, digits, and !#$%&'*+-.^_`|~)."
|
||||
@@ -176,14 +185,13 @@ const updateHttpResourceBodySchema = z
|
||||
)
|
||||
.refine(
|
||||
(data) => {
|
||||
if (data.headers) {
|
||||
// HTTP header values must be visible ASCII or horizontal whitespace, no control chars (RFC 7230)
|
||||
const validHeaderValue = /^[\t\x20-\x7E]*$/;
|
||||
return data.headers.every((h) =>
|
||||
validHeaderValue.test(h.value)
|
||||
);
|
||||
}
|
||||
return true;
|
||||
const validHeaderValue = /^[\t\x20-\x7E]*$/;
|
||||
const allHeaders = [
|
||||
...(data.headers ?? []),
|
||||
...(data.requestHeaders ?? []),
|
||||
...(data.responseHeaders ?? [])
|
||||
];
|
||||
return allHeaders.every((h) => validHeaderValue.test(h.value));
|
||||
},
|
||||
{
|
||||
error: "Header values may only contain printable ASCII characters and horizontal whitespace."
|
||||
@@ -191,16 +199,17 @@ const updateHttpResourceBodySchema = z
|
||||
)
|
||||
.refine(
|
||||
(data) => {
|
||||
if (data.headers) {
|
||||
// Reject Traefik template syntax {{word}} in names or values
|
||||
const templatePattern = /\{\{[^}]+\}\}/;
|
||||
return data.headers.every(
|
||||
(h) =>
|
||||
!templatePattern.test(h.name) &&
|
||||
!templatePattern.test(h.value)
|
||||
);
|
||||
}
|
||||
return true;
|
||||
const templatePattern = /\{\{[^}]+\}\}/;
|
||||
const allHeaders = [
|
||||
...(data.headers ?? []),
|
||||
...(data.requestHeaders ?? []),
|
||||
...(data.responseHeaders ?? [])
|
||||
];
|
||||
return allHeaders.every(
|
||||
(h) =>
|
||||
!templatePattern.test(h.name) &&
|
||||
!templatePattern.test(h.value)
|
||||
);
|
||||
},
|
||||
{
|
||||
error: "Header names and values must not contain template expressions such as {{value}}."
|
||||
@@ -500,7 +509,8 @@ async function updateHttpResource(
|
||||
}
|
||||
|
||||
// catch when the resource policy changes or gets cleared
|
||||
if (resource.resourcePolicyId != updateData.resourcePolicyId) {
|
||||
if (updateData.resourcePolicyId !== undefined &&
|
||||
resource.resourcePolicyId !== updateData.resourcePolicyId) {
|
||||
await clearResourceSpecificSettings(
|
||||
resource.resourceId,
|
||||
resource.orgId,
|
||||
@@ -695,13 +705,32 @@ async function updateHttpResource(
|
||||
await createCertificate(domainId, fullDomain, db);
|
||||
}
|
||||
|
||||
let headers = undefined;
|
||||
if (updateData.headers) {
|
||||
headers = JSON.stringify(updateData.headers);
|
||||
} else if (updateData.headers === null) {
|
||||
headers = null;
|
||||
let requestHeaders = undefined;
|
||||
const mergedRequestHeaders = [
|
||||
...(updateData.headers ?? []),
|
||||
...(updateData.requestHeaders ?? [])
|
||||
];
|
||||
if (
|
||||
updateData.headers !== undefined ||
|
||||
updateData.requestHeaders !== undefined
|
||||
) {
|
||||
requestHeaders =
|
||||
mergedRequestHeaders.length > 0
|
||||
? JSON.stringify(mergedRequestHeaders)
|
||||
: null;
|
||||
}
|
||||
|
||||
let responseHeaders = undefined;
|
||||
if (updateData.responseHeaders) {
|
||||
responseHeaders = JSON.stringify(updateData.responseHeaders);
|
||||
} else if (updateData.responseHeaders === null) {
|
||||
responseHeaders = null;
|
||||
}
|
||||
|
||||
updateData.headers = undefined;
|
||||
updateData.requestHeaders = undefined;
|
||||
updateData.responseHeaders = undefined;
|
||||
|
||||
if (!isLicensed) {
|
||||
updateData.maintenanceModeEnabled = undefined;
|
||||
updateData.maintenanceModeType = undefined;
|
||||
@@ -754,7 +783,7 @@ async function updateHttpResource(
|
||||
|
||||
const updatedResource = await db
|
||||
.update(resources)
|
||||
.set({ ...resourceOnlyData, headers })
|
||||
.set({ ...resourceOnlyData, requestHeaders, responseHeaders })
|
||||
.where(eq(resources.resourceId, resource.resourceId))
|
||||
.returning();
|
||||
|
||||
@@ -778,7 +807,7 @@ async function updateHttpResource(
|
||||
|
||||
const updatedResource = await db
|
||||
.update(resources)
|
||||
.set({ ...updateData, headers })
|
||||
.set({ ...updateData, requestHeaders, responseHeaders })
|
||||
.where(eq(resources.resourceId, resource.resourceId))
|
||||
.returning();
|
||||
|
||||
|
||||
@@ -263,7 +263,7 @@ export async function createSite(
|
||||
const { value: newClientAddress, release } =
|
||||
await getNextAvailableClientSubnet(orgId);
|
||||
releaseSubnetLock = release;
|
||||
updatedAddress = newClientAddress.split("/")[0];
|
||||
updatedAddress = `${newClientAddress.split("/")[0]}/${org.subnet ? org.subnet.split("/")[1] : "32"}`;
|
||||
}
|
||||
|
||||
let newSite: Site | undefined;
|
||||
|
||||
@@ -113,7 +113,7 @@ export async function updateSite(
|
||||
.where(
|
||||
and(
|
||||
eq(sites.niceId, updateData.niceId),
|
||||
eq(sites.orgId, sites.orgId),
|
||||
eq(sites.orgId, existingSite.orgId),
|
||||
ne(sites.siteId, siteId)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import { assertEquals } from "@test/assert";
|
||||
import { getSiteResourceParamsSchema } from "./getSiteResource";
|
||||
|
||||
function testSiteResourceIdOnlyParams() {
|
||||
const result = getSiteResourceParamsSchema.safeParse({
|
||||
siteResourceId: "42"
|
||||
});
|
||||
|
||||
assertEquals(
|
||||
result.success,
|
||||
true,
|
||||
"siteResourceId-only integration routes should pass validation"
|
||||
);
|
||||
|
||||
if (result.success) {
|
||||
assertEquals(
|
||||
result.data.siteResourceId,
|
||||
42,
|
||||
"siteResourceId should be parsed as a number"
|
||||
);
|
||||
assertEquals(
|
||||
result.data.orgId,
|
||||
undefined,
|
||||
"orgId should remain optional"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function testOrgScopedParamsRemainSupported() {
|
||||
const result = getSiteResourceParamsSchema.safeParse({
|
||||
siteResourceId: "42",
|
||||
orgId: "org-id"
|
||||
});
|
||||
|
||||
assertEquals(
|
||||
result.success,
|
||||
true,
|
||||
"org-scoped routes should continue to pass validation"
|
||||
);
|
||||
}
|
||||
|
||||
function testInvalidSiteResourceId() {
|
||||
const result = getSiteResourceParamsSchema.safeParse({
|
||||
siteResourceId: "not-a-number"
|
||||
});
|
||||
|
||||
assertEquals(
|
||||
result.success,
|
||||
false,
|
||||
"non-numeric siteResourceIds should fail validation"
|
||||
);
|
||||
}
|
||||
|
||||
testSiteResourceIdOnlyParams();
|
||||
testOrgScopedParamsRemainSupported();
|
||||
testInvalidSiteResourceId();
|
||||
|
||||
console.log("All getSiteResource parameter validation tests passed.");
|
||||
@@ -10,7 +10,7 @@ import { fromError } from "zod-validation-error";
|
||||
import logger from "@server/logger";
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
|
||||
const getSiteResourceParamsSchema = z.strictObject({
|
||||
export const getSiteResourceParamsSchema = z.strictObject({
|
||||
siteResourceId: z
|
||||
.string()
|
||||
.optional()
|
||||
@@ -22,15 +22,17 @@ const getSiteResourceParamsSchema = z.strictObject({
|
||||
});
|
||||
|
||||
async function query(siteResourceId?: number, niceId?: string, orgId?: string) {
|
||||
if (siteResourceId && orgId) {
|
||||
if (siteResourceId) {
|
||||
const [siteResource] = await db
|
||||
.select()
|
||||
.from(siteResources)
|
||||
.where(
|
||||
and(
|
||||
eq(siteResources.siteResourceId, siteResourceId),
|
||||
eq(siteResources.orgId, orgId)
|
||||
)
|
||||
orgId
|
||||
? and(
|
||||
eq(siteResources.siteResourceId, siteResourceId),
|
||||
eq(siteResources.orgId, orgId)
|
||||
)
|
||||
: eq(siteResources.siteResourceId, siteResourceId)
|
||||
)
|
||||
.limit(1);
|
||||
return siteResource;
|
||||
|
||||
@@ -0,0 +1,342 @@
|
||||
import { execFileSync } from "child_process";
|
||||
import fs from "fs";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
import Database from "better-sqlite3";
|
||||
import { assertEquals } from "@test/assert";
|
||||
|
||||
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||
const repoRoot = path.resolve(here, "..", "..");
|
||||
const migrationsScript = path.join(here, "migrationsSqlite.ts");
|
||||
|
||||
const SEED_STATEMENTS = [
|
||||
`CREATE TABLE versionMigrations (version TEXT PRIMARY KEY, executedAt INTEGER NOT NULL)`,
|
||||
`INSERT INTO versionMigrations (version, executedAt) VALUES ('1.21.0', 1750000000000)`,
|
||||
`CREATE TABLE sites (siteId INTEGER PRIMARY KEY AUTOINCREMENT, subnet TEXT)`,
|
||||
`INSERT INTO sites (subnet) VALUES ('10.0.0.0/24')`,
|
||||
`CREATE TABLE roles (roleId INTEGER PRIMARY KEY AUTOINCREMENT, orgId TEXT, isAdmin INTEGER DEFAULT 0, sshSudoMode TEXT DEFAULT 'none')`,
|
||||
`INSERT INTO roles (orgId, isAdmin, sshSudoMode) VALUES ('org1', 0, 'none')`,
|
||||
`CREATE TABLE licenseKey (licenseKeyId INTEGER PRIMARY KEY AUTOINCREMENT)`,
|
||||
`CREATE TABLE targets (targetId INTEGER PRIMARY KEY AUTOINCREMENT, resourceId INTEGER, siteId INTEGER NOT NULL, ip TEXT NOT NULL, method TEXT, port INTEGER NOT NULL, internalPort INTEGER, enabled INTEGER DEFAULT 1, path TEXT, pathMatchType TEXT, rewritePath TEXT, rewritePathType TEXT, priority INTEGER DEFAULT 100, mode TEXT DEFAULT 'http', authToken TEXT)`,
|
||||
`CREATE TABLE subscriptions (subscriptionId INTEGER PRIMARY KEY AUTOINCREMENT)`,
|
||||
`CREATE TABLE clients (clientId INTEGER PRIMARY KEY AUTOINCREMENT)`,
|
||||
`CREATE TABLE orgs (orgId TEXT PRIMARY KEY)`,
|
||||
`INSERT INTO orgs (orgId) VALUES ('org1')`,
|
||||
`CREATE TABLE siteResources (siteResourceId INTEGER PRIMARY KEY AUTOINCREMENT)`,
|
||||
`CREATE TABLE eventStreamingDestinations (destinationId INTEGER PRIMARY KEY AUTOINCREMENT)`,
|
||||
`CREATE TABLE roleActions (roleId INTEGER, actionId TEXT, orgId TEXT)`,
|
||||
`CREATE TABLE newt (newtId INTEGER PRIMARY KEY AUTOINCREMENT)`
|
||||
];
|
||||
|
||||
function seedDatabase(dbPath: string) {
|
||||
const db = new Database(dbPath);
|
||||
try {
|
||||
for (const statement of SEED_STATEMENTS) {
|
||||
db.exec(statement);
|
||||
}
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
function tableColumns(dbPath: string, tableName: string): string[] {
|
||||
const db = new Database(dbPath, { readonly: true });
|
||||
try {
|
||||
return (
|
||||
db.prepare(`PRAGMA table_info(${tableName})`).all() as Array<{
|
||||
name: unknown;
|
||||
}>
|
||||
).map((row) => String(row.name));
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
function executedMigrationVersions(dbPath: string): string[] {
|
||||
const db = new Database(dbPath, { readonly: true });
|
||||
try {
|
||||
return (
|
||||
db.prepare(`SELECT version FROM versionMigrations`).all() as Array<{
|
||||
version: unknown;
|
||||
}>
|
||||
).map((row) => String(row.version));
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
function runMigrations(
|
||||
workdir: string,
|
||||
env: Record<string, string> = {}
|
||||
): {
|
||||
exitCode: number;
|
||||
output: string;
|
||||
} {
|
||||
const tsconfig = ["tsconfig.json", "tsconfig.oss.json"]
|
||||
.map((file) => path.join(repoRoot, file))
|
||||
.find((file) => fs.existsSync(file));
|
||||
if (!tsconfig) {
|
||||
throw new Error("No tsconfig found for @server path aliases");
|
||||
}
|
||||
const tsxCli = path.join(
|
||||
repoRoot,
|
||||
"node_modules",
|
||||
"tsx",
|
||||
"dist",
|
||||
"cli.mjs"
|
||||
);
|
||||
if (!fs.existsSync(tsxCli)) {
|
||||
throw new Error("tsx is not installed; run npm ci first");
|
||||
}
|
||||
try {
|
||||
const output = execFileSync(
|
||||
process.execPath,
|
||||
[tsxCli, "--tsconfig", tsconfig, migrationsScript],
|
||||
{
|
||||
cwd: workdir,
|
||||
timeout: 120000,
|
||||
encoding: "utf8",
|
||||
env: { ...process.env, NODE_ENV: "test", ...env }
|
||||
}
|
||||
);
|
||||
return { exitCode: 0, output };
|
||||
} catch (error) {
|
||||
const output =
|
||||
error instanceof Error
|
||||
? (error as Error & { stdout?: unknown }).stdout
|
||||
: "";
|
||||
return { exitCode: 1, output: String(output ?? "") };
|
||||
}
|
||||
}
|
||||
|
||||
function createTestEnvironment(): string {
|
||||
for (const generated of ["server/build.ts", "server/db/index.ts"]) {
|
||||
if (!fs.existsSync(path.join(repoRoot, generated))) {
|
||||
throw new Error(
|
||||
`Missing ${generated}; run npm run set:oss && npm run set:sqlite first`
|
||||
);
|
||||
}
|
||||
}
|
||||
const workdir = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), "pangolin-backup-test-")
|
||||
);
|
||||
fs.mkdirSync(path.join(workdir, "config", "db"), { recursive: true });
|
||||
fs.copyFileSync(
|
||||
path.join(repoRoot, "config", "config.example.yml"),
|
||||
path.join(workdir, "config", "config.yml")
|
||||
);
|
||||
const traefikSrc = path.join(repoRoot, "config", "traefik");
|
||||
if (fs.existsSync(traefikSrc)) {
|
||||
fs.cpSync(traefikSrc, path.join(workdir, "config", "traefik"), {
|
||||
recursive: true
|
||||
});
|
||||
}
|
||||
fs.symlinkSync(
|
||||
path.join(repoRoot, "server"),
|
||||
path.join(workdir, "server"),
|
||||
process.platform === "win32" ? "junction" : "dir"
|
||||
);
|
||||
return workdir;
|
||||
}
|
||||
|
||||
function testMultipleSequentialMigrations() {
|
||||
console.log("Running multiple sequential migrations test...");
|
||||
const workdir = createTestEnvironment();
|
||||
try {
|
||||
seedDatabase(path.join(workdir, "config", "db", "db.sqlite"));
|
||||
const result = runMigrations(workdir);
|
||||
assertEquals(result.exitCode, 0, "Seeded migrations must run cleanly");
|
||||
if (!result.output.includes("All migrations completed successfully")) {
|
||||
throw new Error(
|
||||
"Seeded migrations did not complete; the backup assertions below would be vacuous"
|
||||
);
|
||||
}
|
||||
|
||||
const backupsDir = path.join(workdir, "config", "db", "backups");
|
||||
const backups = fs.existsSync(backupsDir)
|
||||
? fs
|
||||
.readdirSync(backupsDir)
|
||||
.filter((file) => file.endsWith(".sqlite"))
|
||||
: [];
|
||||
|
||||
// Upgrading from 1.21.0 runs 1.22.0 and 1.23.0 -> produces 2 distinct backups
|
||||
assertEquals(
|
||||
backups.length,
|
||||
2,
|
||||
"Each migration must have its own distinct backup snapshot"
|
||||
);
|
||||
|
||||
const v122Backup = backups.find((file) =>
|
||||
file.includes("_v1.22.0.sqlite")
|
||||
);
|
||||
const v123Backup = backups.find((file) =>
|
||||
file.includes("_v1.23.0.sqlite")
|
||||
);
|
||||
|
||||
if (!v122Backup || !v123Backup) {
|
||||
throw new Error(
|
||||
`Expected backups for v1.22.0 and v1.23.0, found: ${backups.join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
// Verify pre-1.22.0 snapshot state: sites has 'subnet' (not exitNodeSubnet), versions = [1.21.0]
|
||||
const v122Columns = tableColumns(
|
||||
path.join(backupsDir, v122Backup),
|
||||
"sites"
|
||||
);
|
||||
assertEquals(
|
||||
v122Columns.includes("subnet") &&
|
||||
!v122Columns.includes("exitNodeSubnet"),
|
||||
true,
|
||||
"Backup before 1.22.0 must retain pre-1.22.0 schema (sites.subnet)"
|
||||
);
|
||||
const v122Versions = executedMigrationVersions(
|
||||
path.join(backupsDir, v122Backup)
|
||||
);
|
||||
assertEquals(
|
||||
v122Versions.includes("1.21.0") && !v122Versions.includes("1.22.0"),
|
||||
true,
|
||||
"Backup before 1.22.0 must only record version 1.21.0"
|
||||
);
|
||||
|
||||
// Verify pre-1.23.0 snapshot state: sites has 'exitNodeSubnet' (1.22.0 applied), newt has no agent
|
||||
const v123Columns = tableColumns(
|
||||
path.join(backupsDir, v123Backup),
|
||||
"sites"
|
||||
);
|
||||
assertEquals(
|
||||
v123Columns.includes("exitNodeSubnet"),
|
||||
true,
|
||||
"Backup before 1.23.0 must contain successfully applied 1.22.0 schema (sites.exitNodeSubnet)"
|
||||
);
|
||||
const v123NewtCols = tableColumns(
|
||||
path.join(backupsDir, v123Backup),
|
||||
"newt"
|
||||
);
|
||||
assertEquals(
|
||||
!v123NewtCols.includes("agent"),
|
||||
true,
|
||||
"Backup before 1.23.0 must not contain 1.23.0 schema changes yet"
|
||||
);
|
||||
const v123Versions = executedMigrationVersions(
|
||||
path.join(backupsDir, v123Backup)
|
||||
);
|
||||
assertEquals(
|
||||
v123Versions.includes("1.21.0") && v123Versions.includes("1.22.0"),
|
||||
true,
|
||||
"Backup before 1.23.0 must record both 1.21.0 and 1.22.0"
|
||||
);
|
||||
} finally {
|
||||
fs.rmSync(workdir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function testFailureInLaterMigrationPreservesRestorePoints() {
|
||||
console.log("Running failure in later migration test...");
|
||||
const workdir = createTestEnvironment();
|
||||
try {
|
||||
const dbPath = path.join(workdir, "config", "db", "db.sqlite");
|
||||
seedDatabase(dbPath);
|
||||
|
||||
// Intentionally drop table 'newt' so migration 1.23.0 fails on ALTER TABLE newt ADD COLUMN agent
|
||||
const db = new Database(dbPath);
|
||||
db.exec("DROP TABLE newt;");
|
||||
db.close();
|
||||
|
||||
const result = runMigrations(workdir);
|
||||
assertEquals(
|
||||
result.exitCode,
|
||||
1,
|
||||
"Migration suite must fail when 1.23.0 errors"
|
||||
);
|
||||
|
||||
const backupsDir = path.join(workdir, "config", "db", "backups");
|
||||
const backups = fs.existsSync(backupsDir)
|
||||
? fs
|
||||
.readdirSync(backupsDir)
|
||||
.filter((file) => file.endsWith(".sqlite"))
|
||||
: [];
|
||||
|
||||
// Both pre-1.22.0 and pre-1.23.0 backups must exist
|
||||
assertEquals(
|
||||
backups.length,
|
||||
2,
|
||||
"Backups for earlier successful migration and the failed migration must both exist"
|
||||
);
|
||||
|
||||
const v122Backup = backups.find((file) =>
|
||||
file.includes("_v1.22.0.sqlite")
|
||||
);
|
||||
const v123Backup = backups.find((file) =>
|
||||
file.includes("_v1.23.0.sqlite")
|
||||
);
|
||||
|
||||
if (!v122Backup || !v123Backup) {
|
||||
throw new Error(
|
||||
`Expected restore points for v1.22.0 and v1.23.0, found: ${backups.join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
// Verify pre-1.23.0 backup is a valid restore point with 1.22.0 changes applied
|
||||
const v123SitesCols = tableColumns(
|
||||
path.join(backupsDir, v123Backup),
|
||||
"sites"
|
||||
);
|
||||
assertEquals(
|
||||
v123SitesCols.includes("exitNodeSubnet"),
|
||||
true,
|
||||
"Pre-failure restore point must have 1.22.0 changes intact"
|
||||
);
|
||||
const v123Versions = executedMigrationVersions(
|
||||
path.join(backupsDir, v123Backup)
|
||||
);
|
||||
assertEquals(
|
||||
v123Versions.includes("1.22.0"),
|
||||
true,
|
||||
"Pre-failure restore point must record successful 1.22.0 migration"
|
||||
);
|
||||
} finally {
|
||||
fs.rmSync(workdir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function testDisableBackupOnMigration() {
|
||||
console.log("Running DISABLE_BACKUP_ON_MIGRATION test...");
|
||||
const workdir = createTestEnvironment();
|
||||
try {
|
||||
seedDatabase(path.join(workdir, "config", "db", "db.sqlite"));
|
||||
const result = runMigrations(workdir, {
|
||||
DISABLE_BACKUP_ON_MIGRATION: "1"
|
||||
});
|
||||
assertEquals(
|
||||
result.exitCode,
|
||||
0,
|
||||
"Migrations must succeed with backups disabled"
|
||||
);
|
||||
|
||||
const backupsDir = path.join(workdir, "config", "db", "backups");
|
||||
const backups = fs.existsSync(backupsDir)
|
||||
? fs
|
||||
.readdirSync(backupsDir)
|
||||
.filter((file) => file.endsWith(".sqlite"))
|
||||
: [];
|
||||
assertEquals(
|
||||
backups.length,
|
||||
0,
|
||||
"No backup files should be created when DISABLE_BACKUP_ON_MIGRATION is set"
|
||||
);
|
||||
} finally {
|
||||
fs.rmSync(workdir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
testMultipleSequentialMigrations();
|
||||
testFailureInLaterMigrationPreservesRestorePoints();
|
||||
testDisableBackupOnMigration();
|
||||
console.log("All backup migration regression tests passed successfully!");
|
||||
} catch (error) {
|
||||
console.error("Test failed:", error);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import path from "path";
|
||||
import semver from "semver";
|
||||
import { versionMigrations } from "../db/sqlite";
|
||||
import { __DIRNAME, APP_PATH, APP_VERSION } from "@server/lib/consts";
|
||||
import { formatBackupTimestamp } from "@server/lib/backupFileName";
|
||||
import { formatBackupFileName } from "@server/lib/backupFileName";
|
||||
import { SqliteError } from "better-sqlite3";
|
||||
import fs from "fs";
|
||||
import { build } from "@server/build";
|
||||
@@ -107,7 +107,7 @@ async function run() {
|
||||
await runMigrations();
|
||||
}
|
||||
|
||||
function backupDb() {
|
||||
function backupDb(version?: string) {
|
||||
// make dir config/db/backups
|
||||
const appPath = APP_PATH;
|
||||
const dbDir = path.join(appPath, "db");
|
||||
@@ -120,11 +120,10 @@ function backupDb() {
|
||||
}
|
||||
|
||||
// copy the db.sqlite file to backups
|
||||
// add the date to the filename
|
||||
const date = new Date();
|
||||
const dateString = formatBackupTimestamp(date);
|
||||
// add the date and migration version to the filename
|
||||
const fileName = formatBackupFileName(version);
|
||||
const dbPath = path.join(dbDir, "db.sqlite");
|
||||
const backupPath = path.join(backupsDir, `db_${dateString}.sqlite`);
|
||||
const backupPath = path.join(backupsDir, fileName);
|
||||
fs.copyFileSync(dbPath, backupPath);
|
||||
}
|
||||
|
||||
@@ -163,6 +162,12 @@ export async function runMigrations() {
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error running migrations:", e);
|
||||
if (
|
||||
process.env.NODE_ENV === "test" ||
|
||||
process.env.ENVIRONMENT === "test"
|
||||
) {
|
||||
throw e;
|
||||
}
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, 1000 * 60 * 60 * 24 * 1)
|
||||
);
|
||||
@@ -197,7 +202,7 @@ async function executeScripts() {
|
||||
try {
|
||||
if (!process.env.DISABLE_BACKUP_ON_MIGRATION) {
|
||||
// Backup the database before running the migration
|
||||
backupDb();
|
||||
backupDb(migration.version);
|
||||
}
|
||||
|
||||
await migration.run();
|
||||
|
||||
@@ -10,7 +10,6 @@ import {
|
||||
users
|
||||
} from "../../db/sqlite";
|
||||
import { APP_PATH, configFilePath1, configFilePath2 } from "@server/lib/consts";
|
||||
import { formatBackupTimestamp } from "@server/lib/backupFileName";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import fs from "fs";
|
||||
import * as yaml from "js-yaml";
|
||||
@@ -21,25 +20,6 @@ import { fromZodError } from "zod-validation-error";
|
||||
export default async function migration() {
|
||||
console.log("Running setup script 1.0.0-beta.9...");
|
||||
|
||||
// make dir config/db/backups
|
||||
const appPath = APP_PATH;
|
||||
const dbDir = path.join(appPath, "db");
|
||||
|
||||
const backupsDir = path.join(dbDir, "backups");
|
||||
|
||||
// check if the backups directory exists and create it if it doesn't
|
||||
if (!fs.existsSync(backupsDir)) {
|
||||
fs.mkdirSync(backupsDir, { recursive: true });
|
||||
}
|
||||
|
||||
// copy the db.sqlite file to backups
|
||||
// add the date to the filename
|
||||
const date = new Date();
|
||||
const dateString = formatBackupTimestamp(date);
|
||||
const dbPath = path.join(dbDir, "db.sqlite");
|
||||
const backupPath = path.join(backupsDir, `db_${dateString}.sqlite`);
|
||||
fs.copyFileSync(dbPath, backupPath);
|
||||
|
||||
await db.transaction(async (trx) => {
|
||||
try {
|
||||
// Determine which config file exists
|
||||
|
||||
Reference in New Issue
Block a user