Skip to content

Commit fe07db0

Browse files
aledbfclaude
andcommitted
fix(cli): print -0 and mask secrets from --secrets-file as the reference
Order a secrets object's keys with the Features package's JS object instead of a second copy of the array-index rules, write -0 as "0" as String() does, and leave empty strings out of the values to mask, as replaceAllLog's length filter does. Pin array-index boundaries, number formatting and masking by length with cases computed by node from the reference's jsonc-parser. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bf3e795 commit fe07db0

3 files changed

Lines changed: 51 additions & 57 deletions

File tree

‎internal/cli/secrets.go‎

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"fmt"
88
"math"
99
"os"
10-
"slices"
1110
"strconv"
1211
"strings"
1312
"syscall"
1413

1514
"github.com/tailscale/hujson"
1615

1716
coreerrors "github.com/devcontainers/cli/internal/errors"
17+
"github.com/devcontainers/cli/internal/features"
1818
)
1919

2020
const secretsFileDescription = "Failed to read/parse secrets"
@@ -126,35 +126,22 @@ func decodeOrdered(dec *json.Decoder) (any, error) {
126126
}
127127

128128
// jsSpreadEntries is the [key, value] list of `{...value}`: an object's own
129-
// keys (a repeated key keeps its first position and its last value; array
130-
// index keys come first, ascending), an array's indexes, a string's
129+
// keys in the order JavaScript enumerates them (a repeated key keeps its
130+
// first position and its last value), an array's indexes, a string's
131131
// characters, and nothing for null, a number or a boolean.
132132
func jsSpreadEntries(value any) fileSecrets {
133133
switch v := value.(type) {
134134
case []secret:
135-
pos := map[string]int{}
135+
names := make([]string, len(v))
136+
last := make(map[string]any, len(v))
137+
for i, m := range v {
138+
names[i] = m.name
139+
last[m.name] = m.value
140+
}
136141
var out fileSecrets
137-
for _, m := range v {
138-
if i, ok := pos[m.name]; ok {
139-
out[i].value = m.value
140-
continue
141-
}
142-
pos[m.name] = len(out)
143-
out = append(out, m)
142+
for _, name := range features.KeyOrder(names) {
143+
out = append(out, secret{name, last[name]})
144144
}
145-
slices.SortStableFunc(out, func(a, b secret) int {
146-
ai, aok := jsArrayIndex(a.name)
147-
bi, bok := jsArrayIndex(b.name)
148-
switch {
149-
case aok && bok:
150-
return compareUint(ai, bi)
151-
case aok:
152-
return -1
153-
case bok:
154-
return 1
155-
}
156-
return 0
157-
})
158145
return out
159146
case []any:
160147
out := make(fileSecrets, len(v))
@@ -172,29 +159,6 @@ func jsSpreadEntries(value any) fileSecrets {
172159
return fileSecrets{}
173160
}
174161

175-
func compareUint(a, b uint64) int {
176-
switch {
177-
case a < b:
178-
return -1
179-
case a > b:
180-
return 1
181-
}
182-
return 0
183-
}
184-
185-
// jsArrayIndex reports whether name is a canonical array index (0 to
186-
// 2^32-2 without leading zeros), which JS enumerates before other keys.
187-
func jsArrayIndex(name string) (uint64, bool) {
188-
if name == "" || (len(name) > 1 && name[0] == '0') {
189-
return 0, false
190-
}
191-
n, err := strconv.ParseUint(name, 10, 64)
192-
if err != nil || n > 1<<32-2 {
193-
return 0, false
194-
}
195-
return n, true
196-
}
197-
198162
func (s fileSecrets) env() []string {
199163
if s == nil {
200164
return nil
@@ -207,17 +171,19 @@ func (s fileSecrets) env() []string {
207171
}
208172

209173
// maskValues is what TS maskSecrets masks: Object.values(secrets) filtered
210-
// by `v.length`, so strings and arrays (as String(array)) are masked and
211-
// numbers, booleans and objects are not. A null value fails there as it
212-
// fails TS ("Cannot read properties of null (reading 'length')").
174+
// by `v.length`, so non-empty strings and arrays (as String(array)) are
175+
// masked and numbers, booleans and objects are not. A null value fails there
176+
// as it fails TS ("Cannot read properties of null (reading 'length')").
213177
func (s fileSecrets) maskValues() ([]string, error) {
214178
var values []string
215179
for _, e := range s {
216180
switch v := e.value.(type) {
217181
case nil:
218182
return nil, errors.New("Cannot read properties of null (reading 'length')")
219183
case string:
220-
values = append(values, v)
184+
if v != "" {
185+
values = append(values, v)
186+
}
221187
case []any:
222188
if len(v) > 0 {
223189
values = append(values, jsString(v))
@@ -238,14 +204,17 @@ func jsString(value any) string {
238204
case json.Number:
239205
// jsonc-parser reads numbers with JS Number(): one past the float64
240206
// range is Infinity, which ParseFloat returns along with ErrRange.
241-
f, _ := strconv.ParseFloat(v.String(), 64)
242-
switch {
243-
case math.IsInf(f, 1):
207+
f, _ := v.Float64()
208+
if math.IsInf(f, 0) {
209+
if math.Signbit(f) {
210+
return "-Infinity"
211+
}
244212
return "Infinity"
245-
case math.IsInf(f, -1):
246-
return "-Infinity"
247213
}
248-
// encoding/json writes a finite float64 as JS does.
214+
if f == 0 {
215+
f = 0 // String(-0) is "0"
216+
}
217+
// encoding/json writes any other finite float64 as JS does.
249218
data, _ := json.Marshal(f)
250219
return string(data)
251220
case []any:

‎internal/cli/secrets_held_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func TestLoadSecretsFileFailsAsTS(t *testing.T) {
5151
{"empty", writeSecretsFile(t, ``), "Invalid json data"},
5252
{"trailing comma", writeSecretsFile(t, `{"A":"1",}`), "Invalid json data"},
5353
{"array trailing comma", writeSecretsFile(t, `[1,2,]`), "Invalid json data"},
54+
{"one-element array trailing comma", writeSecretsFile(t, `{"A":["x",]}`), "Invalid json data"},
55+
{"one-member object trailing comma", writeSecretsFile(t, `{"A":{"x":1,}}`), "Invalid json data"},
5456
{"trailing data", writeSecretsFile(t, `{"A":"1"} x`), "Invalid json data"},
5557
{"bom", writeSecretsFile(t, "\xEF\xBB\xBF{\"A\":\"1\"}"), "Invalid json data"},
5658
{"unquoted name", writeSecretsFile(t, `{A:"1"}`), "Invalid json data"},
@@ -87,6 +89,18 @@ func TestLoadSecretsFileAcceptsWhatJSONCParses(t *testing.T) {
8789
{"number", `5`, []string{}, nil},
8890
{"repeated and index keys", `{"B":"1","2":"x","A":"2","B":"3","1":"y"}`,
8991
[]string{"1=y", "2=x", "B=3", "A=2"}, []string{"y", "x", "3", "2"}},
92+
// Expected values below are what node prints for the reference's
93+
// jsonc.parse followed by `{...secrets}`.
94+
{"array index boundaries",
95+
`{"B":"b","4294967295":"max","4294967294":"top","01":"lead","0":"zero","-1":"neg","99999999999999999999":"long","10":"ten","2":"two"}`,
96+
[]string{"0=zero", "2=two", "10=ten", "4294967294=top", "B=b", "4294967295=max", "01=lead", "-1=neg", "99999999999999999999=long"},
97+
[]string{"zero", "two", "ten", "top", "b", "max", "lead", "neg", "long"}},
98+
{"String of numbers",
99+
`{"Z":-0,"M":-0.0e5,"BIG":1.7976931348623157e308,"NBIG":-1.7976931348623157e308,"I":1e400,"NI":-1e400,"E20":1e20,"E21":1e21,"L":123456789012345678901234,"S":1e-7,"T":0.000001}`,
100+
[]string{"Z=0", "M=0", "BIG=1.7976931348623157e+308", "NBIG=-1.7976931348623157e+308", "I=Infinity", "NI=-Infinity",
101+
"E20=100000000000000000000", "E21=1e+21", "L=1.2345678901234569e+23", "S=1e-7", "T=0.000001"}, nil},
102+
{"masking by length", `{"A":["x"],"N":[null],"NN":[[null,1],null],"E":[],"S":""}`,
103+
[]string{"A=x", "N=", "NN=,1,", "E=", "S="}, []string{"x", "", ",1,"}},
90104
} {
91105
t.Run(tc.name, func(t *testing.T) {
92106
s, err := loadSecretsFile(writeSecretsFile(t, tc.content))

‎internal/features/jsobject.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ func (o *jsObject) order() []string {
7676
return append(idx, rest...)
7777
}
7878

79+
// KeyOrder lists the distinct keys of an object that JavaScript gave the
80+
// properties keys, in that order, as it enumerates them: the integer-index
81+
// keys ascending, then the others where each was first set.
82+
func KeyOrder(keys []string) []string {
83+
o := newJSObject()
84+
for _, k := range keys {
85+
o.set(k, nil)
86+
}
87+
return o.order()
88+
}
89+
7990
func (o *jsObject) MarshalJSON() ([]byte, error) {
8091
var buf bytes.Buffer
8192
buf.WriteByte('{')

0 commit comments

Comments
 (0)