feat: escape composite-literal fields in NameOrganizationPair (#26675)

This commit is contained in:
Steven Masley
2026-06-24 14:27:23 -05:00
committed by GitHub
parent 6a57f4751f
commit c08b04adbc
2 changed files with 26 additions and 1 deletions
+23 -1
View File
@@ -294,7 +294,29 @@ func (*NameOrganizationPair) Scan(_ interface{}) error {
//
// SELECT ARRAY[('customrole'::text,'ece79dac-926e-44ca-9790-2ff7c5eb6e0c'::uuid)];
func (a NameOrganizationPair) Value() (driver.Value, error) {
return fmt.Sprintf(`(%s,%s)`, a.Name, a.OrganizationID.String()), nil
// The string values must be escaped in case there are special characters, quotes, etc.
// 'NameOrganizationPair' is a composite value, which has no driver handler
// in the `pq` package.
//
// pq.StringArray formats the single name as `{"<escaped>"}`. Strip
// the outer braces to get the quoted+escaped form that composite
// literal syntax accepts unchanged.
//
// Ideally `appendArrayQuotedBytes` would be exported, and we could call
// it directly.
v, err := (&pq.StringArray{a.Name}).Value()
if err != nil {
return nil, err
}
s, ok := v.(string)
if !ok {
return nil, xerrors.Errorf("unexpected type %T", v)
}
stripCurlyBraces := s[1 : len(s)-1]
return fmt.Sprintf("(%s,%s)", stripCurlyBraces, a.OrganizationID.String()), nil
}
// AgentIDNamePair is used as a result tuple for workspace and agent rows.