{"record":{"id":"225f19d550c271e9","repo":"cilium/cilium","slug":"failed-to-unquote-string-s-s","errorCode":null,"errorMessage":"Failed to Unquote string: %s\n%s","messagePattern":"Failed to Unquote string: (.+?)\n(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cilium-dbg/cmd/helpers.go","lineNumber":121,"sourceCode":"\t\t}\n\n\t\t// Determine the current indentation\n\t\tfor i := range loc[0] - 1 {\n\t\t\tidx := loc[0] - i - 1\n\t\t\tif resBytes[idx] != ' ' {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tindent = fmt.Sprintf(\"\\t%s\\t\", indent)\n\t\t}\n\n\t\tstringStart := loc[0]\n\t\tstringEnd := loc[1]\n\n\t\t// Unquote the string with the nested json.\n\t\tquotedBytes := resBytes[stringStart:stringEnd]\n\t\tunquoted, err := strconv.Unquote(string(quotedBytes))\n\t\tif err != nil {\n\t\t\treturn bytes.Buffer{}, fmt.Errorf(\"Failed to Unquote string: %s\\n%s\", err.Error(), string(quotedBytes))\n\t\t}\n\n\t\t// Find the JSON within the unquoted string.\n\t\tnestedStart := 0\n\t\tnestedEnd := 0\n\t\t// Find the left-most match\n\t\tif loc = reJSON.FindStringIndex(unquoted); loc != nil {\n\t\t\tnestedStart = loc[0]\n\t\t\tnestedEnd = loc[1]\n\t\t}\n\n\t\t// Decode the nested JSON\n\t\tdecoded := \"\"\n\t\tif nestedEnd != 0 {\n\t\t\tm := make(map[string]any)\n\t\t\tnested := bytes.NewBufferString(unquoted[nestedStart:nestedEnd])\n\t\t\tif err := json.NewDecoder(nested).Decode(&m); err != nil {\n\t\t\t\treturn bytes.Buffer{}, fmt.Errorf(\"Failed to decode nested JSON: %s (\\n%s\\n)\", err.Error(), unquoted[nestedStart:nestedEnd])","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/cilium-dbg/cmd/helpers.go#L103-L139","documentation":"expandNestedJSON in cilium-dbg scans command output for strings containing escaped nested JSON and tries to pretty-print it. It extracts the matched quoted region and calls strconv.Unquote on it; if that region is not a valid Go-quoted string literal, Unquote fails and this error is returned. It indicates the regexp matched something that looks like embedded JSON but is not actually a syntactically valid quoted string.","triggerScenarios":"Calling a cilium-dbg command whose output goes through expandNestedJSON (e.g. `cilium bpf policy get` / endpoint list output) where the regex `\"[^\"\\\\{]*{.*[^\\\\]\"` matches a malformed quoted fragment: unbalanced quotes, invalid escape sequences (e.g. \\x sequences Go's Unquote rejects), or a match spanning truncating output.","commonSituations":"Piping output of cilium-dbg commands whose nested JSON label/annotation data contains unusual escapes; output truncated by terminal width or log rotation mid-string; regex false positives on strings containing braces that are not real JSON strings.","solutions":["Inspect the quoted fragment printed in the error and fix the source string's quoting/escapes at the producing side.","Validate the string with strconv.CanBackquote or strconv.Unquote in a test harness before relying on nested-JSON expansion.","Disable nested JSON expansion (dump output without pretty-print flags / use --output json) so expandNestedJSON is not invoked on the region.","If output is truncated, increase terminal width/capture full output to a file before running the command."],"exampleFix":"// before: string fragment with invalid escape breaks Unquote\nbad := \"[{\\\"labels\\\":\\\"k8s:io.\\x01bad\\\"}]\"\n// after: ensure proper escaping at the producer\nquoted := strconv.Quote(goodJSONString) // valid Go quoted literal\nunquoted, err := strconv.Unquote(quoted)\nif err != nil { return fmt.Errorf(\"invalid quoted JSON: %w\", err) }","handlingStrategy":"validation","validationCode":"func isGoQuoted(s string) bool { _, err := strconv.Unquote(s); return err == nil }\nif !isGoQuoted(fragment) { /* skip expansion or log and fall back to raw output */ }","typeGuard":"func validQuotedJSON(b []byte) bool { u, err := strconv.Unquote(string(b)); return err == nil && json.Valid([]byte(u)) }","tryCatchPattern":"out, err := expandNestedJSON(buf)\nif err != nil {\n    log.Printf(\"nested JSON expansion skipped: %v\", err)\n    out = buf // fall back to raw output\n}","preventionTips":["Feed expandNestedJSON only complete, untruncated command output (capture to file, not a narrow terminal).","Validate candidate regions with strconv.Unquote or json.Valid before expansion in tests.","Prefer `-o json` structured output over text output that requires regex-based nested JSON expansion.","Test with real agent output containing escapes (quotes, newlines) to catch Unquote edge cases."],"tags":["go","cli","json","parsing"],"backgroundTag":"json-unquote-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}