Skip to content

Commit e91d291

Browse files
feat(client): add support for browser usage (#504)
1 parent 75c57e1 commit e91d291

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,18 @@ The following runtimes are supported:
418418
- Vercel Edge Runtime.
419419
- Jest 28 or greater with the `"node"` environment (`"jsdom"` is not supported at this time).
420420
- Nitro v2.6 or greater.
421-
422-
> [!WARNING]
423-
> Web browser runtimes aren't supported. The SDK will throw an error if used in a browser environment.
421+
- Web browsers: disabled by default to avoid exposing your secret API credentials. Enable browser support by explicitly setting `dangerouslyAllowBrowser` to true'.
422+
<details>
423+
<summary>More explanation</summary>
424+
### Why is this dangerous?
425+
Enabling the `dangerouslyAllowBrowser` option can be dangerous because it exposes your secret API credentials in the client-side code. Web browsers are inherently less secure than server environments,
426+
any user with access to the browser can potentially inspect, extract, and misuse these credentials. This could lead to unauthorized access using your credentials and potentially compromise sensitive data or functionality.
427+
### When might this not be dangerous?
428+
In certain scenarios where enabling browser support might not pose significant risks:
429+
- Internal Tools: If the application is used solely within a controlled internal environment where the users are trusted, the risk of credential exposure can be mitigated.
430+
- Public APIs with Limited Scope: If your API has very limited scope and the exposed credentials do not grant access to sensitive data or critical operations, the potential impact of exposure is reduced.
431+
- Development or debugging purpose: Enabling this feature temporarily might be acceptable, provided the credentials are short-lived, aren't also used in production environments, or are frequently rotated.
432+
</details>
424433

425434
Note that React Native is not supported at this time.
426435

src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export interface ClientOptions {
7272
* param to `undefined` in request options.
7373
*/
7474
defaultQuery?: Core.DefaultQuery;
75+
76+
/**
77+
* By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
78+
* Only set this option to `true` if you understand the risks and have appropriate mitigations in place.
79+
*/
80+
dangerouslyAllowBrowser?: boolean;
7581
}
7682

7783
/**
@@ -95,6 +101,7 @@ export class Anthropic extends Core.APIClient {
95101
* @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
96102
* @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API.
97103
* @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.
104+
* @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
98105
*/
99106
constructor({
100107
baseURL = Core.readEnv('ANTHROPIC_BASE_URL'),
@@ -109,9 +116,9 @@ export class Anthropic extends Core.APIClient {
109116
baseURL: baseURL || `https://api.anthropic.com`,
110117
};
111118

112-
if (Core.isRunningInBrowser()) {
119+
if (!options.dangerouslyAllowBrowser && Core.isRunningInBrowser()) {
113120
throw new Errors.AnthropicError(
114-
"It looks like you're running in a browser-like environment, which is disabled to protect your secret API credentials from attackers. If you have a strong business need for client-side use of this API, please open a GitHub issue with your use-case and security mitigations.",
121+
"It looks like you're running in a browser-like environment.\n\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\nIf you understand the risks and have appropriate mitigations in place,\nyou can set the `dangerouslyAllowBrowser` option to `true`, e.g.,\n\nnew Anthropic({ apiKey, dangerouslyAllowBrowser: true });\n\nTODO: link!\n",
115122
);
116123
}
117124

@@ -140,6 +147,9 @@ export class Anthropic extends Core.APIClient {
140147
protected override defaultHeaders(opts: Core.FinalRequestOptions): Core.Headers {
141148
return {
142149
...super.defaultHeaders(opts),
150+
...(this._options.dangerouslyAllowBrowser ?
151+
{ 'anthropic-dangerous-direct-browser-access': 'true' }
152+
: undefined),
143153
'anthropic-version': '2023-06-01',
144154
...this._options.defaultHeaders,
145155
};

0 commit comments

Comments
 (0)