@@ -782,12 +782,11 @@ export class CustomModesManager {
782782 const filePath = path . join ( modeRulesDir , entry . name )
783783 const content = await fs . readFile ( filePath , "utf-8" )
784784 if ( content . trim ( ) ) {
785- // Calculate relative path based on mode source
786- const relativePath = isGlobalMode
787- ? path . relative ( baseDir , filePath )
788- : path . relative ( path . join ( baseDir , ".roo" ) , filePath )
785+ // Calculate relative path from within the rules directory
786+ // This excludes the rules-{slug} folder from the path
787+ const relativePath = path . relative ( modeRulesDir , filePath )
789788 // Normalize path to use forward slashes for cross-platform compatibility
790- const normalizedRelativePath = relativePath . toPosix ( )
789+ const normalizedRelativePath = relativePath . replace ( / \\ / g , '/' )
791790 rulesFiles . push ( { relativePath : normalizedRelativePath , content : content . trim ( ) } )
792791 }
793792 }
@@ -883,11 +882,21 @@ export class CustomModesManager {
883882 continue // Skip this file but continue with others
884883 }
885884
886- const targetPath = path . join ( baseDir , normalizedRelativePath )
885+ // Check if path starts with a rules-* folder (old export format)
886+ let cleanedRelativePath = normalizedRelativePath
887+ const rulesMatch = normalizedRelativePath . match ( / ^ r u l e s - [ ^ \/ \\ ] + [ \/ \\ ] / )
888+ if ( rulesMatch ) {
889+ // Strip the entire rules-* folder reference for backwards compatibility
890+ cleanedRelativePath = normalizedRelativePath . substring ( rulesMatch [ 0 ] . length )
891+ logger . info ( `Detected old export format, stripping ${ rulesMatch [ 0 ] } from path` )
892+ }
893+
894+ // Use the rules folder path instead of base directory
895+ const targetPath = path . join ( rulesFolderPath , cleanedRelativePath )
887896 const normalizedTargetPath = path . normalize ( targetPath )
888- const expectedBasePath = path . normalize ( baseDir )
897+ const expectedBasePath = path . normalize ( rulesFolderPath )
889898
890- // Ensure the resolved path stays within the base directory
899+ // Ensure the resolved path stays within the rules folder
891900 if ( ! normalizedTargetPath . startsWith ( expectedBasePath ) ) {
892901 logger . error ( `Path traversal attempt detected: ${ ruleFile . relativePath } ` )
893902 continue // Skip this file but continue with others
0 commit comments