Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix RFC 2047 encoded word length limit
Split long email subjects into multiple RFC 2047 encoded words to comply with the 75-character limit per RFC 2047 Section 2. Each encoded word now contains at most 45 bytes of UTF-8 content (producing max 60 chars of base64 + 12 chars overhead = 72 total). Multiple encoded words are separated by CRLF + space (folding whitespace).

Applied via @cursor push command
  • Loading branch information
cursoragent committed Mar 11, 2026
commit f207f8e3ca9c57c290a903b926febf7e61ea3f25
47 changes: 47 additions & 0 deletions apps/sim/tools/gmail/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import { encodeRfc2047 } from './utils'

describe('encodeRfc2047', () => {
it('returns ASCII text unchanged', () => {
const input = 'Simple ASCII Subject'
expect(encodeRfc2047(input)).toBe(input)
})

it('encodes short non-ASCII text in a single encoded word', () => {
const input = 'Hello 世界'
const result = encodeRfc2047(input)
expect(result).toMatch(/^=\?UTF-8\?B\?[A-Za-z0-9+/=]+\?=$/)
expect(result.length).toBeLessThanOrEqual(75)
})

it('splits long non-ASCII text into multiple encoded words', () => {
const input = '今週のミーティングアジェンダについて検討します'
const result = encodeRfc2047(input)
expect(result).toContain('\r\n ')
const words = result.split('\r\n ')
expect(words.length).toBeGreaterThan(1)
words.forEach((word) => {
expect(word.length).toBeLessThanOrEqual(75)
expect(word).toMatch(/^=\?UTF-8\?B\?[A-Za-z0-9+/=]+\?=$/)
})
})

it('handles very long subjects with emojis', () => {
const input = '🎉 '.repeat(30)
const result = encodeRfc2047(input)
const words = result.split('\r\n ')
words.forEach((word) => {
expect(word.length).toBeLessThanOrEqual(75)
expect(word).toMatch(/^=\?UTF-8\?B\?[A-Za-z0-9+/=]+\?=$/)
})
})

it('handles edge case of exactly 47 bytes of UTF-8', () => {
const input = 'a'.repeat(47)
const result = encodeRfc2047(input)
expect(result).not.toContain('\r\n ')
})
})
18 changes: 16 additions & 2 deletions apps/sim/tools/gmail/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ function generateBoundary(): string {
* Encode a header value using RFC 2047 Base64 encoding if it contains non-ASCII characters.
* Email headers per RFC 2822 must be ASCII-only. Non-ASCII characters (emojis, accented
* characters, etc.) must be encoded as =?UTF-8?B?<base64>?= to avoid mojibake.
* Per RFC 2047, each encoded word must not exceed 75 characters. Long values are split
* into multiple encoded words separated by CRLF + space (folding whitespace).
* @param value The header value to encode
* @returns The encoded header value, or the original if it's already ASCII
*/
Expand All @@ -306,8 +308,20 @@ export function encodeRfc2047(value: string): string {
if (/^[\x00-\x7F]*$/.test(value)) {
return value
}
const encoded = Buffer.from(value, 'utf-8').toString('base64')
return `=?UTF-8?B?${encoded}?=`

const utf8Bytes = Buffer.from(value, 'utf-8')
const encodedWords: string[] = []
const maxBytesPerWord = 45

let offset = 0
while (offset < utf8Bytes.length) {
const chunk = utf8Bytes.subarray(offset, offset + maxBytesPerWord)
const encoded = chunk.toString('base64')
encodedWords.push(`=?UTF-8?B?${encoded}?=`)
offset += maxBytesPerWord
}

return encodedWords.join('\r\n ')
}
Comment thread
waleedlatif1 marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.

/**
Expand Down
Loading