Skip to content

Commit 29d8949

Browse files
author
zhangleitao
committed
Renamed function NewFieldRaw() to NewUnsafeFieldRaw()
1 parent a2fe7a4 commit 29d8949

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

field/export.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ func NewField(table, column string, opts ...Option) Field {
3232
return Field{expr: expr{col: toColumn(table, column, opts...)}}
3333
}
3434

35-
// NewFieldRaw create new field by native sql
36-
func NewFieldRaw(rawSQL string, vars ...interface{}) Field {
35+
// NewUnsafeFieldRaw create new field by native sql
36+
//
37+
// Warning: Using NewUnsafeFieldRaw with raw SQL exposes your application to SQL injection vulnerabilities.
38+
// Always validate/sanitize inputs and prefer parameterized queries or NewField methods for field construction.
39+
// Use this low-level function only when absolutely necessary, and ensure any embedded values are properly escaped.
40+
func NewUnsafeFieldRaw(rawSQL string, vars ...interface{}) Field {
3741
return Field{expr: expr{e: clause.Expr{SQL: rawSQL, Vars: vars}}}
3842
}
3943

field/export_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,52 +90,52 @@ func TestExpr_Build(t *testing.T) {
9090
Result: "GROUP_CONCAT(`id`)",
9191
},
9292
{
93-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1"),
93+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1"),
9494
Result: "if(column1=?,column2,column3)",
9595
ExpectedVars: []interface{}{"1"},
9696
},
9797
{
98-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").Eq(p),
98+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1").Eq(p),
9999
Result: "if(column1=?,column2,column3) = ?",
100100
ExpectedVars: []interface{}{"1", p},
101101
},
102102
{
103-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", field.NewField("", "new_id")).Eq(p),
103+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", field.NewField("", "new_id")).Eq(p),
104104
Result: "if(column1=`new_id`,column2,column3) = ?",
105105
ExpectedVars: []interface{}{p},
106106
},
107107
{
108-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").EqCol(field.NewField("", "new_id")),
108+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1").EqCol(field.NewField("", "new_id")),
109109
Result: "if(column1=?,column2,column3) = `new_id`",
110110
ExpectedVars: []interface{}{"1"},
111111
},
112112
{
113-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").EqCol(field.NewField("", "new_id").WithTable("tableB")),
113+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1").EqCol(field.NewField("", "new_id").WithTable("tableB")),
114114
Result: "if(column1=?,column2,column3) = `tableB`.`new_id`",
115115
ExpectedVars: []interface{}{"1"},
116116
},
117117
{
118-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").IsNull(),
118+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1").IsNull(),
119119
Result: "if(column1=?,column2,column3) IS NULL",
120120
ExpectedVars: []interface{}{"1"},
121121
},
122122
{
123-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").GroupConcat(),
123+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1").GroupConcat(),
124124
Result: "GROUP_CONCAT(if(column1=?,column2,column3))",
125125
ExpectedVars: []interface{}{"1"},
126126
},
127127
{
128-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").Desc(),
128+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1").Desc(),
129129
Result: "if(column1=?,column2,column3) DESC",
130130
ExpectedVars: []interface{}{"1"},
131131
},
132132
{
133-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").IfNull(p),
133+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1").IfNull(p),
134134
Result: "IFNULL(if(column1=?,column2,column3),?)",
135135
ExpectedVars: []interface{}{"1", p},
136136
},
137137
{
138-
Expr: field.NewFieldRaw("if(column1=?,column2,column3)", "1").As("column4"),
138+
Expr: field.NewUnsafeFieldRaw("if(column1=?,column2,column3)", "1").As("column4"),
139139
Result: "if(column1=?,column2,column3) AS `column4`",
140140
ExpectedVars: []interface{}{"1"},
141141
},

0 commit comments

Comments
 (0)