Skip to content
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

fix(blob): limit keys to 950 characters #750

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
5 changes: 5 additions & 0 deletions .changeset/unlucky-months-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vercel/blob': patch
---

Limit pathname length to 950 to respect internal limitations and provide better early DX.
17 changes: 17 additions & 0 deletions packages/blob/src/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,23 @@ describe('blob client', () => {
expect(headers['x-cache-control-max-age']).toEqual('60');
});

it('throws when filepath is too long', async () => {
mockClient
.intercept({
path: () => true,
method: 'PUT',
})
.reply(200, mockedFileMetaPut);

await expect(
put('a'.repeat(951), 'Test Body', {
access: 'public',
}),
).rejects.toThrow(
new Error('Vercel Blob: pathname is too long, maximum length is 950'),
);
});

const table: [string, (signal: AbortSignal) => Promise<unknown>][] = [
[
'put',
Expand Down
12 changes: 12 additions & 0 deletions packages/blob/src/put-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export function createPutHeaders<TOptions extends CommonPutCommandOptions>(
return headers;
}

// maximum pathname length is:
// 1024 (provider limit) - 26 chars (vercel internal suffixes) - 31 chars (blob `-randomId` suffix) = 967
// we round it to 950 to make it more human friendly, and we apply the limit whatever the value of
// addRandomSuffix is, to make it consistent
const MAXIMUM_PATHNAME_LENGTH = 950;

export async function createPutOptions<
TOptions extends CommonPutCommandOptions,
>({
Expand All @@ -85,6 +91,12 @@ export async function createPutOptions<
throw new BlobError('pathname is required');
}

if (pathname.length > MAXIMUM_PATHNAME_LENGTH) {
vvo marked this conversation as resolved.
Show resolved Hide resolved
throw new BlobError(
`pathname is too long, maximum length is ${MAXIMUM_PATHNAME_LENGTH}`,
);
}

if (!options) {
throw new BlobError('missing options, see usage');
}
Expand Down
Loading