connection: interpolate json.RawMessage as string #1058
Conversation
json encoded data is represented as bytes however it should be interpolated as a string. Signed-off-by: Alex Snast <[email protected]>
LGTM. Thanks! |
connection: interpolate json.RawMessage as string (go-sql-driver#1058)
Following go-sql-driver#1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
Following go-sql-driver#1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
Following go-sql-driver#1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
Following go-sql-driver#1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
Following go-sql-driver#1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
Following go-sql-driver#1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
Following #1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
I'm sad this has been merged. This is just a user error to pass a I wrote this function that allows to wrap a json.RawMessage (or any other type that is is serialized as JSON in a DB column), and this doesn't require to patch any SQL drivers. var errInvalidJSONWrapperValue = errors.New("invalid target value for sqlutil.JSON wrapper")
var errInvalidJSONValue = errors.New("invalid value for sqlutil.JSON target")
// JSON is an helper to wrap a value for I/O to a database column as a JSON string
func JSON(p interface{}) driver.Value {
if p == nil {
panic(errInvalidJSONWrapperValue)
}
v := reflect.ValueOf(p)
if v.Kind() == reflect.Ptr && !v.IsNil() && v.Elem().CanSet() {
return jsonScanner{jsonValuer{p}}
} else {
return jsonValuer{p}
}
}
type jsonValuer struct {
target interface{}
}
type jsonScanner struct {
jsonValuer
}
// Value implements interface driver.Valuer for both jsonValuer and jsonScanner
func (j jsonValuer) Value() (driver.Value, error) {
return json.Marshal(j.target)
}
// Scan implements interface sql.Scanner
func (j jsonScanner) Scan(value interface{}) error {
switch value := value.(type) {
case []byte:
return json.Unmarshal(value, j.target)
case string:
return json.Unmarshal([]byte(value), j.target)
default:
target := reflect.ValueOf(j.target)
source := reflect.ValueOf(value)
if value == nil {
source = reflect.Zero(target.Elem().Type())
}
if !source.Type().AssignableTo(target.Elem().Type()) {
return errInvalidJSONValue
}
target.Elem().Set(source)
return nil
}
} |
The fact that we use types from stdlib doesn't add any dependency so I'm not sure what you mean by saying "How the growth of dependencies will stop ...". |
json encoded data is represented as bytes however it should be interpolated as a string Fixes go-sql-driver#819
…l-driver#1059) Following go-sql-driver#1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
json encoded data is represented as bytes however it should be interpolated as a string Fixes go-sql-driver#819
…l-driver#1059) Following go-sql-driver#1058, in order for the driver.Value to get as a json.RawMessage, the converter should accept it as a valid value, and handle it as bytes in case where interpolation is disabled
json encoded data is represented as bytes however it should be
interpolated as a string.
Signed-off-by: Alex Snast [email protected]
Description
On argument interpolation treat
json.RawMessage
as a string instead of[]bytes
to prevents _binary statement injection.Checklist