Skip to content

Commit

Permalink
Fix path traversal (#274)
Browse files Browse the repository at this point in the history
## What type of PR is this? (check all applicable)

- [ ] Refactor
- [ ] Feature
- [x] Bug Fix
- [ ] Optimization
- [ ] Documentation Update
- [ ] Go Version Update
- [ ] Dependency Update


## Added/updated tests?

- [ ] Yes
- [x] No, and this is why: _no additional tests needed, small fix_
- [ ] I need help with writing tests

## Run verifications and test

- [ ] `make verify` is passing
- [x] `make test` is passing

```
PASS
coverage: 78.1% of statements
ok  	github.com/gorilla/sessions	1.155s	coverage: 78.1% of statements
```
  • Loading branch information
moloch-- committed Apr 17, 2024
1 parent 3eed1c4 commit e308bfd
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions store.go
Expand Up @@ -14,6 +14,11 @@ import (
"github.com/gorilla/securecookie"
)

const (
// File name prefix for session files.
sessionFilePrefix = "session_"
)

// Store is an interface for custom session stores.
//
// See CookieStore and FilesystemStore for examples.
Expand Down Expand Up @@ -257,15 +262,15 @@ func (s *FilesystemStore) save(session *Session) error {
if err != nil {
return err
}
filename := filepath.Join(s.path, "session_"+session.ID)
filename := filepath.Join(s.path, sessionFilePrefix+filepath.Base(session.ID))
fileMutex.Lock()
defer fileMutex.Unlock()
return os.WriteFile(filename, []byte(encoded), 0600)
}

// load reads a file and decodes its content into session.Values.
func (s *FilesystemStore) load(session *Session) error {
filename := filepath.Join(s.path, "session_"+session.ID)
filename := filepath.Join(s.path, sessionFilePrefix+filepath.Base(session.ID))
fileMutex.RLock()
defer fileMutex.RUnlock()
fdata, err := os.ReadFile(filepath.Clean(filename))
Expand All @@ -281,7 +286,7 @@ func (s *FilesystemStore) load(session *Session) error {

// delete session file
func (s *FilesystemStore) erase(session *Session) error {
filename := filepath.Join(s.path, "session_"+session.ID)
filename := filepath.Join(s.path, sessionFilePrefix+filepath.Base(session.ID))

fileMutex.RLock()
defer fileMutex.RUnlock()
Expand Down

0 comments on commit e308bfd

Please sign in to comment.