mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
New approach to validate jws
This commit is contained in:
11
backend/src/@types/knex.d.ts
vendored
11
backend/src/@types/knex.d.ts
vendored
@@ -722,6 +722,17 @@ declare module "knex/types/tables" {
|
|||||||
TPkiAcmeAccountsInsert,
|
TPkiAcmeAccountsInsert,
|
||||||
TPkiAcmeAccountsUpdate
|
TPkiAcmeAccountsUpdate
|
||||||
>;
|
>;
|
||||||
|
[TableName.PkiAcmeOrder]: KnexOriginal.CompositeTableType<
|
||||||
|
TPkiAcmeOrders,
|
||||||
|
TPkiAcmeOrdersInsert,
|
||||||
|
TPkiAcmeOrdersUpdate
|
||||||
|
>;
|
||||||
|
[TableName.PkiAcmeAuth]: KnexOriginal.CompositeTableType<TPkiAcmeAuths, TPkiAcmeAuthsInsert, TPkiAcmeAuthsUpdate>;
|
||||||
|
[TableName.PkiAcmeChallenge]: KnexOriginal.CompositeTableType<
|
||||||
|
TPkiAcmeChallenges,
|
||||||
|
TPkiAcmeChallengesInsert,
|
||||||
|
TPkiAcmeChallengesUpdate
|
||||||
|
>;
|
||||||
[TableName.CertificateTemplateEstConfig]: KnexOriginal.CompositeTableType<
|
[TableName.CertificateTemplateEstConfig]: KnexOriginal.CompositeTableType<
|
||||||
TCertificateTemplateEstConfigs,
|
TCertificateTemplateEstConfigs,
|
||||||
TCertificateTemplateEstConfigsInsert,
|
TCertificateTemplateEstConfigsInsert,
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ export async function up(knex: Knex): Promise<void> {
|
|||||||
// Multi-value emails array
|
// Multi-value emails array
|
||||||
t.specificType("emails", "text[]").notNullable();
|
t.specificType("emails", "text[]").notNullable();
|
||||||
|
|
||||||
|
// TODO: make public key a string instead of jsonb to make indexing much easier
|
||||||
// Public key (JWK format)
|
// Public key (JWK format)
|
||||||
t.jsonb("publicKey").notNullable();
|
t.jsonb("publicKey").notNullable();
|
||||||
// The JWS algorithm used to sign the public key when creating the account, e.g. "RS256", "ES256", "PS256", etc.
|
// The JWS algorithm used to sign the public key when creating the account, e.g. "RS256", "ES256", "PS256", etc.
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
CreateAcmeAccountBodySchema,
|
||||||
CreateAcmeAccountResponseSchema,
|
CreateAcmeAccountResponseSchema,
|
||||||
CreateAcmeOrderResponseSchema,
|
CreateAcmeOrderResponseSchema,
|
||||||
CreateAcmeOrderSchema,
|
CreateAcmeOrderSchema,
|
||||||
@@ -27,6 +28,7 @@ import { TCreateAcmeAccountPayload, TRawJwsPayload } from "@app/ee/services/pki-
|
|||||||
import { ApiDocsTags } from "@app/lib/api-docs";
|
import { ApiDocsTags } from "@app/lib/api-docs";
|
||||||
import { getConfig } from "@app/lib/config/env";
|
import { getConfig } from "@app/lib/config/env";
|
||||||
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
|
import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
|
||||||
|
import { AcmeBadPublicKeyError } from "@app/ee/services/pki-acme/pki-acme-errors";
|
||||||
|
|
||||||
export const registerPkiAcmeRouter = async (server: FastifyZodProvider) => {
|
export const registerPkiAcmeRouter = async (server: FastifyZodProvider) => {
|
||||||
const appCfg = getConfig();
|
const appCfg = getConfig();
|
||||||
@@ -111,13 +113,24 @@ export const registerPkiAcmeRouter = async (server: FastifyZodProvider) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
handler: async (req, res) => {
|
handler: async (req, res) => {
|
||||||
const { payload, jwk } = await server.services.pkiAcme.validateCreateAcmeAccountJwsPayload(
|
const { payload, protectedHeader, jwk } = await server.services.pkiAcme.validateJwsPayload(
|
||||||
req.body as TRawJwsPayload
|
req.body as TRawJwsPayload,
|
||||||
|
async (protectedHeader) => {
|
||||||
|
if (!protectedHeader.jwk) {
|
||||||
|
throw new AcmeBadPublicKeyError({ detail: "JWK is required in the protected header" });
|
||||||
|
}
|
||||||
|
return protectedHeader.jwk as unknown as JsonWebKey;
|
||||||
|
},
|
||||||
|
CreateAcmeAccountBodySchema
|
||||||
);
|
);
|
||||||
|
if (!jwk) {
|
||||||
|
throw new AcmeBadPublicKeyError({ detail: "JWK is required in the protected header" });
|
||||||
|
}
|
||||||
const { status, body, headers } = await server.services.pkiAcme.createAcmeAccount(
|
const { status, body, headers } = await server.services.pkiAcme.createAcmeAccount(
|
||||||
req.params.profileId,
|
req.params.profileId,
|
||||||
|
protectedHeader.alg,
|
||||||
jwk,
|
jwk,
|
||||||
payload as TCreateAcmeAccountPayload
|
payload
|
||||||
);
|
);
|
||||||
// TODO: DRY
|
// TODO: DRY
|
||||||
res.code(status);
|
res.code(status);
|
||||||
|
|||||||
@@ -20,11 +20,6 @@ export const RawJwsPayloadSchema = z.object({
|
|||||||
signature: z.string()
|
signature: z.string()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const JwsPayloadSchema = z.object({
|
|
||||||
protectedHeader: ProtectedHeaderSchema,
|
|
||||||
payload: z.unknown()
|
|
||||||
});
|
|
||||||
|
|
||||||
// Directory endpoint
|
// Directory endpoint
|
||||||
export const GetAcmeDirectorySchema = z.object({
|
export const GetAcmeDirectorySchema = z.object({
|
||||||
params: z.object({
|
params: z.object({
|
||||||
|
|||||||
@@ -81,7 +81,8 @@ export const pkiAcmeServiceFactory = ({
|
|||||||
if (protectedHeader === undefined) {
|
if (protectedHeader === undefined) {
|
||||||
throw new AcmeMalformedError({ detail: "Protected header is required" });
|
throw new AcmeMalformedError({ detail: "Protected header is required" });
|
||||||
}
|
}
|
||||||
ProtectedHeaderSchema.parse(protectedHeader);
|
const parsedHeader = ProtectedHeaderSchema.parse(protectedHeader);
|
||||||
|
// TODO: consume the nonce here
|
||||||
const jwk = await getJWK(protectedHeader);
|
const jwk = await getJWK(protectedHeader);
|
||||||
return await importJWK(jwk, protectedHeader.alg);
|
return await importJWK(jwk, protectedHeader.alg);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
|
import { JWSHeaderParameters } from "jose";
|
||||||
import {
|
import {
|
||||||
CreateAcmeAccountBodySchema,
|
CreateAcmeAccountBodySchema,
|
||||||
CreateAcmeAccountResponseSchema,
|
CreateAcmeAccountResponseSchema,
|
||||||
@@ -12,7 +13,6 @@ import {
|
|||||||
GetAcmeAuthorizationResponseSchema,
|
GetAcmeAuthorizationResponseSchema,
|
||||||
GetAcmeDirectoryResponseSchema,
|
GetAcmeDirectoryResponseSchema,
|
||||||
GetAcmeOrderResponseSchema,
|
GetAcmeOrderResponseSchema,
|
||||||
JwsPayloadSchema,
|
|
||||||
ListAcmeOrdersResponseSchema,
|
ListAcmeOrdersResponseSchema,
|
||||||
ProtectedHeaderSchema,
|
ProtectedHeaderSchema,
|
||||||
RawJwsPayloadSchema,
|
RawJwsPayloadSchema,
|
||||||
@@ -32,16 +32,16 @@ export type TRespondToAcmeChallengeResponse = z.infer<typeof RespondToAcmeChalle
|
|||||||
|
|
||||||
// Payload types
|
// Payload types
|
||||||
export type TRawJwsPayload = z.infer<typeof RawJwsPayloadSchema>;
|
export type TRawJwsPayload = z.infer<typeof RawJwsPayloadSchema>;
|
||||||
export type TJwsPayload = z.infer<typeof JwsPayloadSchema>;
|
|
||||||
export type TProtectedHeader = z.infer<typeof ProtectedHeaderSchema>;
|
export type TProtectedHeader = z.infer<typeof ProtectedHeaderSchema>;
|
||||||
export type TCreateAcmeAccountPayload = z.infer<typeof CreateAcmeAccountBodySchema>;
|
export type TCreateAcmeAccountPayload = z.infer<typeof CreateAcmeAccountBodySchema>;
|
||||||
export type TCreateAcmeOrderPayload = z.infer<typeof CreateAcmeOrderBodySchema>;
|
export type TCreateAcmeOrderPayload = z.infer<typeof CreateAcmeOrderBodySchema>;
|
||||||
export type TDeactivateAcmeAccountPayload = z.infer<typeof DeactivateAcmeAccountBodySchema>;
|
export type TDeactivateAcmeAccountPayload = z.infer<typeof DeactivateAcmeAccountBodySchema>;
|
||||||
export type TFinalizeAcmeOrderPayload = z.infer<typeof FinalizeAcmeOrderBodySchema>;
|
export type TFinalizeAcmeOrderPayload = z.infer<typeof FinalizeAcmeOrderBodySchema>;
|
||||||
|
|
||||||
export type TJwsPayloadWithJwk = TJwsPayload & {
|
export type TJwsPayload<T> = {
|
||||||
jwk: JsonWebKey;
|
protectedHeader: TProtectedHeader;
|
||||||
alg: string;
|
jwk?: JsonWebKey;
|
||||||
|
payload: T;
|
||||||
};
|
};
|
||||||
export type TAcmeResponse<TPayload> = {
|
export type TAcmeResponse<TPayload> = {
|
||||||
status: number;
|
status: number;
|
||||||
@@ -50,7 +50,11 @@ export type TAcmeResponse<TPayload> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type TPkiAcmeServiceFactory = {
|
export type TPkiAcmeServiceFactory = {
|
||||||
validateCreateAcmeAccountJwsPayload(body: TRawJwsPayload): Promise<TJwsPayloadWithJwk>;
|
validateJwsPayload: <T>(
|
||||||
|
rawJwsPayload: TRawJwsPayload,
|
||||||
|
getJWK: (protectedHeader: JWSHeaderParameters) => Promise<JsonWebKey>,
|
||||||
|
schema: z.ZodSchema<T>
|
||||||
|
) => Promise<TJwsPayload<T>>;
|
||||||
getAcmeDirectory: (profileId: string) => Promise<TGetAcmeDirectoryResponse>;
|
getAcmeDirectory: (profileId: string) => Promise<TGetAcmeDirectoryResponse>;
|
||||||
getAcmeNewNonce: (profileId: string) => Promise<string>;
|
getAcmeNewNonce: (profileId: string) => Promise<string>;
|
||||||
createAcmeAccount: (
|
createAcmeAccount: (
|
||||||
|
|||||||
Reference in New Issue
Block a user