New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connection: interpolate json.RawMessage as string #1058
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 <alexsn@fb.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 alexsn@fb.com
Description
On argument interpolation treat
json.RawMessage
as a string instead of[]bytes
to prevents _binary statement injection.Checklist