Compare commits

..

3 Commits

Author SHA1 Message Date
copilot-swe-agent[bot]
baf45c47eb Tidy SSH defaults formatting 2026-06-16 23:56:01 +00:00
copilot-swe-agent[bot]
76d0b5753a Restrict public browser gateway defaults to newt targets 2026-06-16 23:53:23 +00:00
Owen
f9cc52ece9 Remove NoNewPrivileges
Fixes https://github.com/fosrl/newt/issues/383
2026-06-14 15:02:18 -07:00
9 changed files with 84 additions and 122 deletions

View File

@@ -154,8 +154,12 @@ export async function createResourceRule(
}
// Create the new resource rule
if (resource.resourcePolicyId !== null) {
const policyId = resource.resourcePolicyId;
const isInlinePolicy =
resource.resourcePolicyId === null &&
resource.defaultResourcePolicyId !== null;
if (isInlinePolicy) {
const policyId = resource.defaultResourcePolicyId!;
const [newRule] = await db
.insert(resourcePolicyRules)
.values({

View File

@@ -2,7 +2,7 @@ import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
import { resourceRules, resourcePolicyRules, resources } from "@server/db";
import { and, eq } from "drizzle-orm";
import { eq } from "drizzle-orm";
import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
@@ -73,18 +73,14 @@ export async function deleteResourceRule(
);
}
if (resource.resourcePolicyId !== null) {
const isInlinePolicy =
resource.resourcePolicyId === null &&
resource.defaultResourcePolicyId !== null;
if (isInlinePolicy) {
const [deletedRule] = await db
.delete(resourcePolicyRules)
.where(
and(
eq(resourcePolicyRules.ruleId, ruleId),
eq(
resourcePolicyRules.resourcePolicyId,
resource.resourcePolicyId
)
)
)
.where(eq(resourcePolicyRules.ruleId, ruleId))
.returning();
if (!deletedRule) {

View File

@@ -141,10 +141,16 @@ export async function getResource(
);
}
const isInlinePolicy =
resource.resourcePolicyId === null &&
resource.defaultResourcePolicyId !== null;
let returnData = resource;
if (resource.resourcePolicyId !== null) {
if (isInlinePolicy) {
// get the policy
const policy = await queryInlinePolicy(resource.resourcePolicyId);
const policy = await queryInlinePolicy(
resource.defaultResourcePolicyId!
);
returnData = {
...returnData,
sso: policy?.sso || null,

View File

@@ -140,11 +140,15 @@ export async function listResourceRules(
);
}
const isInlinePolicy =
resource.resourcePolicyId === null &&
resource.defaultResourcePolicyId !== null;
let rulesList: Awaited<ReturnType<typeof queryResourceRules>>;
let totalCount: number;
if (resource.resourcePolicyId !== null) {
const policyId = resource.resourcePolicyId;
if (isInlinePolicy) {
const policyId = resource.defaultResourcePolicyId!;
const policyRules = await queryPolicyRules(policyId)
.limit(limit)
.offset(offset);

View File

@@ -1,8 +1,8 @@
import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
import { resourcePolicyRules, resourceRules, resources } from "@server/db";
import { and, eq } from "drizzle-orm";
import { resourceRules, resources } from "@server/db";
import { eq } from "drizzle-orm";
import response from "@server/lib/response";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
@@ -37,29 +37,6 @@ const updateResourceRuleSchema = z
error: "At least one field must be provided for update"
});
function getRuleValueValidationError(
match: "CIDR" | "IP" | "PATH" | "COUNTRY" | "ASN" | "REGION",
value: string
): string | null {
if (match === "CIDR" && !isValidCIDR(value)) {
return "Invalid CIDR provided";
}
if (match === "IP" && !isValidIP(value)) {
return "Invalid IP provided";
}
if (match === "PATH" && !isValidUrlGlobPattern(value)) {
return "Invalid URL glob pattern provided";
}
if (match === "REGION" && !isValidRegionId(value)) {
return "Invalid region ID provided";
}
return null;
}
registry.registerPath({
method: "post",
path: "/resource/{resourceId}/rule/{ruleId}",
@@ -151,68 +128,6 @@ export async function updateResourceRule(
);
}
if (resource.resourcePolicyId !== null) {
const [existingRule] = await db
.select()
.from(resourcePolicyRules)
.where(
and(
eq(resourcePolicyRules.ruleId, ruleId),
eq(
resourcePolicyRules.resourcePolicyId,
resource.resourcePolicyId
)
)
)
.limit(1);
if (!existingRule) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
`Resource rule with ID ${ruleId} not found`
)
);
}
const match = updateData.match || existingRule.match;
const { value } = updateData;
if (value !== undefined) {
const validationError = getRuleValueValidationError(
match,
value
);
if (validationError) {
return next(
createHttpError(HttpCode.BAD_REQUEST, validationError)
);
}
}
const [updatedRule] = await db
.update(resourcePolicyRules)
.set(updateData)
.where(
and(
eq(resourcePolicyRules.ruleId, ruleId),
eq(
resourcePolicyRules.resourcePolicyId,
resource.resourcePolicyId
)
)
)
.returning();
return response(res, {
data: updatedRule,
success: true,
error: false,
message: "Resource rule updated successfully",
status: HttpCode.OK
});
}
// Verify that the rule exists and belongs to the specified resource
const [existingRule] = await db
.select()
@@ -242,11 +157,42 @@ export async function updateResourceRule(
const { value } = updateData;
if (value !== undefined) {
const validationError = getRuleValueValidationError(match, value);
if (validationError) {
return next(
createHttpError(HttpCode.BAD_REQUEST, validationError)
);
if (match === "CIDR") {
if (!isValidCIDR(value)) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Invalid CIDR provided"
)
);
}
} else if (match === "IP") {
if (!isValidIP(value)) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Invalid IP provided"
)
);
}
} else if (match === "PATH") {
if (!isValidUrlGlobPattern(value)) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Invalid URL glob pattern provided"
)
);
}
} else if (match === "REGION") {
if (!isValidRegionId(value)) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Invalid region ID provided"
)
);
}
}
}

