@@ -10,6 +10,10 @@ const ROUTE = '/api/support/ticket'
1010const MAX_DESCRIPTION_LENGTH = 1000
1111const MIN_DESCRIPTION_LENGTH = 10
1212const MAX_EMAIL_LENGTH = 254
13+ // Legit payloads top out around ~3.5 KB (Turnstile token + capped fields).
14+ // Rejecting anything larger before JSON.parse blocks DoS amplification with
15+ // oversized bodies that would otherwise consume memory just to be discarded.
16+ const MAX_BODY_BYTES = 8_192
1317
1418interface SiteVerifyResponse {
1519 success : boolean
@@ -39,13 +43,24 @@ const FIELD_ERROR_CODES: Record<keyof TicketData, string> = {
3943}
4044const FIELD_PRIORITY : ( keyof TicketData ) [ ] = [ 'token' , 'topic' , 'description' , 'email' ]
4145
46+ /**
47+ * Strips Unicode control characters a reader can't see but that change how
48+ * text is rendered — RTL overrides (U+202A–202E), directional isolates
49+ * (U+2066–2069), zero-width spaces/joiners (U+200B–200D), and BOM (U+FEFF).
Hidden Unicode 50+ * These are used to disguise URLs (e.g. `evil.com/exe.doc`) or hide payloads
51+ * inside otherwise-innocent text.
52+ */
53+ const stripInvisibleControls = ( text : string ) : string =>
54+ text . replaceAll ( / [ \u202A - \u202E \u2066 - \u2069 \u200B - \u200D \uFEFF ] / g, '' )
55+
4256/**
4357 * Escapes characters that Slack interprets in `mrkdwn` text so that
4458 * user-supplied content can't inject mentions (e.g. `<!channel>`, `<@U…>`)
45- * or break the block layout. See https://api.slack.com/reference/surfaces/formatting#escaping
59+ * or break the block layout, and strips invisible control chars.
60+ * See https://api.slack.com/reference/surfaces/formatting#escaping
4661 */
47- const escapeSlack = ( text : string ) : string =>
48- text . replaceAll ( '&' , '&' ) . replaceAll ( '<' , '<' ) . replaceAll ( '>' , '>' )
62+ const escapeSlackMrkdwn = ( text : string ) : string =>
63+ stripInvisibleControls ( text ) . replaceAll ( '&' , '&' ) . replaceAll ( '<' , '<' ) . replaceAll ( '>' , '>' )
4964
5065const generateTicketRef = ( ) : string =>
5166 `SUP-${ crypto . randomUUID ( ) . replaceAll ( '-' , '' ) . slice ( 0 , 8 ) . toUpperCase ( ) } `
@@ -61,19 +76,34 @@ const buildSlackBlocks = (
6176 type : 'header' ,
6277 text : { type : 'plain_text' , text : 'New support ticket' , emoji : false } ,
6378 } ,
79+ {
80+ type : 'context' ,
81+ elements : [
82+ {
83+ type : 'mrkdwn' ,
84+ text : ':warning: Content submitted by an unverified user. Verify any link before clicking.' ,
85+ } ,
86+ ] ,
87+ } ,
6488 {
6589 type : 'section' ,
6690 fields : [
6791 { type : 'mrkdwn' , text : `*Ticket:*\n${ ticketRef } ` } ,
6892 { type : 'mrkdwn' , text : `*Topic:*\n${ topic } ` } ,
69- { type : 'mrkdwn' , text : `*From:*\n${ email ? escapeSlack ( email ) : '_anonymous_' } ` } ,
93+ { type : 'mrkdwn' , text : `*From:*\n${ email ? escapeSlackMrkdwn ( email ) : '_anonymous_' } ` } ,
7094 { type : 'mrkdwn' , text : `*Received:*\n${ receivedAt } ` } ,
7195 ] ,
7296 } ,
7397 { type : 'divider' } ,
7498 {
7599 type : 'section' ,
76- text : { type : 'mrkdwn' , text : `*Description:*\n${ escapeSlack ( description ) } ` } ,
100+ text : { type : 'mrkdwn' , text : '*Description:*' } ,
101+ } ,
102+ {
103+ type : 'section' ,
104+ // plain_text disables Slack's URL auto-linkification, so a phishing link
105+ // in the description is not clickable — the agent must copy it out.
106+ text : { type : 'plain_text' , text : stripInvisibleControls ( description ) , emoji : false } ,
77107 } ,
78108]
79109
@@ -89,6 +119,11 @@ export async function POST(request: NextRequest) {
89119 return NextResponse . json ( { success : false , error : 'server_misconfigured' } , { status : 500 } )
90120 }
91121
122+ const contentLength = Number ( request . headers . get ( 'content-length' ) ?? 0 )
123+ if ( contentLength > MAX_BODY_BYTES ) {
124+ return NextResponse . json ( { success : false , error : 'body_too_large' } , { status : 413 } )
125+ }
126+
92127 let rawBody : unknown
93128 try {
94129 rawBody = await request . json ( )
@@ -107,26 +142,20 @@ export async function POST(request: NextRequest) {
107142
108143 const { token, topic, description, email } = parsed . data
109144
110- const remoteip = request . headers . get ( 'x-forwarded-for' ) ?. split ( ',' ) [ 0 ] . trim ( )
111-
112145 try {
146+ // `remoteip` is intentionally omitted — the only header we could source it
147+ // from (`x-forwarded-for`) is attacker-controllable and would let a spoofed
148+ // value reach Cloudflare. siteverify accepts requests without it.
113149 const cfResponse = await fetch ( SITEVERIFY_URL , {
114150 method : 'POST' ,
115151 headers : { 'content-type' : 'application/x-www-form-urlencoded' } ,
116- body : new URLSearchParams ( {
117- secret,
118- response : token ,
119- ...( remoteip ? { remoteip } : { } ) ,
120- } ) ,
152+ body : new URLSearchParams ( { secret, response : token } ) ,
121153 } )
122154 const verification = ( await cfResponse . json ( ) ) as SiteVerifyResponse
123155
124156 if ( ! verification . success ) {
125157 logger . warn ( { route : ROUTE , errorCodes : verification [ 'error-codes' ] } , 'Turnstile verification failed' )
126- return NextResponse . json (
127- { success : false , errorCodes : verification [ 'error-codes' ] ?? [ ] } ,
128- { status : 403 } ,
129- )
158+ return NextResponse . json ( { success : false , error : 'captcha_failed' } , { status : 403 } )
130159 }
131160 } catch ( err ) {
132161 logger . error ( { err, route : ROUTE } , 'Error contacting Cloudflare siteverify' )
@@ -140,7 +169,7 @@ export async function POST(request: NextRequest) {
140169 method : 'POST' ,
141170 headers : { 'content-type' : 'application/json' } ,
142171 body : JSON . stringify ( {
143- text : `New support ticket ${ ticketRef } (${ topic } ) from ${ email ? escapeSlack ( email ) : 'anonymous' } ` ,
172+ text : `New support ticket ${ ticketRef } (${ topic } ) from ${ email ? escapeSlackMrkdwn ( email ) : 'anonymous' } ` ,
144173 blocks : buildSlackBlocks ( ticketRef , topic , description , email , new Date ( ) . toISOString ( ) ) ,
145174 } ) ,
146175 } )
0 commit comments