-
Notifications
You must be signed in to change notification settings - Fork 11
Code review with changes #7
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
base: main
Are you sure you want to change the base?
Conversation
| name := r.FormValue("name") | ||
| email := r.FormValue("email") |
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.
Missing validations here
| func setupJsonApi() { | ||
| http.HandleFunc("/createUser", func(w http.ResponseWriter, r *http.Request) { | ||
| // create mysql connection | ||
| conn := createConnection() |
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.
Creating a common createConnection() is preferable
| email := r.FormValue("email") | ||
| query := "INSERT INTO users (name, email) VALUES (" + name + ", " + email + ")" | ||
| result, err := conn.Exec(query) | ||
| fmt.Println("result ", result, " err ", err.Error()) |
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.
using log is better
|
|
||
| func main() { | ||
| setupJsonApi() | ||
| http.ListenAndServe(":80", nil) |
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.
there is no error handling for http.ListenAndServe
|
|
||
| // createConnection creates a connection to mysql database | ||
| func createConnection() *sql.DB { | ||
| db, err := sql.Open("mysql", "root:password@tcp(127.0.0.1:3306)/test") |
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.
Using constants and encrypted environment variables is more secure.
| email := r.FormValue("email") | ||
| query := "Update users set name=" + name + ", email=" + email + " where id=" + r.FormValue("id") | ||
| result, err := conn.Exec(query) | ||
| fmt.Println("result ", result, " err ", err.Error()) |
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.
returning HTTP errors makes it easy to understand and debug.
Here are some issues I found:
1. SQL Injection Vulnerability:
❌The code directly concatenates user input into the SQL query string. That is not ideal.
✔Use parameterized queries to bind values securely
2. Error Handling:
❌The code does not properly handle potential errors during operations.
✔Add error handling to handle errors effectively, such as returning an error response to the client.
3.Code Duplication:
❌The code for creating a database connection is duplicated in both the createUser and updateUser handlers.
✔ It's better to create a reusable function for establishing a database connection.
4. Global Database Connections:
❌It's generally not recommended to use a global database connection.
✔Passing the database connection as a parameter to functions or using a struct to manage the database connection within a specific scope is better.
5. Lack of defer statements :
❌It is better to close the database connections after the function exits. We have to use defer keyword provided by go
✔We have to use defer keyword provided by go.
Here are some suggestions:
1. Use logging specific packages:
Instead of fmt, log is recommended because we can include additional information like timestamp, log level, source file information. fmt is more general purpose formatting package.
2. Use HTTP status codes:
Using HTTP status codes helps us in providing more meaningful error messages and helps us in debugging.
3. Use constants over direct values.
4. Add validations to the user inputs.
5. Use environment variables.
6. Be specific with the error messages.
Authored By :
Akshith Bharadwaj
akshithbharadwaj2@gmail.com
+91-9347620298