Skip to content

Commit 05970df

Browse files
Allow Origin header to be set (for non-browser platforms). (anza-xyz#888)
* Allow Origin header to be set (for non-browser platforms). * Add changeset * Runtime error in Node/browser environments in dev * Add tests. Allow `Origin` in Node build. Expand on the changeset. --------- Co-authored-by: Steven Luscher <steven.luscher@anza.xyz>
1 parent d110d41 commit 05970df

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

.changeset/eager-lizards-stand.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@solana/rpc-transport-http': minor
3+
---
4+
5+
The React Native and Node builds now permit you to set the `Origin` header. This header continues to be forbidden in the browser build, as it features on the list of forbidden request headers: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_request_header

packages/rpc-transport-http/src/__tests__/http-transport-headers-test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ describe('assertIsAllowedHttpRequestHeader', () => {
2727
'Expect',
2828
'Host',
2929
'Keep-Alive',
30-
'Origin',
3130
'Permissions-Policy',
3231
'Proxy-Anything',
3332
'Proxy-Authenticate',
@@ -64,6 +63,17 @@ describe('assertIsAllowedHttpRequestHeader', () => {
6463
);
6564
});
6665
}
66+
if (__BROWSER__) {
67+
it('throws when called with the `Origin` header', () => {
68+
expect(() => {
69+
assertIsAllowedHttpRequestHeaders({ Origin: 'https://spoofed.site' });
70+
}).toThrow(
71+
new SolanaError(SOLANA_ERROR__RPC__TRANSPORT_HTTP_HEADER_FORBIDDEN, {
72+
headers: ['Origin'],
73+
}),
74+
);
75+
});
76+
}
6777
['Authorization', 'Content-Language', 'Solana-Client'].forEach(allowedHeader => {
6878
it('does not throw when called with the header `' + allowedHeader + '`', () => {
6979
expect(() => {

packages/rpc-transport-http/src/http-transport-headers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ type ForbiddenHeaders =
3333
| 'Expect'
3434
| 'Host'
3535
| 'Keep-Alive'
36-
| 'Origin'
36+
// Similar to `Accept-Encoding`, we don't have a way to target TypeScript types depending on
37+
// which platform you are authoring for. `Origin` is therefore omitted from the forbidden
38+
// headers type, but is still a runtime error in dev mode when supplied in a browser context.
39+
// | 'Origin'
3740
| 'Permissions-Policy'
3841
| 'Referer'
3942
| 'TE'
@@ -64,7 +67,6 @@ const FORBIDDEN_HEADERS: Record<string, boolean> = /* @__PURE__ */ Object.assign
6467
expect: true,
6568
host: true,
6669
'keep-alive': true,
67-
origin: true,
6870
'permissions-policy': true,
6971
// Prefix matching is implemented in code, below.
7072
// 'proxy-': true,
@@ -77,6 +79,7 @@ const FORBIDDEN_HEADERS: Record<string, boolean> = /* @__PURE__ */ Object.assign
7779
via: true,
7880
},
7981
__NODEJS__ ? undefined : { 'accept-encoding': true },
82+
__BROWSER__ ? { origin: true } : undefined,
8083
);
8184

8285
export function assertIsAllowedHttpRequestHeaders(

0 commit comments

Comments
 (0)