@pikecode/api-key-manager 2.0.1 → 2.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikecode/api-key-manager",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "A CLI tool for managing and switching multiple API provider configurations",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/config.js CHANGED
@@ -4,6 +4,7 @@ const os = require('os');
4
4
  const chalk = require('chalk');
5
5
  const { writeJsonAtomic } = require('./utils/atomic-file');
6
6
  const { validateAndNormalizeConfigData } = require('./utils/import-validator');
7
+ const { validator } = require('./utils/validator');
7
8
 
8
9
  /**
9
10
  * @typedef {Object} ProviderConfig
@@ -134,6 +135,7 @@ class ConfigManager {
134
135
  await this._performSave(this.config, { skipConflictCheck: true });
135
136
  } else {
136
137
  this.config = validateAndNormalizeConfigData({ ...this.getDefaultConfig(), ...data });
138
+ this._warnAboutInsecureRemoteHttpProviders();
137
139
  }
138
140
 
139
141
  // 迁移旧的认证模式
@@ -193,6 +195,21 @@ class ConfigManager {
193
195
  }
194
196
  }
195
197
 
198
+ _warnAboutInsecureRemoteHttpProviders() {
199
+ for (const [name, provider] of Object.entries(this.config.providers || {})) {
200
+ if (!validator.isInsecureRemoteHttp(provider.baseUrl)) {
201
+ continue;
202
+ }
203
+
204
+ const providerName = provider.displayName || name;
205
+ console.log(
206
+ chalk.yellow(
207
+ `⚠️ 供应商 "${providerName}" 使用远程 HTTP,Token 将以明文传输: ${provider.baseUrl}`
208
+ )
209
+ );
210
+ }
211
+ }
212
+
196
213
  _migrateAuthModes() {
197
214
  // 迁移旧配置以保持向后兼容
198
215
  if (this.config.providers) {
@@ -135,8 +135,10 @@ function validateAndNormalizeData(data, source) {
135
135
  const launchArgs = provider.launchArgs || [];
136
136
  const lastUsedArgs = provider.lastUsedArgs || [];
137
137
 
138
- // 本地配置、导入配置和交互式编辑必须共享同一套 URL 规则。
139
- const baseUrlError = validator.validateUrl(provider.baseUrl, false);
138
+ // 外部导入保持安全默认值;本地显式配置允许连接远程 HTTP 代理。
139
+ const baseUrlError = validator.validateUrl(provider.baseUrl, false, {
140
+ allowInsecureHttp: source !== 'import'
141
+ });
140
142
  if (baseUrlError) {
141
143
  throw new Error(`供应商 "${safeName}" 的基础 URL 无效: ${baseUrlError}`);
142
144
  }
@@ -1,6 +1,17 @@
1
1
  const { getClaudeLaunchArgs } = require('./launch-args');
2
2
  const { containsUnsafeTerminalCharacters } = require('./terminal-format');
3
3
 
4
+ function isLoopbackHostname(hostname) {
5
+ const normalizedHostname = hostname.toLowerCase();
6
+ return (
7
+ normalizedHostname === 'localhost' ||
8
+ normalizedHostname.endsWith('.localhost') ||
9
+ normalizedHostname === '::1' ||
10
+ normalizedHostname === '[::1]' ||
11
+ /^127(?:\.\d{1,3}){3}$/.test(normalizedHostname)
12
+ );
13
+ }
14
+
4
15
  const validator = {
5
16
  validateName(name) {
6
17
  if (!name || typeof name !== 'string') {
@@ -75,7 +86,7 @@ const validator = {
75
86
  return null;
76
87
  },
77
88
 
78
- validateUrl(url, required = true) {
89
+ validateUrl(url, required = true, options = {}) {
79
90
  if (url === null || url === undefined || url === '') {
80
91
  return required ? 'URL不能为空' : null;
81
92
  }
@@ -99,21 +110,30 @@ const validator = {
99
110
  return 'URL包含不安全字符';
100
111
  }
101
112
 
102
- const hostname = parsedUrl.hostname.toLowerCase();
103
- const isLoopback =
104
- hostname === 'localhost' ||
105
- hostname.endsWith('.localhost') ||
106
- hostname === '::1' ||
107
- hostname === '[::1]' ||
108
- /^127(?:\.\d{1,3}){3}$/.test(hostname);
109
-
110
- if (parsedUrl.protocol === 'http:' && !isLoopback) {
113
+ if (
114
+ parsedUrl.protocol === 'http:' &&
115
+ !isLoopbackHostname(parsedUrl.hostname) &&
116
+ options.allowInsecureHttp === false
117
+ ) {
111
118
  return '远程 API 地址必须使用 HTTPS;HTTP 仅允许 localhost 或回环地址';
112
119
  }
113
120
 
114
121
  return null;
115
122
  },
116
123
 
124
+ isInsecureRemoteHttp(url) {
125
+ if (typeof url !== 'string') {
126
+ return false;
127
+ }
128
+
129
+ try {
130
+ const parsedUrl = new URL(url);
131
+ return parsedUrl.protocol === 'http:' && !isLoopbackHostname(parsedUrl.hostname);
132
+ } catch {
133
+ return false;
134
+ }
135
+ },
136
+
117
137
  validateToken(token) {
118
138
  if (!token || typeof token !== 'string') {
119
139
  return 'Token不能为空';