Skip to content

Commit 73459f2

Browse files
fix: shorten multipart boundary sep to less than RFC specificed max length
1 parent 919dafa commit 73459f2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/openai/internal/util.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,8 @@ class << self
566566
#
567567
# @return [Array(String, Enumerable<String>)]
568568
private def encode_multipart_streaming(body)
569-
boundary = SecureRandom.urlsafe_base64(60)
569+
# RFC 1521 Section 7.2.1 says we should have 70 char maximum for boundary length
570+
boundary = SecureRandom.urlsafe_base64(46)
570571

571572
closing = []
572573
strio = writable_enum do |y|

test/openai/internal/util_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ def env_table
213213
end
214214
end
215215

216+
def test_encoding_length
217+
headers, = OpenAI::Internal::Util.encode_content(
218+
{"content-type" => "multipart/form-data"},
219+
Pathname(__FILE__)
220+
)
221+
assert_pattern do
222+
headers.fetch("content-type") => /boundary=(.+)$/
223+
end
224+
field, = Regexp.last_match.captures
225+
assert(field.length < 70 - 6)
226+
end
227+
216228
def test_file_encode
217229
file = Pathname(__FILE__)
218230
headers = {"content-type" => "multipart/form-data"}

0 commit comments

Comments
 (0)