Skip to main content

Insecure functionality - Session management

Need

Secure session management

Context

  • Usage of Go 1.16 for building efficient and scalable applications
  • Usage of sessions package from github.com/gin-contrib for managing user sessions in Gin framework
  • Usage of Gin for building web applications in Go

Description

Non compliant code

package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()
store := sessions.NewCookieStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))

r.GET("/home", func(c *gin.Context) {
session := sessions.Default(c)
session.Set("message", "Hello World")
session.Save()
})

r.GET("/check", func(c *gin.Context) {
session := sessions.Default(c)
token := session.Get("message")
if token == nil {
c.JSON(401, gin.H{"error": "unauthorized"})
} else {
c.JSON(200, gin.H{"message": token})
}
})
r.Run(":8080")
}

The above code represents a simple web application built with the Gin framework in Go. It uses the github.com/gin-contrib/sessions package to manage sessions.

The main function initializes a new session store with a secret key and sets up two routes: /home and /check.

The /home route sets a message in the session and saves the session. The /check route retrieves the message from the session. If the message is not found, it responds with a 401 Unauthorized error; otherwise, it responds with a 200 OK status along with the message.

The vulnerability lies in the session management. Currently, the session token's expiration time can be modified. This means an attacker could potentially take an expired session token, modify its expiration time, and continue to use it to make requests to the application. This is a significant security flaw as it allows an unauthorized user to gain access to the application by using an expired session token.

Steps

  • Implement a secure session management mechanism that generates unique and random session tokens.
  • Set an appropriate expiration time for the session tokens to limit their validity period.
  • Use a secure session storage mechanism, such as a database or encrypted cookie store, instead of the default cookie store.
  • Ensure that the session tokens are securely transmitted over HTTPS to prevent interception and tampering.
  • Implement proper session validation and verification mechanisms to prevent unauthorized access or modification of session data.
  • Regularly rotate session tokens to mitigate the risk of session hijacking or token reuse attacks.
  • Implement session invalidation mechanisms, such as logout functionality, to allow users to terminate their sessions.
  • Regularly review and update the session management implementation to address any new vulnerabilities or best practices.

Compliant code

package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/gorilla/securecookie"
)

func main() {
r := gin.Default()
secureCookie := securecookie.New([]byte("secret"), nil)
store := sessions.NewCookieStore(secureCookie)
store.Options(sessions.Options{MaxAge: 3600 * 24, HttpOnly: true, Secure: true}) // Set cookie expiration time and secure attributes
r.Use(sessions.Sessions("mysession", store))

r.GET("/home", func(c *gin.Context) {
session := sessions.Default(c)
session.Set("message", "Hello World")
session.Save()
})

r.GET("/check", func(c *gin.Context) {
session := sessions.Default(c)
if session.IsNew == true {
c.JSON(401, gin.H{"error": "unauthorized"})
} else {
token := session.Get("message")
c.JSON(200, gin.H{"message": token})
}
})
r.Run(":8080")
}

The updated code now uses a secure cookie store for session management. The securecookie package from the Gorilla toolkit is used to create a new secure cookie instance. This instance is then used to create a new cookie store for the session.

The cookie store options are set to ensure that the session cookies are secure. The MaxAge option is set to 24 hours to limit the validity period of the session tokens. The HttpOnly option is set to true to prevent the session cookies from being accessed through client-side scripts, which can help prevent cross-site scripting (XSS) attacks. The Secure option is set to true to ensure that the session cookies are only transmitted over HTTPS.

In the /check route, the IsNew property of the session is checked to determine if the session is new or not. If the session is new, a 401 Unauthorized error is returned. If the session is not new, the session token is retrieved and returned in the response.

This updated implementation provides a more secure session management mechanism that mitigates the risk of session hijacking or token reuse attacks. It also ensures that the session tokens are securely transmitted and stored, and that they cannot be modified or accessed without authorization.

References