Skip to content

Commit

Permalink
feat: ignorebodyhashcheck only when body is not used
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Oct 18, 2024
1 parent 926a309 commit dbe0c54
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions packages/app/src/app/submit/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ export const formSchema = z.object({
message: "Needs to match this pattern 'xxxx/yyyy'",
}),
description: z.string().min(1),
tags: z.string().transform( ( str, ctx ) => {
tags: z.string().transform((str, ctx) => {
try {
return str.split(",")
} catch ( e ) {
ctx.addIssue( { code: 'custom', message: 'Invalid tags' } )
} catch (e) {
ctx.addIssue({ code: 'custom', message: 'Invalid tags' })
return z.NEVER
}
}),
emailQuery: z.string(),
useNewSdk: z.boolean(),
parameters: z.object({
name: z.string().min(1).transform(value => value.replace(/"/g, '')),
// name can only be valid variable names
name: z.string().min(1).regex(/^[a-zA-Z_][a-zA-Z0-9_]*$/, "Invalid name, must start with a letter, digit, or underscore, and can only contain letters, digits or underscores."),
ignoreBodyHashCheck: z.boolean(),
enableMasking: z.boolean(),
shaPrecomputeSelector: z.string().transform(value => value.replace(/(?<!\\)"/g, '\\"')),
Expand All @@ -29,16 +30,16 @@ export const formSchema = z.object({
}),
dkimSelector: z.string().optional().refine(
(value) => {
if (value === undefined || value === "") return true; // Allow undefined values
return /^[a-zA-Z0-9](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?)*$/.test(value) && value.length <= 253;
if (value === undefined || value === "") return true; // Allow undefined values
return /^[a-zA-Z0-9](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-_]{0,61}[a-zA-Z0-9])?)*$/.test(value) && value.length <= 253;
},
{
message: "Invalid DKIM selector format or length",
message: "Invalid DKIM selector format or length",
}
),
emailBodyMaxLength: z.coerce.number().transform((n, ctx) => {
if (n % 64 !== 0) {
ctx.addIssue({code: 'custom', message: 'Must be a multiple of 64'})
ctx.addIssue({ code: 'custom', message: 'Must be a multiple of 64' })
}
return n;
}),
Expand All @@ -61,24 +62,24 @@ export const formSchema = z.object({
try {
return JSON.parse(str)
} catch (e) {
ctx.addIssue( {code: 'custom', message: 'Must look like [[[22,1],[1,1]]]'})
ctx.addIssue({ code: 'custom', message: 'Must look like [[[22,1],[1,1]]]' })
}
}).optional(),
parts: z.string().transform( ( str, ctx ) => {
parts: z.string().transform((str, ctx) => {
// Check if the string contains 'is_public'
if (!str.includes('is_public')) {
ctx.addIssue({
code: 'custom',
message: 'Each parts config must include at least one "is_public" field, and at least one thats true and one thats false. Please add it for now until we fix this requirement.'
ctx.addIssue({
code: 'custom',
message: 'Each parts config must include at least one "is_public" field, and at least one thats true and one thats false. Please add it for now until we fix this requirement.'
});
return z.NEVER;
}
let parsed;

try {
parsed = JSON.parse( str )
} catch ( e ) {
ctx.addIssue( { code: 'custom', message: 'Invalid JSON' } )
parsed = JSON.parse(str)
} catch (e) {
ctx.addIssue({ code: 'custom', message: 'Invalid JSON' })
return z.NEVER
}
// Validate the structure of the parsed JSON
Expand Down Expand Up @@ -110,16 +111,20 @@ export const formSchema = z.object({
return parsed
}
catch (e: any) {
ctx.addIssue( { code: 'custom', message: (e as Error).message } )
ctx.addIssue({ code: 'custom', message: (e as Error).message })
return z.NEVER
}

}).optional()
.or(z.array(z.any())), // this is that when we pre-populate the form directly with an array, the form will still accept it
.or(z.array(z.any())), // this is that when we pre-populate the form directly with an array, the form will still accept it
})),
externalInputs: z.array(z.object({
name: z.string().min(1),
maxLength: z.coerce.number().positive().default(64),
}))
}),
})
}).refine(data => // make sure if values contain body, then ignoreBodyHashCheck must be false
!data.parameters.ignoreBodyHashCheck || !data.parameters.values.some(value => value.location === 'body'), {
message: "Ignore body hash check must be false if you want to extract data from the email body",
path: ["parameters", "ignoreBodyHashCheck"]
});

0 comments on commit dbe0c54

Please sign in to comment.