Skip to content

Commit 27ee0c3

Browse files
authored
fix: trim long sender names for postman email (#1236)
## Problem Sending transactional emails can cause downstream issues in Postman when sender names exceed 255 characters. This issue was discovered in a Pipe that used a variable from FormSG, where a user mistakenly entered incorrect information in a field intended for their name. ## Solution Trim excessively long sender names to ensure emails can still be sent successfully. This is an edge case, as sender names are generally not expected to be that long. ## How to test? Connect either a FormSG or a webhook to a Pipe, and use the variable in the Email by Postman step. - [ ] Make a submission with extremely long sender name, email is still sent with the trimmed sender name
1 parent b706a7e commit 27ee0c3

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/backend/src/apps/postman/__tests__/common/parameters.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,12 @@ describe('postman transactional email schema zod validation', () => {
188188
)
189189
},
190190
)
191+
192+
it('should trim long reply to email', () => {
193+
validPayload.senderName = 'a'.repeat(256)
194+
const validLength = 255 - ' <[email protected]>'.length
195+
const result = transactionalEmailSchema.safeParse(validPayload)
196+
assert(result.success === true)
197+
expect(result.data.senderName).toEqual('a'.repeat(validLength))
198+
})
191199
})

packages/backend/src/apps/postman/common/parameters.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import validator from 'email-validator'
44
import { uniq } from 'lodash'
55
import { z } from 'zod'
66

7+
import appConfig from '@/config/app'
78
import { parseS3Id } from '@/helpers/s3'
89

910
import { POSTMAN_SUPPORTED_ATTACHMENTS_GUIDE_URL } from './constants'
@@ -121,7 +122,16 @@ export const transactionalEmailSchema = z.object({
121122
}
122123
return value.trim() === '' ? undefined : value.trim()
123124
}, z.string().email({ message: 'Invalid reply to email' }).optional()),
124-
senderName: z.string().min(1, { message: 'Empty sender name' }).trim(),
125+
senderName: z
126+
.string()
127+
.min(1, { message: 'Empty sender name' })
128+
.trim()
129+
// NOTE: we trim the sender name so that long sender names do not cause the email to fail.
130+
// Postman limits the sender name to 255 characters.
131+
// the API sends "{senderName} <[email protected]>" so it needs to be included in the calculation.
132+
.transform((value) =>
133+
value.substring(0, 255 - ` <${appConfig.postman.fromAddress}>`.length),
134+
),
125135
attachments: z.array(z.string()).transform((array, context) => {
126136
const result: string[] = []
127137
for (const value of array) {

0 commit comments

Comments
 (0)