Skip to content

Commit 57a8d1b

Browse files
pappzelithrar
authored andcommitted
[feat] Delete store file when cookies max-age <= 0 (#93)
* Delete store file in case if cookies max-age < 0 * Improve the file path definition in erase function * Protect the session file with mutex in delete func * Delete filesystem session if max-age is <= 0 * Add tests for filesystem store delete function * Extend the doc with the file session deletion. * format source code in store_test.go
1 parent 7ab2742 commit 57a8d1b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

store.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,22 @@ func (s *FilesystemStore) New(r *http.Request, name string) (*Session, error) {
205205
}
206206

207207
// Save adds a single session to the response.
208+
//
209+
// If the Options.MaxAge of the session is <= 0 then the session file will be
210+
// deleted from the store path. With this process it enforces the properly
211+
// session cookie handling so no need to trust in the cookie management in the
212+
// web browser.
208213
func (s *FilesystemStore) Save(r *http.Request, w http.ResponseWriter,
209214
session *Session) error {
215+
// Delete if max-age is <= 0
216+
if session.Options.MaxAge <= 0 {
217+
if err := s.erase(session); err != nil {
218+
return err
219+
}
220+
http.SetCookie(w, NewCookie(session.Name(), "", session.Options))
221+
return nil
222+
}
223+
210224
if session.ID == "" {
211225
// Because the ID is used in the filename, encode it to
212226
// use alphanumeric characters only.
@@ -268,3 +282,14 @@ func (s *FilesystemStore) load(session *Session) error {
268282
}
269283
return nil
270284
}
285+
286+
// delete session file
287+
func (s *FilesystemStore) erase(session *Session) error {
288+
filename := filepath.Join(s.path, "session_"+session.ID)
289+
290+
fileMutex.RLock()
291+
defer fileMutex.RUnlock()
292+
293+
err := os.Remove(filename)
294+
return err
295+
}

store_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,55 @@ func TestGH2MaxLength(t *testing.T) {
7171
t.Fatal("failed to Save:", err)
7272
}
7373
}
74+
75+
// Test delete filesystem store with max-age: -1
76+
func TestGH8FilesystemStoreDelete(t *testing.T) {
77+
store := NewFilesystemStore("", []byte("some key"))
78+
req, err := http.NewRequest("GET", "http://www.example.com", nil)
79+
if err != nil {
80+
t.Fatal("failed to create request", err)
81+
}
82+
w := httptest.NewRecorder()
83+
84+
session, err := store.New(req, "hello")
85+
if err != nil {
86+
t.Fatal("failed to create session", err)
87+
}
88+
89+
err = session.Save(req, w)
90+
if err != nil {
91+
t.Fatal("failed to save session", err)
92+
}
93+
94+
session.Options.MaxAge = -1
95+
err = session.Save(req, w)
96+
if err != nil {
97+
t.Fatal("failed to delete session", err)
98+
}
99+
}
100+
101+
// Test delete filesystem store with max-age: 0
102+
func TestGH8FilesystemStoreDelete2(t *testing.T) {
103+
store := NewFilesystemStore("", []byte("some key"))
104+
req, err := http.NewRequest("GET", "http://www.example.com", nil)
105+
if err != nil {
106+
t.Fatal("failed to create request", err)
107+
}
108+
w := httptest.NewRecorder()
109+
110+
session, err := store.New(req, "hello")
111+
if err != nil {
112+
t.Fatal("failed to create session", err)
113+
}
114+
115+
err = session.Save(req, w)
116+
if err != nil {
117+
t.Fatal("failed to save session", err)
118+
}
119+
120+
session.Options.MaxAge = 0
121+
err = session.Save(req, w)
122+
if err != nil {
123+
t.Fatal("failed to delete session", err)
124+
}
125+
}

0 commit comments

Comments
 (0)