File tree Expand file tree Collapse file tree
src/core/task-persistence Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -801,7 +801,10 @@ export class TaskHistoryStore {
801801 value !== "." &&
802802 value !== ".." &&
803803 ! value . includes ( "/" ) &&
804- ! value . includes ( "\\" )
804+ ! value . includes ( "\\" ) &&
805+ ! value . includes ( ":" ) &&
806+ ! value . includes ( "\u0000" ) &&
807+ ! / [ . ] $ / . test ( value )
805808 )
806809 }
807810
@@ -1271,6 +1274,11 @@ export class TaskHistoryStore {
12711274 * Get the path to a task's `history_item.json` file.
12721275 */
12731276 private async getTaskFilePath ( taskId : string ) : Promise < string > {
1277+ // IDs must be single directory names on every supported OS. This also rejects Windows
1278+ // drive/stream syntax and trailing-dot/space aliases before any history-file access.
1279+ if ( ! this . isSafeTaskId ( taskId ) ) {
1280+ throw new Error ( `Invalid task ID: ${ JSON . stringify ( taskId ) } ` )
1281+ }
12741282 const tasksDir = await this . getTasksDir ( )
12751283 return path . join ( tasksDir , taskId , GlobalFileNames . historyItem )
12761284 }
Original file line number Diff line number Diff line change @@ -529,6 +529,38 @@ describe("TaskHistoryStore", () => {
529529 } )
530530
531531 describe ( "refreshStrict()" , ( ) => {
532+ it . each ( [
533+ "" ,
534+ "." ,
535+ ".." ,
536+ "../outside" ,
537+ "..\\outside" ,
538+ "child/../owner" ,
539+ "/outside" ,
540+ "C:outside" ,
541+ ".. " ,
542+ "child\u0000" ,
543+ ] ) ( "rejects unsafe task ID %j" , async ( taskId ) => {
544+ await store . initialize ( )
545+ store . dispose ( )
546+ await expect ( store . refreshStrict ( taskId ) ) . rejects . toThrow ( "Invalid task ID" )
547+ } )
548+
549+ it ( "does not load an escaped record even when its ID matches the traversal" , async ( ) => {
550+ await store . initialize ( )
551+ store . dispose ( )
552+ const taskId = "../outside"
553+ const outsideDir = path . join ( tmpDir , "outside" )
554+ await fs . mkdir ( outsideDir )
555+ await fs . writeFile (
556+ path . join ( outsideDir , GlobalFileNames . historyItem ) ,
557+ JSON . stringify ( makeHistoryItem ( { id : taskId , status : "completed" } ) ) ,
558+ )
559+
560+ await expect ( store . refreshStrict ( taskId ) ) . rejects . toThrow ( "Invalid task ID" )
561+ expect ( store . get ( taskId ) ) . toBeUndefined ( )
562+ } )
563+
532564 it . each ( [
533565 [ "missing" , undefined ] ,
534566 [ "read-error" , { code : "EISDIR" } ] ,
You can’t perform that action at this time.
0 commit comments