-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_sql_injection_test.go
80 lines (73 loc) · 1.86 KB
/
database_sql_injection_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package GoDatabase
import (
"database/sql"
"fmt"
"testing"
)
func TestExecQuerySQLInjection(t *testing.T) {
db, ctx := GetConnection()
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
panic(err.Error())
}
}(db)
username := "admin'; #"
password := "salah"
sqlQuery := "SELECT username FROM user WHERE username = '" + username + "' AND password = '" + password + "' LIMIT 1"
// sql injection is possible because the query is not parameterized
fmt.Println("SQL Query:", sqlQuery)
rows, err := db.QueryContext(ctx, sqlQuery)
if err != nil {
panic(err.Error())
}
if rows.Next() {
var username string
err = rows.Scan(&username) // scan the result into the variable, similar to rows.Next
if err != nil {
panic(err.Error())
}
fmt.Println("Login Success!", username)
} else {
fmt.Println("Login Failed!", username)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
panic(err.Error())
}
}(rows)
}
func TestExecQuerySQLInjectionWithParameter(t *testing.T) {
db, ctx := GetConnection()
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
panic(err.Error())
}
}(db)
username := "admin'; #"
password := "salah"
sqlQuery := "SELECT username FROM user WHERE username = ? AND password = ? LIMIT 1" // ? is the placeholder
// sql injection is not possible because the query is parameterized
rows, err := db.QueryContext(ctx, sqlQuery, username, password) // pass the parameter to the query
if err != nil {
panic(err.Error())
}
if rows.Next() {
var username string
err = rows.Scan(&username) // scan the result into the variable, similar to rows.Next
if err != nil {
panic(err.Error())
}
fmt.Println("Login Success!", username)
} else {
fmt.Println("Login Failed!", username)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
panic(err.Error())
}
}(rows)
}