mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
devvm _proxy takes one flag, -config, and running the proxy is Main's only
mode. What only the host does runs in devvm itself, through typed functions:
- internal/agentlogin (new; imports only fetch, jsonc, proxy and state):
File{Kind, Path, Salt, TokenURL} with VMCopy (the VM's copy and what it
keeps as it is), Bindings, Get (refreshing OAuth tokens), plus Hosts,
MCPSecrets and Redact. The salt is a parameter, not a global or
DEVVM_LOGIN_SALT. Login files are read with proxy.ReadRegular (no FIFO
hang, 64 MiB cap) and written with state.WriteFile; the refresh goes
through fetch.NewClient (HTTPS-only redirects, 30 s, reply capped at
1 MiB) with context.WithoutCancel.
- trust.InitCA makes devvm's CA (state.WriteFileMode), replacing -init-ca;
netpolicy's InitCA field is it, and proxy_ca_failed carries the error.
- internal/jsonc: devcontainer's JSONC stripper, now redaction's too; the
redactor's own copy changed commas inside strings.
- proxy.HostOK is exported for agentlogin's MCP host check.
- secrets.Store loses its Login seam and LoginKept; IO loses Stdin and Kept.
Tests: proxy.txtar's login, redact and init-ca cases become unit tests in
agentlogin and trust; proxy.txtar and TestMainRefuses check the old flags
are refused. New: TestRefreshOutlivesItsCaller, TestRefreshFails,
TestGetRefuses, TestLoginFileNotRegular, TestHosts, TestRedactRefuses,
TestRedactJSONKeepsStrings, TestInitCA, TestInitCAFails, TestBundle.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
// Package jsonc reads JSON with comments, as devcontainer.json and
|
|
// opencode's settings are written: Strip makes it JSON. It is the
|
|
// standard library only.
|
|
package jsonc
|
|
|
|
import "strings"
|
|
|
|
// Strip returns JSON with comments as JSON: its comments, trailing commas
|
|
// and byte order mark blanked with spaces, so what's left is JSON at the
|
|
// same offsets, newlines kept. Anything inside a string stays as it is.
|
|
func Strip(data []byte) []byte {
|
|
out := []byte(string(data))
|
|
if len(out) >= 3 && out[0] == 0xef && out[1] == 0xbb && out[2] == 0xbf {
|
|
out[0], out[1], out[2] = ' ', ' ', ' '
|
|
}
|
|
blank := func(from, to int) {
|
|
for i := from; i < to && i < len(out); i++ {
|
|
if out[i] != '\n' && out[i] != '\r' {
|
|
out[i] = ' '
|
|
}
|
|
}
|
|
}
|
|
// Comments first: a comma before one is still trailing.
|
|
for i := 0; i < len(out); i++ {
|
|
switch {
|
|
case out[i] == '"':
|
|
i = stringEnd(out, i)
|
|
case out[i] == '/' && i+1 < len(out) && out[i+1] == '/':
|
|
end := i
|
|
for end < len(out) && out[end] != '\n' {
|
|
end++
|
|
}
|
|
blank(i, end)
|
|
i = end
|
|
case out[i] == '/' && i+1 < len(out) && out[i+1] == '*':
|
|
end := strings.Index(string(out[i+2:]), "*/")
|
|
if end < 0 {
|
|
// Unterminated: the JSON then ends early, and says so.
|
|
blank(i, len(out))
|
|
return out
|
|
}
|
|
blank(i, i+2+end+2)
|
|
i += 2 + end + 1
|
|
}
|
|
}
|
|
for i := 0; i < len(out); i++ {
|
|
switch out[i] {
|
|
case '"':
|
|
i = stringEnd(out, i)
|
|
case ',':
|
|
j := i + 1
|
|
for j < len(out) && strings.IndexByte(" \t\r\n", out[j]) >= 0 {
|
|
j++
|
|
}
|
|
if j < len(out) && (out[j] == '}' || out[j] == ']') {
|
|
out[i] = ' '
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// stringEnd returns the index of the quote that ends the string starting
|
|
// at start, or the last index when it doesn't end.
|
|
func stringEnd(b []byte, start int) int {
|
|
for i := start + 1; i < len(b); i++ {
|
|
switch b[i] {
|
|
case '\\':
|
|
i++
|
|
case '"':
|
|
return i
|
|
}
|
|
}
|
|
return len(b) - 1
|
|
}
|