Skip to content
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

Hide studentIds and tokens in logs #181

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ To run the service locally, follow these steps:

- Clone this repository
```sh
git clone https://github.com/tum-calendar-proxy/tum-calendar-proxy.git
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
git clone https://github.com/TUM-Dev/CalendarProxy.git
```
- Navigate to the project directory:
- Navigate to the project directory:
```sh
cd tum-calendar-proxy
cd CalendarProxy
```
- Run the proxy server:
```sh
Expand Down
41 changes: 40 additions & 1 deletion internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -71,6 +72,44 @@ func newApp() (*App, error) {
return &a, nil
}

func customLogFormatter(params gin.LogFormatterParams) string {
return fmt.Sprintf("[GIN] %v |%s %3d %s | %13v | %15s |%s %-7s%s %#v\n%s",
params.TimeStamp.Format("2006/01/02 - 15:04:05"),
params.StatusCodeColor(),
params.StatusCode,
params.ResetColor(),
params.Latency,
params.ClientIP,
params.MethodColor(),
params.Method,
params.ResetColor(),
hideTokens(params.Path),
params.ErrorMessage,
)
}

func hideTokens(path string) string {
u, err := url.Parse(path)
if err != nil {
return path
}

pStud := u.Query().Get("pStud")
pPers := u.Query().Get("pPers")
pToken := u.Query().Get("pToken")

if pToken == "" || (pStud == "" && pPers == "") {
return path
}

manyXes := strings.Repeat("X", 12)
tokenReplaced := pToken[:4] + manyXes
if pStud != "" {
return fmt.Sprintf("/?pStud=%s&pToken=%s", pStud[:4]+manyXes, tokenReplaced)
}
return fmt.Sprintf("/?pPers=%s&pToken=%s", pPers[:4]+manyXes, tokenReplaced)
Comment on lines +105 to +110
Copy link
Member

Choose a reason for hiding this comment

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

IMO, just dropping all query parameters from the logs works just as well

}

func (a *App) Run() error {
if err := sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/4",
Expand All @@ -93,7 +132,7 @@ func (a *App) Run() error {
gin.SetMode("release")
a.engine = gin.New()
a.engine.Use(sentrygin.New(sentrygin.Options{}))
logger := gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: []string{"/health"}})
logger := gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: []string{"/health"}, Formatter: customLogFormatter})
a.engine.Use(logger, gin.Recovery())
a.configRoutes()

Expand Down
Loading