@@ -2,22 +2,26 @@ import fs from "node:fs/promises";
22import os from "node:os" ;
33import path from "node:path" ;
44import { afterEach , describe , expect , it } from "vitest" ;
5- import { replaceDirectoryContents } from "./mirror.js" ;
5+ import {
6+ DEFAULT_OPEN_SHELL_MIRROR_EXCLUDE_DIRS ,
7+ replaceDirectoryContents ,
8+ stageDirectoryContents ,
9+ } from "./mirror.js" ;
10+
11+ const dirs : string [ ] = [ ] ;
12+
13+ async function makeTmpDir ( ) : Promise < string > {
14+ const dir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-mirror-test-" ) ) ;
15+ dirs . push ( dir ) ;
16+ return dir ;
17+ }
18+
19+ afterEach ( async ( ) => {
20+ await Promise . all ( dirs . map ( ( d ) => fs . rm ( d , { recursive : true , force : true } ) ) ) ;
21+ dirs . length = 0 ;
22+ } ) ;
623
724describe ( "replaceDirectoryContents" , ( ) => {
8- const dirs : string [ ] = [ ] ;
9-
10- async function makeTmpDir ( ) : Promise < string > {
11- const dir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-mirror-test-" ) ) ;
12- dirs . push ( dir ) ;
13- return dir ;
14- }
15-
16- afterEach ( async ( ) => {
17- await Promise . all ( dirs . map ( ( d ) => fs . rm ( d , { recursive : true , force : true } ) ) ) ;
18- dirs . length = 0 ;
19- } ) ;
20-
2125 it ( "copies source entries to target" , async ( ) => {
2226 const source = await makeTmpDir ( ) ;
2327 const target = await makeTmpDir ( ) ;
@@ -89,4 +93,90 @@ describe("replaceDirectoryContents", () => {
8993 // "Hooks" (variant case) must still be excluded
9094 await expect ( fs . access ( path . join ( target , "Hooks" ) ) ) . rejects . toThrow ( ) ;
9195 } ) ;
96+
97+ it ( "preserves default excluded directories and repository metadata" , async ( ) => {
98+ const source = await makeTmpDir ( ) ;
99+ const target = await makeTmpDir ( ) ;
100+
101+ await fs . mkdir ( path . join ( source , "hooks" ) , { recursive : true } ) ;
102+ await fs . writeFile ( path . join ( source , "hooks" , "pre-commit" ) , "malicious" ) ;
103+ await fs . mkdir ( path . join ( source , "git-hooks" ) , { recursive : true } ) ;
104+ await fs . writeFile ( path . join ( source , "git-hooks" , "pre-commit" ) , "malicious" ) ;
105+ await fs . mkdir ( path . join ( source , ".git" , "hooks" ) , { recursive : true } ) ;
106+ await fs . writeFile ( path . join ( source , ".git" , "hooks" , "post-checkout" ) , "malicious" ) ;
107+ await fs . writeFile ( path . join ( source , "safe.txt" ) , "ok" ) ;
108+
109+ await fs . mkdir ( path . join ( target , "hooks" ) , { recursive : true } ) ;
110+ await fs . writeFile ( path . join ( target , "hooks" , "trusted" ) , "trusted" ) ;
111+ await fs . mkdir ( path . join ( target , "git-hooks" ) , { recursive : true } ) ;
112+ await fs . writeFile ( path . join ( target , "git-hooks" , "trusted" ) , "trusted" ) ;
113+ await fs . mkdir ( path . join ( target , ".git" ) , { recursive : true } ) ;
114+ await fs . writeFile ( path . join ( target , ".git" , "HEAD" ) , "ref: refs/heads/main\n" ) ;
115+
116+ await replaceDirectoryContents ( {
117+ sourceDir : source ,
118+ targetDir : target ,
119+ excludeDirs : DEFAULT_OPEN_SHELL_MIRROR_EXCLUDE_DIRS ,
120+ } ) ;
121+
122+ expect ( await fs . readFile ( path . join ( target , "safe.txt" ) , "utf8" ) ) . toBe ( "ok" ) ;
123+ expect ( await fs . readFile ( path . join ( target , "hooks" , "trusted" ) , "utf8" ) ) . toBe ( "trusted" ) ;
124+ expect ( await fs . readFile ( path . join ( target , "git-hooks" , "trusted" ) , "utf8" ) ) . toBe ( "trusted" ) ;
125+ expect ( await fs . readFile ( path . join ( target , ".git" , "HEAD" ) , "utf8" ) ) . toBe (
126+ "ref: refs/heads/main\n" ,
127+ ) ;
128+ await expect ( fs . access ( path . join ( target , ".git" , "hooks" , "post-checkout" ) ) ) . rejects . toThrow ( ) ;
129+ } ) ;
130+
131+ it ( "skips symbolic links when copying into the host workspace" , async ( ) => {
132+ const source = await makeTmpDir ( ) ;
133+ const target = await makeTmpDir ( ) ;
134+
135+ await fs . writeFile ( path . join ( source , "safe.txt" ) , "ok" ) ;
136+ await fs . mkdir ( path . join ( source , "nested" ) , { recursive : true } ) ;
137+ await fs . writeFile ( path . join ( source , "nested" , "file.txt" ) , "nested" ) ;
138+ await fs . symlink ( "/tmp/host-secret" , path . join ( source , "escaped-link" ) ) ;
139+ await fs . symlink ( "/tmp/host-secret-dir" , path . join ( source , "nested" , "escaped-dir" ) ) ;
140+
141+ await replaceDirectoryContents ( { sourceDir : source , targetDir : target } ) ;
142+
143+ expect ( await fs . readFile ( path . join ( target , "safe.txt" ) , "utf8" ) ) . toBe ( "ok" ) ;
144+ expect ( await fs . readFile ( path . join ( target , "nested" , "file.txt" ) , "utf8" ) ) . toBe ( "nested" ) ;
145+ await expect ( fs . lstat ( path . join ( target , "escaped-link" ) ) ) . rejects . toThrow ( ) ;
146+ await expect ( fs . lstat ( path . join ( target , "nested" , "escaped-dir" ) ) ) . rejects . toThrow ( ) ;
147+ } ) ;
148+
149+ it ( "preserves existing trusted host symlinks" , async ( ) => {
150+ const source = await makeTmpDir ( ) ;
151+ const target = await makeTmpDir ( ) ;
152+
153+ await fs . writeFile ( path . join ( source , "safe.txt" ) , "ok" ) ;
154+ await fs . writeFile ( path . join ( source , "linked-entry" ) , "remote-plain-file" ) ;
155+ await fs . symlink ( "/tmp/trusted-host-target" , path . join ( target , "linked-entry" ) ) ;
156+
157+ await replaceDirectoryContents ( { sourceDir : source , targetDir : target } ) ;
158+
159+ expect ( await fs . readFile ( path . join ( target , "safe.txt" ) , "utf8" ) ) . toBe ( "ok" ) ;
160+ expect ( await fs . readlink ( path . join ( target , "linked-entry" ) ) ) . toBe ( "/tmp/trusted-host-target" ) ;
161+ } ) ;
162+ } ) ;
163+
164+ describe ( "stageDirectoryContents" , ( ) => {
165+ it ( "stages upload content without symbolic links" , async ( ) => {
166+ const source = await makeTmpDir ( ) ;
167+ const staged = await makeTmpDir ( ) ;
168+
169+ await fs . writeFile ( path . join ( source , "safe.txt" ) , "ok" ) ;
170+ await fs . mkdir ( path . join ( source , "nested" ) , { recursive : true } ) ;
171+ await fs . writeFile ( path . join ( source , "nested" , "file.txt" ) , "nested" ) ;
172+ await fs . symlink ( "/tmp/host-secret" , path . join ( source , "escaped-link" ) ) ;
173+ await fs . symlink ( "/tmp/host-secret-dir" , path . join ( source , "nested" , "escaped-dir" ) ) ;
174+
175+ await stageDirectoryContents ( { sourceDir : source , targetDir : staged } ) ;
176+
177+ expect ( await fs . readFile ( path . join ( staged , "safe.txt" ) , "utf8" ) ) . toBe ( "ok" ) ;
178+ expect ( await fs . readFile ( path . join ( staged , "nested" , "file.txt" ) , "utf8" ) ) . toBe ( "nested" ) ;
179+ await expect ( fs . lstat ( path . join ( staged , "escaped-link" ) ) ) . rejects . toThrow ( ) ;
180+ await expect ( fs . lstat ( path . join ( staged , "nested" , "escaped-dir" ) ) ) . rejects . toThrow ( ) ;
181+ } ) ;
92182} ) ;
0 commit comments