Skip to content

Commit cced597

Browse files
committed
feat: add restricted HTTP headers validation and improve Anthropic API configuration
- Add RESTRICTED_HEADERS set to prevent setting forbidden HTTP headers - Implement isRestrictedHeader() method to validate header names before sending requests - Skip restricted headers with warning log during request building - Ensure anthropic-version header is always set for Anthropic provider - Refactor Anthropic API key configuration for better readability - Bump version to 0.4.21
1 parent 6e549fc commit cced597

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

JetBrains/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "com.snow"
8-
version = "0.4.20"
8+
version = "0.4.21"
99

1010
repositories {
1111
mavenCentral()

JetBrains/src/main/kotlin/com/snow/plugin/commit/SnowCommitMessageGenerationService.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ import kotlin.math.min
2828
private const val MAX_DIFF_CHARS = 120_000
2929
private const val API_MAX_RETRIES = 5
3030
private const val API_RETRY_BASE_DELAY_MS = 1_000L
31+
private val RESTRICTED_HEADERS = setOf(
32+
"connection",
33+
"content-length",
34+
"date",
35+
"expect",
36+
"from",
37+
"host",
38+
"upgrade",
39+
"via",
40+
"warning",
41+
)
3142

3243
@Service(Service.Level.PROJECT)
3344
class SnowCommitMessageGenerationService(private val project: Project) {
@@ -310,6 +321,10 @@ class SnowCommitMessageGenerationService(private val project: Project) {
310321
.POST(HttpRequest.BodyPublishers.ofString(body.toString()))
311322

312323
buildHeaders(config, provider).forEach { (key, value) ->
324+
if (isRestrictedHeader(key)) {
325+
logger.warn("Skip restricted header: $key")
326+
return@forEach
327+
}
313328
requestBuilder.header(key, value)
314329
}
315330

@@ -338,12 +353,23 @@ class SnowCommitMessageGenerationService(private val project: Project) {
338353
if (provider == "gemini" && config.apiKey.isNotBlank()) {
339354
headers["x-goog-api-key"] = config.apiKey
340355
}
341-
if (provider == "anthropic" && config.apiKey.isNotBlank()) {
342-
headers["x-api-key"] = config.apiKey
356+
if (provider == "anthropic") {
357+
if (config.apiKey.isNotBlank()) {
358+
headers["x-api-key"] = config.apiKey
359+
}
360+
if (headers.keys.none { it.equals("anthropic-version", ignoreCase = true) }) {
361+
headers["anthropic-version"] = "2023-06-01"
362+
}
343363
}
344364
return headers
345365
}
346366

367+
private fun isRestrictedHeader(name: String): Boolean {
368+
val lower = name.lowercase()
369+
return lower in RESTRICTED_HEADERS
370+
}
371+
372+
347373
private fun loadActiveSnowConfig(): SnowApiConfig {
348374
val configDir = File(System.getProperty("user.home"), ".snow")
349375
val activeProfile = getActiveProfileName(configDir)

0 commit comments

Comments
 (0)