@@ -47,6 +47,9 @@ export function createFileSessionStorage<Data = SessionData, FlashData = Data>({
47
47
48
48
try {
49
49
let file = getFile ( dir , id ) ;
50
+ if ( ! file ) {
51
+ throw new Error ( "Error generating session" ) ;
52
+ }
50
53
await fsp . mkdir ( path . dirname ( file ) , { recursive : true } ) ;
51
54
await fsp . writeFile ( file , content , { encoding : "utf-8" , flag : "wx" } ) ;
52
55
return id ;
@@ -58,6 +61,9 @@ export function createFileSessionStorage<Data = SessionData, FlashData = Data>({
58
61
async readData ( id ) {
59
62
try {
60
63
let file = getFile ( dir , id ) ;
64
+ if ( ! file ) {
65
+ return null ;
66
+ }
61
67
let content = JSON . parse ( await fsp . readFile ( file , "utf-8" ) ) ;
62
68
let data = content . data ;
63
69
let expires =
@@ -81,6 +87,9 @@ export function createFileSessionStorage<Data = SessionData, FlashData = Data>({
81
87
async updateData ( id , data , expires ) {
82
88
let content = JSON . stringify ( { data, expires } ) ;
83
89
let file = getFile ( dir , id ) ;
90
+ if ( ! file ) {
91
+ return ;
92
+ }
84
93
await fsp . mkdir ( path . dirname ( file ) , { recursive : true } ) ;
85
94
await fsp . writeFile ( file , content , "utf-8" ) ;
86
95
} ,
@@ -90,16 +99,24 @@ export function createFileSessionStorage<Data = SessionData, FlashData = Data>({
90
99
if ( ! id ) {
91
100
return ;
92
101
}
102
+ let file = getFile ( dir , id ) ;
103
+ if ( ! file ) {
104
+ return ;
105
+ }
93
106
try {
94
- await fsp . unlink ( getFile ( dir , id ) ) ;
107
+ await fsp . unlink ( file ) ;
95
108
} catch ( error : any ) {
96
109
if ( error . code !== "ENOENT" ) throw error ;
97
110
}
98
111
} ,
99
112
} ) ;
100
113
}
101
114
102
- export function getFile ( dir : string , id : string ) : string {
115
+ export function getFile ( dir : string , id : string ) : string | null {
116
+ if ( ! / ^ [ 0 - 9 a - f ] { 16 } $ / i. test ( id ) ) {
117
+ return null ;
118
+ }
119
+
103
120
// Divide the session id up into a directory (first 2 bytes) and filename
104
121
// (remaining 6 bytes) to reduce the chance of having very large directories,
105
122
// which should speed up file access. This is a maximum of 2^16 directories,
0 commit comments