@ex-machina/opencode-anthropic-auth 1.0.4 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,6 +12,19 @@ Add the plugin to your OpenCode configuration:
12
12
  }
13
13
  ```
14
14
 
15
+ > [!TIP]
16
+ > It is STRONGLY advised that you pin the plugin to a version. This will keep you from getting automatic updates; however, this will protect you from nefarious updates.
17
+ >
18
+ > This holds true for ANY OpenCode plugin. If you do not pin them, OpenCode will automatically update them on startup. It's a massive vulnerability waiting to happen.
19
+
20
+ #### Example of pinned version
21
+
22
+ ```json
23
+ {
24
+ "plugin": ["@ex-machina/opencode-anthropic-auth@1.1.1"]
25
+ }
26
+ ```
27
+
15
28
  ## Authentication Methods
16
29
 
17
30
  The plugin provides three authentication options:
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { authorize, exchange } from './auth';
2
2
  import { CLIENT_ID, TOKEN_URL } from './constants';
3
- import { createStrippedStream, mergeHeaders, prefixToolNames, rewriteUrl, setOAuthHeaders, } from './transform';
3
+ import { createStrippedStream, isInsecure, mergeHeaders, prefixToolNames, rewriteUrl, setOAuthHeaders, } from './transform';
4
4
  export const AnthropicAuthPlugin = async ({ client }) => {
5
5
  return {
6
6
  'experimental.chat.system.transform': (input, output) => {
@@ -117,6 +117,8 @@ export const AnthropicAuthPlugin = async ({ client }) => {
117
117
  ...init,
118
118
  body,
119
119
  headers: requestHeaders,
120
+ // biome-ignore lint/suspicious/noExplicitAny: tls option is Bun-specific, not in standard RequestInit
121
+ ...(isInsecure() && { tls: { rejectUnauthorized: false } }),
120
122
  });
121
123
  return createStrippedStream(response);
122
124
  },
@@ -22,8 +22,15 @@ export declare function prefixToolNames(body: string): string;
22
22
  * Strip TOOL_PREFIX from tool names in streaming response text.
23
23
  */
24
24
  export declare function stripToolPrefix(text: string): string;
25
+ /**
26
+ * Check if TLS verification should be skipped for custom API endpoints.
27
+ * Only effective when ANTHROPIC_BASE_URL is also set.
28
+ */
29
+ export declare function isInsecure(): boolean;
25
30
  /**
26
31
  * Rewrite the request URL to add ?beta=true for /v1/messages requests.
32
+ * When ANTHROPIC_BASE_URL is set, overrides the origin (protocol + host)
33
+ * for all API requests flowing through the fetch wrapper.
27
34
  * Returns the modified input and URL (if applicable).
28
35
  */
29
36
  export declare function rewriteUrl(input: FetchInput): {
package/dist/transform.js CHANGED
@@ -98,8 +98,41 @@ export function prefixToolNames(body) {
98
98
  export function stripToolPrefix(text) {
99
99
  return text.replace(/"name"\s*:\s*"mcp_([^"]+)"/g, '"name": "$1"');
100
100
  }
101
+ /**
102
+ * Check if TLS verification should be skipped for custom API endpoints.
103
+ * Only effective when ANTHROPIC_BASE_URL is also set.
104
+ */
105
+ export function isInsecure() {
106
+ if (!process.env.ANTHROPIC_BASE_URL?.trim())
107
+ return false;
108
+ const raw = process.env.ANTHROPIC_INSECURE?.trim();
109
+ return raw === '1' || raw === 'true';
110
+ }
111
+ /**
112
+ * Parse ANTHROPIC_BASE_URL from the environment.
113
+ * Returns a valid HTTP(S) URL or null if unset/invalid.
114
+ */
115
+ function resolveBaseUrl() {
116
+ const raw = process.env.ANTHROPIC_BASE_URL?.trim();
117
+ if (!raw)
118
+ return null;
119
+ try {
120
+ const baseUrl = new URL(raw);
121
+ if ((baseUrl.protocol !== 'http:' && baseUrl.protocol !== 'https:') ||
122
+ baseUrl.username ||
123
+ baseUrl.password) {
124
+ return null;
125
+ }
126
+ return baseUrl;
127
+ }
128
+ catch {
129
+ return null;
130
+ }
131
+ }
101
132
  /**
102
133
  * Rewrite the request URL to add ?beta=true for /v1/messages requests.
134
+ * When ANTHROPIC_BASE_URL is set, overrides the origin (protocol + host)
135
+ * for all API requests flowing through the fetch wrapper.
103
136
  * Returns the modified input and URL (if applicable).
104
137
  */
105
138
  export function rewriteUrl(input) {
@@ -115,16 +148,25 @@ export function rewriteUrl(input) {
115
148
  catch {
116
149
  requestUrl = null;
117
150
  }
118
- if (requestUrl &&
119
- requestUrl.pathname === '/v1/messages' &&
151
+ if (!requestUrl)
152
+ return { input, url: null };
153
+ const originalHref = requestUrl.href;
154
+ const baseUrl = resolveBaseUrl();
155
+ if (baseUrl) {
156
+ requestUrl.protocol = baseUrl.protocol;
157
+ requestUrl.host = baseUrl.host;
158
+ }
159
+ if (requestUrl.pathname === '/v1/messages' &&
120
160
  !requestUrl.searchParams.has('beta')) {
121
161
  requestUrl.searchParams.set('beta', 'true');
122
- const newInput = input instanceof Request
123
- ? new Request(requestUrl.toString(), input)
124
- : requestUrl;
125
- return { input: newInput, url: requestUrl };
126
162
  }
127
- return { input, url: requestUrl };
163
+ if (requestUrl.href === originalHref) {
164
+ return { input, url: requestUrl };
165
+ }
166
+ const newInput = input instanceof Request
167
+ ? new Request(requestUrl.toString(), input)
168
+ : requestUrl;
169
+ return { input: newInput, url: requestUrl };
128
170
  }
129
171
  /**
130
172
  * Create a streaming response that strips the tool prefix from tool names.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ex-machina/opencode-anthropic-auth",
3
- "version": "1.0.4",
3
+ "version": "1.1.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/ex-machina-co/opencode-anthropic-auth"