Skip to content

Code review with changes#7

Open
akshith271 wants to merge 1 commit into
telematum:mainfrom
akshith271:akshith_review
Open

Code review with changes#7
akshith271 wants to merge 1 commit into
telematum:mainfrom
akshith271:akshith_review

Conversation

@akshith271
Copy link
Copy Markdown

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
[email protected]
+91-9347620298

Comment thread api.go
Comment on lines -12 to -13
name := r.FormValue("name")
email := r.FormValue("email")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing validations here

Comment thread api.go
func setupJsonApi() {
http.HandleFunc("/createUser", func(w http.ResponseWriter, r *http.Request) {
// create mysql connection
conn := createConnection()
Copy link
Copy Markdown
Author

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

Comment thread api.go
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())
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using log is better

Comment thread main.go

func main() {
setupJsonApi()
http.ListenAndServe(":80", nil)
Copy link
Copy Markdown
Author

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

Comment thread utils.go

// 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")
Copy link
Copy Markdown
Author

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.

Comment thread api.go
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())
Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant