mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
Improve requested user/host validation for ssh certificate template
This commit is contained in:
@@ -130,7 +130,7 @@ export const sshCertificateTemplateServiceFactory = ({
|
||||
|
||||
if (name) {
|
||||
const existingTemplate = await sshCertificateTemplateDAL.getByName(name, actorOrgId);
|
||||
if (existingTemplate) {
|
||||
if (existingTemplate && existingTemplate.id !== id) {
|
||||
throw new BadRequestError({
|
||||
message: `SSH certificate template with name ${name} already exists`
|
||||
});
|
||||
|
||||
@@ -122,34 +122,70 @@ export const validateSshCertificatePrincipals = (
|
||||
) => {
|
||||
switch (certType) {
|
||||
case SshCertType.USER: {
|
||||
const allowsAllUsers = template.allowedUsers?.includes("*") ?? false;
|
||||
return principals.every((principal) => {
|
||||
if (principal === "*") return false;
|
||||
if (allowsAllUsers) return isValidUserPattern(principal);
|
||||
return template.allowedUsers?.includes(principal);
|
||||
if (template.allowedUsers.length === 0) {
|
||||
throw new BadRequestError({
|
||||
message: "No allowed users are configured in the SSH certificate template."
|
||||
});
|
||||
}
|
||||
|
||||
const allowsAllUsers = template.allowedUsers.includes("*") ?? false;
|
||||
|
||||
principals.forEach((principal) => {
|
||||
if (principal === "*") {
|
||||
throw new BadRequestError({
|
||||
message: `Principal '*' is not allowed for user certificates.`
|
||||
});
|
||||
}
|
||||
if (allowsAllUsers && !isValidUserPattern(principal)) {
|
||||
throw new BadRequestError({
|
||||
message: `Principal '${principal}' does not match a valid user pattern.`
|
||||
});
|
||||
}
|
||||
if (!allowsAllUsers && !template.allowedUsers.includes(principal)) {
|
||||
throw new BadRequestError({
|
||||
message: `Principal '${principal}' is not in the list of allowed users.`
|
||||
});
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
case SshCertType.HOST: {
|
||||
const allowsAllHosts = template.allowedHosts?.includes("*") ?? false;
|
||||
return principals.every((principal) => {
|
||||
if (principal.includes("*")) return false;
|
||||
if (allowsAllHosts) return isValidHostPattern(principal);
|
||||
if (template.allowedHosts.length === 0) {
|
||||
throw new BadRequestError({
|
||||
message: "No allowed hosts are configured in the SSH certificate template."
|
||||
});
|
||||
}
|
||||
|
||||
// Validate against allowed domains
|
||||
return (
|
||||
isValidHostPattern(principal) &&
|
||||
template.allowedHosts?.some((allowedHost) => {
|
||||
const allowsAllHosts = template.allowedHosts.includes("*") ?? false;
|
||||
|
||||
principals.forEach((principal) => {
|
||||
if (principal.includes("*")) {
|
||||
throw new BadRequestError({
|
||||
message: `Principal '${principal}' with wildcards is not allowed for host certificates.`
|
||||
});
|
||||
}
|
||||
if (allowsAllHosts && !isValidHostPattern(principal)) {
|
||||
throw new BadRequestError({
|
||||
message: `Principal '${principal}' does not match a valid host pattern.`
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
!allowsAllHosts &&
|
||||
!template.allowedHosts.some((allowedHost) => {
|
||||
if (allowedHost.startsWith("*.")) {
|
||||
// Match subdomains of a wildcard domain
|
||||
const baseDomain = allowedHost.slice(2); // Remove the leading "*."
|
||||
return principal.endsWith(`.${baseDomain}`);
|
||||
}
|
||||
|
||||
// Exact match for non-wildcard domains
|
||||
return principal === allowedHost;
|
||||
})
|
||||
);
|
||||
) {
|
||||
throw new BadRequestError({
|
||||
message: `Principal '${principal}' is not in the list of allowed hosts or domains.`
|
||||
});
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new BadRequestError({
|
||||
|
||||
Reference in New Issue
Block a user