fix: address Cubic round 5 issues — prototype-pollution guard, URL-encode, JSONC preservation, config-context warning, dynamic config path
This commit is contained in:
parent
be03e27faf
commit
8a2c3cc98d
@ -1,4 +1,4 @@
|
|||||||
import { writeFileSync } from "node:fs"
|
import { readFileSync, writeFileSync } from "node:fs"
|
||||||
import type { ConfigMergeResult, InstallConfig } from "../types"
|
import type { ConfigMergeResult, InstallConfig } from "../types"
|
||||||
import { getConfigDir } from "./config-context"
|
import { getConfigDir } from "./config-context"
|
||||||
import { ensureConfigDirectoryExists } from "./ensure-config-directory-exists"
|
import { ensureConfigDirectoryExists } from "./ensure-config-directory-exists"
|
||||||
@ -45,7 +45,23 @@ export function addProviderConfig(config: InstallConfig): ConfigMergeResult {
|
|||||||
newConfig.provider = providers
|
newConfig.provider = providers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (format === "jsonc") {
|
||||||
|
const content = readFileSync(path, "utf-8")
|
||||||
|
const providerRegex = /"provider"\s*:\s*\{[\s\S]*?\n \}/
|
||||||
|
const providerJson = JSON.stringify(newConfig.provider, null, 2)
|
||||||
|
.split("\n")
|
||||||
|
.map((line, i) => (i === 0 ? line : ` ${line}`))
|
||||||
|
.join("\n")
|
||||||
|
if (providerRegex.test(content)) {
|
||||||
|
const newContent = content.replace(providerRegex, `"provider": ${providerJson}`)
|
||||||
|
writeFileSync(path, newContent)
|
||||||
|
} else {
|
||||||
|
const newContent = content.replace(/(\{)/, `$1\n "provider": ${providerJson},`)
|
||||||
|
writeFileSync(path, newContent)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
writeFileSync(path, JSON.stringify(newConfig, null, 2) + "\n")
|
writeFileSync(path, JSON.stringify(newConfig, null, 2) + "\n")
|
||||||
|
}
|
||||||
return { success: true, configPath: path }
|
return { success: true, configPath: path }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import { parseOpenCodeConfigFileWithError, type OpenCodeConfig } from "./parse-o
|
|||||||
|
|
||||||
export async function fetchLatestVersion(packageName: string): Promise<string | null> {
|
export async function fetchLatestVersion(packageName: string): Promise<string | null> {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`https://registry.npmjs.org/${packageName}/latest`)
|
const res = await fetch(`https://registry.npmjs.org/${encodeURIComponent(packageName)}/latest`)
|
||||||
if (!res.ok) return null
|
if (!res.ok) return null
|
||||||
const data = (await res.json()) as { version: string }
|
const data = (await res.json()) as { version: string }
|
||||||
return data.version
|
return data.version
|
||||||
|
|||||||
@ -39,7 +39,7 @@ export async function runBunInstallWithDetails(): Promise<BunInstallResult> {
|
|||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
timedOut: true,
|
timedOut: true,
|
||||||
error: `bun install timed out after ${BUN_INSTALL_TIMEOUT_SECONDS} seconds. Try running manually: cd ~/.config/opencode && bun i`,
|
error: `bun install timed out after ${BUN_INSTALL_TIMEOUT_SECONDS} seconds. Try running manually: cd ${getConfigDir()} && bun i`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,9 @@ export function initConfigContext(binary: OpenCodeBinaryType, version: string |
|
|||||||
|
|
||||||
export function getConfigContext(): ConfigContext {
|
export function getConfigContext(): ConfigContext {
|
||||||
if (!configContext) {
|
if (!configContext) {
|
||||||
|
if (process.env.NODE_ENV !== "production") {
|
||||||
|
console.warn("[config-context] getConfigContext() called before initConfigContext(); defaulting to CLI paths.")
|
||||||
|
}
|
||||||
const paths = getOpenCodeConfigPaths({ binary: "opencode", version: null })
|
const paths = getOpenCodeConfigPaths({ binary: "opencode", version: null })
|
||||||
configContext = { binary: "opencode", version: null, paths }
|
configContext = { binary: "opencode", version: null, paths }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ export function deepMergeRecord<TTarget extends Record<string, unknown>>(
|
|||||||
const result: TTarget = { ...target }
|
const result: TTarget = { ...target }
|
||||||
|
|
||||||
for (const key of Object.keys(source) as Array<keyof TTarget>) {
|
for (const key of Object.keys(source) as Array<keyof TTarget>) {
|
||||||
|
if (key === "__proto__" || key === "constructor" || key === "prototype") continue
|
||||||
const sourceValue = source[key]
|
const sourceValue = source[key]
|
||||||
const targetValue = result[key]
|
const targetValue = result[key]
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user