-
Notifications
You must be signed in to change notification settings - Fork 323
fix(typescript): guard against null/undefined in file upload core utility #16708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
73275ed
4f1cf60
43ab7f4
f112f94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| - summary: | | ||
| Fix `TypeError: Cannot use 'in' operator to search for 'path' in undefined` | ||
| crash when `null` or `undefined` is passed to a file upload parameter at | ||
| runtime. The error is now a clear `TypeError` describing the expected types | ||
| instead of a cryptic `in` operator failure. | ||
| type: fix |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,11 @@ export async function toBinaryUploadRequest( | |
|
|
||
| export async function toMultipartDataPart( | ||
| file: Uploadable, | ||
| ): Promise<{ data: Uploadable.FileLike; filename?: string; contentType?: string }> { | ||
| ): Promise<{ data: Uploadable.FileLike; filename?: string; contentType?: string } | undefined> { | ||
| if (file == null) { | ||
| console.warn(`File upload received ${file === null ? "null" : "undefined"}, skipping.`); | ||
| return undefined; | ||
| } | ||
| const { data, filename, contentType } = await getFileWithMetadata(file, { | ||
| noSniffFileSize: true, | ||
| }); | ||
|
|
@@ -37,6 +41,11 @@ async function getFileWithMetadata( | |
| file: Uploadable, | ||
| { noSniffFileSize }: { noSniffFileSize?: boolean } = {}, | ||
| ): Promise<Uploadable.WithMetadata> { | ||
| if (file == null) { | ||
| throw new TypeError( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use a different error type, not
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the rest of this file, line 90 uses |
||
| `Expected file to be a Blob, Buffer, ReadableStream, or an object with a "path" or "data" property, but received ${file === null ? "null" : "undefined"}.`, | ||
| ); | ||
| } | ||
| if (isFileLike(file)) { | ||
| return getFileWithMetadata( | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,7 +79,11 @@ export class Node18FormData implements CrossPlatformFormData { | |
| } | ||
|
|
||
| public async appendFile(key: string, value: Uploadable): Promise<void> { | ||
| const { data, filename } = await toMultipartDataPart(value); | ||
| const part = await toMultipartDataPart(value); | ||
| if (part == null) { | ||
| return; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't be necessary, as
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed — moving the null guard to the top of
Comment on lines
+82
to
+86
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should throw here. appendFile should not be allowed to be called with null.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The warn+skip behavior here was specifically requested to match the old 3.12.x SDK tolerance — the customer's optional file field passes If the intent is to throw here instead, the fix would need to move upstream: the generated code that calls @cadesark — could you weigh in on the approach? Should we:
Option 2 is the cleaner design but requires changes to the TypeScript SDK generator's request body serialization.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — |
||
| const { data, filename } = part; | ||
|
|
||
| if (data instanceof Blob) { | ||
| this.fd?.append(key, data, filename); | ||
|
|
@@ -140,7 +144,11 @@ export class Node16FormData implements CrossPlatformFormData { | |
| } | ||
|
|
||
| public async appendFile(key: string, value: Uploadable): Promise<void> { | ||
| const { data, filename } = await toMultipartDataPart(value); | ||
| const part = await toMultipartDataPart(value); | ||
| if (part == null) { | ||
| return; | ||
| } | ||
| const { data, filename } = part; | ||
|
|
||
| let bufferedValue; | ||
| if (data instanceof Blob) { | ||
|
|
@@ -181,7 +189,11 @@ export class WebFormData implements CrossPlatformFormData { | |
| } | ||
|
|
||
| public async appendFile(key: string, value: Uploadable): Promise<void> { | ||
| const { data, filename, contentType } = await toMultipartDataPart(value); | ||
| const part = await toMultipartDataPart(value); | ||
| if (part == null) { | ||
| return; | ||
| } | ||
| const { data, filename, contentType } = part; | ||
|
|
||
| if (data instanceof Blob) { | ||
| this.fd?.append(key, data, filename); | ||
|
|
@@ -221,7 +233,11 @@ export class FormDataWrapper { | |
| } | ||
|
|
||
| public async appendFile(key: string, value: Uploadable): Promise<void> { | ||
| const { data, filename, contentType } = await toMultipartDataPart(value); | ||
| const part = await toMultipartDataPart(value); | ||
| if (part == null) { | ||
| return; | ||
| } | ||
| const { data, filename, contentType } = part; | ||
| const blob = await convertToBlob(data, contentType); | ||
| if (filename) { | ||
| this.fd.append(key, blob, filename); | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since
fileis not null | undefined, I still think we should throw an error here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed — since
fileis typed asUploadable(not nullable), if it somehow reaches here as null it's a logic error and should throw rather than silently swallow. The tolerance/skip should happen at theappendFilelevel (the API boundary) before this function is ever called.