Skip to content

Commit 76a718c

Browse files
authored
Force auth token usage for storage uploads (#6973)
1 parent 1e924e0 commit 76a718c

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

packages/thirdweb/src/storage/upload/web-node.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export async function uploadBatch<const TFiles extends UploadableFile[]>(
2525
body: form,
2626
requestTimeoutMs:
2727
client.config?.storage?.fetch?.requestTimeoutMs || 120000,
28+
// force auth token usage for storage uploads
29+
useAuthToken: true,
2830
},
2931
);
3032

packages/thirdweb/src/utils/fetch.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("getClientFetch", () => {
5252
);
5353
});
5454

55-
it("should send a bearer token if secret key is a JWT", () => {
55+
it("should NOT send a bearer token if secret key is a JWT", () => {
5656
vi.spyOn(global, "fetch").mockResolvedValue(new Response());
5757
const clientFetch = getClientFetch({
5858
clientId: "test-client-id",
@@ -67,6 +67,28 @@ describe("getClientFetch", () => {
6767
}),
6868
);
6969

70+
// biome-ignore lint/suspicious/noExplicitAny: `any` type ok for tests
71+
const headers = (global.fetch as any).mock.calls[0][1].headers;
72+
expect(headers.get("authorization")).toBe(null);
73+
});
74+
75+
it("should send a bearer token if secret key is a JWT and useAuthToken is true", () => {
76+
vi.spyOn(global, "fetch").mockResolvedValue(new Response());
77+
const clientFetch = getClientFetch({
78+
clientId: "test-client-id",
79+
secretKey: "foo.bar.baz",
80+
});
81+
clientFetch("https://api.thirdweb.com/test", {
82+
useAuthToken: true,
83+
});
84+
85+
expect(global.fetch).toHaveBeenCalledWith(
86+
"https://api.thirdweb.com/test",
87+
expect.objectContaining({
88+
headers: expect.any(Headers),
89+
}),
90+
);
91+
7092
// biome-ignore lint/suspicious/noExplicitAny: `any` type ok for tests
7193
const headers = (global.fetch as any).mock.calls[0][1].headers;
7294
expect(headers.get("authorization")).toBe("Bearer foo.bar.baz");

packages/thirdweb/src/utils/fetch.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ export function getClientFetch(client: ThirdwebClient, ecosystem?: Ecosystem) {
2222
*/
2323
async function fetchWithHeaders(
2424
url: string | Request,
25-
init?: Omit<RequestInit, "signal"> & { requestTimeoutMs?: number },
25+
init?: Omit<RequestInit, "signal"> & {
26+
requestTimeoutMs?: number;
27+
useAuthToken?: boolean;
28+
},
2629
): Promise<Response> {
27-
const { requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT, ...restInit } =
28-
init || {};
30+
const {
31+
requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT,
32+
useAuthToken,
33+
...restInit
34+
} = init || {};
2935

3036
let headers = restInit.headers
3137
? new Headers(restInit.headers)
@@ -41,7 +47,7 @@ export function getClientFetch(client: ThirdwebClient, ecosystem?: Ecosystem) {
4147
}
4248
// auth token if secret key === jwt
4349
const authToken =
44-
client.secretKey && isJWT(client.secretKey)
50+
useAuthToken && client.secretKey && isJWT(client.secretKey)
4551
? client.secretKey
4652
: undefined;
4753
// secret key if secret key !== jwt

0 commit comments

Comments
 (0)