-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(blob): enforce content-type on fetch requests (#491)
* fix(blob): enforce content-type on fetch requests Before this commit, we would not specify the type of body we were sending to the customer routes handling token generation. On the app router example this is not an issue because you have to express await req.json(). But on the pages router situations, this is an issue because req.body is automatically filled/parsed using the content-type. We're fixing another place where we have this issue in our internal API triggering upload completed. * Create tender-planes-fail.md * chore(blob): bump internal API version to 5 to enforce JSON content type callbacks
- Loading branch information
Showing
8 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"@vercel/blob": patch | ||
--- | ||
|
||
fix(blob): Enforce content-type on fetch requests during token generation | ||
|
||
Before this change, we would not send the content-type header on fetch requests sent to your server during client uploads. We consider this a bugfix as it should have been sent before. | ||
|
||
⚠️ If you upgrade to this version, and you're using any smart request body parser (like Next.js Pages API routes) then: You need to remove any `JSON.parse(request.body)` at the `handleUpload` step, as the body will be JSON by default now. This is valid for the `onBeforeGenerateToken` and `onUploadCompleted` steps. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// TextEncoder and TextDecoder are not defined in Jest dom environment, | ||
// but they are available everywhere else. | ||
// See https://stackoverflow.com/questions/68468203/why-am-i-getting-textencoder-is-not-defined-in-jest | ||
const { TextEncoder, TextDecoder } = require('node:util'); | ||
|
||
Object.assign(global, { TextDecoder, TextEncoder }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import undici from 'undici'; | ||
import { upload } from './client'; | ||
|
||
// can't use undici mocking utilities because jsdom does not support performance.markResourceTiming | ||
jest.mock('undici', () => ({ | ||
fetch: jest | ||
.fn() | ||
.mockResolvedValueOnce({ | ||
status: 200, | ||
ok: true, | ||
json: () => | ||
Promise.resolve({ | ||
type: 'blob.generate-client-token', | ||
clientToken: 'fake-token-for-test', | ||
}), | ||
}) | ||
.mockResolvedValueOnce({ | ||
status: 200, | ||
ok: true, | ||
json: () => | ||
Promise.resolve({ | ||
url: `https://storeId.public.blob.vercel-storage.com/superfoo.txt`, | ||
pathname: 'foo.txt', | ||
contentType: 'text/plain', | ||
contentDisposition: 'attachment; filename="foo.txt"', | ||
}), | ||
}), | ||
})); | ||
|
||
describe('upload()', () => { | ||
beforeEach(() => { | ||
process.env.BLOB_READ_WRITE_TOKEN = | ||
'vercel_blob_rw_12345fakeStoreId_30FakeRandomCharacters12345678'; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should upload a file from the client', async () => { | ||
await expect( | ||
upload('foo.txt', 'Test file data', { | ||
access: 'public', | ||
handleUploadUrl: '/api/upload', | ||
}), | ||
).resolves.toMatchInlineSnapshot(` | ||
{ | ||
"contentDisposition": "attachment; filename="foo.txt"", | ||
"contentType": "text/plain", | ||
"pathname": "foo.txt", | ||
"url": "https://storeId.public.blob.vercel-storage.com/superfoo.txt", | ||
} | ||
`); | ||
|
||
const fetchMock = undici.fetch as jest.Mock; | ||
expect(fetchMock).toHaveBeenCalledTimes(2); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
1, | ||
'http://localhost:3000/api/upload', | ||
{ | ||
body: '{"type":"blob.generate-client-token","payload":{"pathname":"foo.txt","callbackUrl":"http://localhost:3000/api/upload"}}', | ||
headers: { 'content-type': 'application/json' }, | ||
method: 'POST', | ||
}, | ||
); | ||
expect(fetchMock).toHaveBeenNthCalledWith( | ||
2, | ||
'https://blob.vercel-storage.com/foo.txt', | ||
{ | ||
body: 'Test file data', | ||
duplex: 'half', | ||
headers: { | ||
authorization: 'Bearer fake-token-for-test', | ||
'x-api-version': '5', | ||
}, | ||
method: 'PUT', | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f9c4061
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.
Successfully deployed to the following URLs:
vercel-storage-next-integration-test-suite – ./
vercel-storage-next-integration-test-s-git-14bcac-curated-tests.vercel.app
storage-rho.vercel.app
vercel-storage-next-integration-test-suite-curated-tests.vercel.app