View File

@@ -40,6 +40,7 @@ type TargetRow = {
targetId: number;
resourceId: number;
siteId: number;
siteType: string | null;
siteName?: string;
mode: string | null;
ip: string;
@@ -105,7 +106,8 @@ function RdpServerForm({
const api = createApiClient(useEnvContext());
const router = useRouter();
const targets = targetsResponse.targets.filter((t) => t.mode === "rdp");
const firstTarget = targets[0];
const browserGatewayTargets = targets.filter((t) => t.siteType === "newt");
const firstTarget = browserGatewayTargets[0];
const formSchema = useMemo(
() => createBrowserGatewayTargetFormSchema(t),
@@ -115,7 +117,7 @@ function RdpServerForm({
const form = useForm<BrowserGatewayTargetFormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
selectedSites: targets.map((target) => ({
selectedSites: browserGatewayTargets.map((target) => ({
siteId: target.siteId,
name: target.siteName ?? String(target.siteId),
type: "newt" as const

View File

@@ -62,6 +62,7 @@ type TargetRow = {
targetId: number;
resourceId: number;
siteId: number;
siteType: string | null;
siteName?: string;
mode: string | null;
ip: string;
@@ -130,7 +131,9 @@ function SshServerForm({
const isNativeInitially = resource.authDaemonMode === "native";
const targets = targetsResponse.targets.filter((t) => t.mode === "ssh");
const browserGatewayTargets = targets.filter((t) => t.siteType === "newt");
const firstTarget = targets[0];
const firstBrowserGatewayTarget = browserGatewayTargets[0];
const initialPamMode =
(resource.pamMode as "passthrough" | "push") || "passthrough";
const initialStandardDaemonLocation = isNativeInitially
@@ -163,18 +166,18 @@ function SshServerForm({
selectedSites:
isNativeInitially || useSingleSiteOnLoad
? []
: targets.map((target) => ({
: browserGatewayTargets.map((target) => ({
siteId: target.siteId,
name: target.siteName ?? String(target.siteId),
type: "newt" as const
})),
selectedSite:
useSingleSiteOnLoad && firstTarget
useSingleSiteOnLoad && firstBrowserGatewayTarget
? {
siteId: firstTarget.siteId,
siteId: firstBrowserGatewayTarget.siteId,
name:
firstTarget.siteName ??
String(firstTarget.siteId),
firstBrowserGatewayTarget.siteName ??
String(firstBrowserGatewayTarget.siteId),
type: "newt" as const
}
: null,
@@ -190,11 +193,11 @@ function SshServerForm({
: null,
destination: isNativeInitially
? ""
: (firstTarget?.ip ?? ""),
: (firstBrowserGatewayTarget?.ip ?? ""),
destinationPort: isNativeInitially
? "22"
: firstTarget
? String(firstTarget.port)
: firstBrowserGatewayTarget
? String(firstBrowserGatewayTarget.port)
: "22"
}
});

View File

@@ -40,6 +40,7 @@ type TargetRow = {
targetId: number;
resourceId: number;
siteId: number;
siteType: string | null;
siteName?: string;
mode: string | null;
ip: string;
@@ -105,7 +106,8 @@ function VncServerForm({
const api = createApiClient(useEnvContext());
const router = useRouter();
const targets = targetsResponse.targets.filter((t) => t.mode === "vnc");
const firstTarget = targets[0];
const browserGatewayTargets = targets.filter((t) => t.siteType === "newt");
const firstTarget = browserGatewayTargets[0];
const formSchema = useMemo(
() => createBrowserGatewayTargetFormSchema(t),
@@ -115,7 +117,7 @@ function VncServerForm({
const form = useForm<BrowserGatewayTargetFormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
selectedSites: targets.map((target) => ({
selectedSites: browserGatewayTargets.map((target) => ({
siteId: target.siteId,
name: target.siteName ?? String(target.siteId),
type: "newt" as const

View File

@@ -139,7 +139,6 @@ Restart=always
RestartSec=2
UMask=0077
NoNewPrivileges=true
PrivateTmp=true
[Install]