11import path from "node:path" ;
22import {
3+ assertNoSymlinkParents ,
34 assertNoSymlinkParentsSync ,
45 readRegularFile ,
56 readRegularFileSync ,
7+ statRegularFile ,
68 statRegularFileSync ,
79} from "openclaw/plugin-sdk/security-runtime" ;
810
@@ -14,22 +16,30 @@ export function resolveWebCredsBackupPath(authDir: string): string {
1416 return path . join ( authDir , "creds.json.bak" ) ;
1517}
1618
17- function assertWebCredsParentPathSafe ( filePath : string ) : void {
19+ function resolveWebCredsParentCheck ( filePath : string ) {
1820 const dir = path . resolve ( path . dirname ( filePath ) ) ;
19- assertNoSymlinkParentsSync ( {
21+ return {
2022 rootDir : path . parse ( dir ) . root ,
2123 targetPath : dir ,
2224 allowMissing : true ,
2325 allowRootChildSymlink : true ,
2426 requireDirectories : true ,
2527 messagePrefix : "WhatsApp credential file path" ,
26- } ) ;
28+ } as const ;
2729}
2830
29- export function assertWebCredsPathRegularFileOrMissing ( filePath : string ) : void {
31+ async function assertWebCredsParentPathSafe ( filePath : string ) : Promise < void > {
32+ await assertNoSymlinkParents ( resolveWebCredsParentCheck ( filePath ) ) ;
33+ }
34+
35+ function assertWebCredsParentPathSafeSync ( filePath : string ) : void {
36+ assertNoSymlinkParentsSync ( resolveWebCredsParentCheck ( filePath ) ) ;
37+ }
38+
39+ export async function assertWebCredsPathRegularFileOrMissing ( filePath : string ) : Promise < void > {
3040 try {
31- assertWebCredsParentPathSafe ( filePath ) ;
32- statRegularFileSync ( filePath ) ;
41+ await assertWebCredsParentPathSafe ( filePath ) ;
42+ await statRegularFile ( filePath ) ;
3343 } catch ( error ) {
3444 throw new Error (
3545 `WhatsApp credential file path is unsafe; creds.json must be a regular file or missing: ${ filePath } ` ,
@@ -38,18 +48,9 @@ export function assertWebCredsPathRegularFileOrMissing(filePath: string): void {
3848 }
3949}
4050
41- export function isWebCredsPathRegularFileOrMissing ( filePath : string ) : boolean {
42- try {
43- assertWebCredsPathRegularFileOrMissing ( filePath ) ;
44- return true ;
45- } catch {
46- return false ;
47- }
48- }
49-
5051export function readWebCredsJsonRawSync ( filePath : string ) : string | null {
5152 try {
52- assertWebCredsParentPathSafe ( filePath ) ;
53+ assertWebCredsParentPathSafeSync ( filePath ) ;
5354 const { buffer, stat } = readRegularFileSync ( {
5455 filePath,
5556 } ) ;
@@ -61,7 +62,7 @@ export function readWebCredsJsonRawSync(filePath: string): string | null {
6162
6263export async function readWebCredsJsonRaw ( filePath : string ) : Promise < string | null > {
6364 try {
64- assertWebCredsParentPathSafe ( filePath ) ;
65+ await assertWebCredsParentPathSafe ( filePath ) ;
6566 const { buffer, stat } = await readRegularFile ( {
6667 filePath,
6768 } ) ;
@@ -73,7 +74,7 @@ export async function readWebCredsJsonRaw(filePath: string): Promise<string | nu
7374
7475export function statWebCredsFileSync ( filePath : string ) : { mtimeMs : number ; size : number } | null {
7576 try {
76- assertWebCredsParentPathSafe ( filePath ) ;
77+ assertWebCredsParentPathSafeSync ( filePath ) ;
7778 const result = statRegularFileSync ( filePath ) ;
7879 if ( result . missing || result . stat . size <= 1 ) {
7980 return null ;
@@ -90,7 +91,7 @@ export function statWebCredsFileSync(filePath: string): { mtimeMs: number; size:
9091export function hasWebCredsRegularFileSync ( authDir : string ) : boolean {
9192 try {
9293 const credsPath = resolveWebCredsPath ( authDir ) ;
93- assertWebCredsParentPathSafe ( credsPath ) ;
94+ assertWebCredsParentPathSafeSync ( credsPath ) ;
9495 return ! statRegularFileSync ( credsPath ) . missing ;
9596 } catch {
9697 return false ;
0 commit comments