-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add logic to prevent auto-approving edits of configuration files #4667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import path from "path" | ||
| import ignore, { Ignore } from "ignore" | ||
|
|
||
| export const SHIELD_SYMBOL = "\u{1F6E1}" | ||
|
|
||
| /** | ||
| * Controls write access to Roo configuration files by enforcing protection patterns. | ||
| * Prevents auto-approved modifications to sensitive Roo configuration files. | ||
| */ | ||
| export class RooProtectedController { | ||
| private cwd: string | ||
| private ignoreInstance: Ignore | ||
|
|
||
| // Predefined list of protected Roo configuration patterns | ||
| private static readonly PROTECTED_PATTERNS = [ | ||
| ".rooignore", | ||
| ".roo/**", | ||
| ".rooprotected", // For future use | ||
| ".roo*", // Any file starting with .roo | ||
| ] | ||
|
|
||
| constructor(cwd: string) { | ||
| this.cwd = cwd | ||
| // Initialize ignore instance with protected patterns | ||
| this.ignoreInstance = ignore() | ||
| this.ignoreInstance.add(RooProtectedController.PROTECTED_PATTERNS) | ||
| } | ||
|
|
||
| /** | ||
| * Check if a file is write-protected | ||
| * @param filePath - Path to check (relative to cwd) | ||
| * @returns true if file is write-protected, false otherwise | ||
| */ | ||
| isWriteProtected(filePath: string): boolean { | ||
| try { | ||
| // Normalize path to be relative to cwd and use forward slashes | ||
| const absolutePath = path.resolve(this.cwd, filePath) | ||
| const relativePath = path.relative(this.cwd, absolutePath).toPosix() | ||
|
|
||
| // Use ignore library to check if file matches any protected pattern | ||
| return this.ignoreInstance.ignores(relativePath) | ||
| } catch (error) { | ||
| // If there's an error processing the path, err on the side of caution | ||
| // Ignore is designed to work with relative file paths, so will throw error for paths outside cwd | ||
| console.error(`Error checking protection for ${filePath}:`, error) | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get set of write-protected files from a list | ||
| * @param paths - Array of paths to filter (relative to cwd) | ||
| * @returns Set of protected file paths | ||
| */ | ||
| getProtectedFiles(paths: string[]): Set<string> { | ||
| const protectedFiles = new Set<string>() | ||
|
|
||
| for (const filePath of paths) { | ||
| if (this.isWriteProtected(filePath)) { | ||
| protectedFiles.add(filePath) | ||
| } | ||
| } | ||
|
|
||
| return protectedFiles | ||
| } | ||
|
|
||
| /** | ||
| * Filter an array of paths, marking which ones are protected | ||
| * @param paths - Array of paths to check (relative to cwd) | ||
| * @returns Array of objects with path and protection status | ||
| */ | ||
| annotatePathsWithProtection(paths: string[]): Array<{ path: string; isProtected: boolean }> { | ||
| return paths.map((filePath) => ({ | ||
| path: filePath, | ||
| isProtected: this.isWriteProtected(filePath), | ||
| })) | ||
| } | ||
|
|
||
| /** | ||
| * Get display message for protected file operations | ||
| */ | ||
| getProtectionMessage(): string { | ||
| return "This is a Roo configuration file and requires approval for modifications" | ||
| } | ||
|
|
||
| /** | ||
| * Get formatted instructions about protected files for the LLM | ||
| * @returns Formatted instructions about file protection | ||
| */ | ||
| getInstructions(): string { | ||
| const patterns = RooProtectedController.PROTECTED_PATTERNS.join(", ") | ||
| return `# Protected Files\n\n(The following Roo configuration file patterns are write-protected and always require approval for modifications, regardless of autoapproval settings. When using list_files, you'll notice a ${SHIELD_SYMBOL} next to files that are write-protected.)\n\nProtected patterns: ${patterns}` | ||
| } | ||
|
|
||
| /** | ||
| * Get the list of protected patterns (for testing/debugging) | ||
| */ | ||
| static getProtectedPatterns(): readonly string[] { | ||
| return RooProtectedController.PROTECTED_PATTERNS | ||
| } | ||
| } | ||
118 changes: 118 additions & 0 deletions
118
src/core/protect/__tests__/RooProtectedController.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import path from "path" | ||
| import { RooProtectedController } from "../RooProtectedController" | ||
|
|
||
| describe("RooProtectedController", () => { | ||
| const TEST_CWD = "/test/workspace" | ||
| let controller: RooProtectedController | ||
|
|
||
| beforeEach(() => { | ||
| controller = new RooProtectedController(TEST_CWD) | ||
| }) | ||
|
|
||
| describe("isWriteProtected", () => { | ||
| it("should protect .rooignore file", () => { | ||
| expect(controller.isWriteProtected(".rooignore")).toBe(true) | ||
| }) | ||
|
|
||
| it("should protect files in .roo directory", () => { | ||
| expect(controller.isWriteProtected(".roo/config.json")).toBe(true) | ||
| expect(controller.isWriteProtected(".roo/settings/user.json")).toBe(true) | ||
| expect(controller.isWriteProtected(".roo/modes/custom.json")).toBe(true) | ||
| }) | ||
|
|
||
| it("should protect .rooprotected file", () => { | ||
| expect(controller.isWriteProtected(".rooprotected")).toBe(true) | ||
| }) | ||
|
|
||
| it("should protect files starting with .roo", () => { | ||
| expect(controller.isWriteProtected(".roosettings")).toBe(true) | ||
| expect(controller.isWriteProtected(".rooconfig")).toBe(true) | ||
| }) | ||
|
|
||
| it("should not protect regular files", () => { | ||
| expect(controller.isWriteProtected("src/index.ts")).toBe(false) | ||
| expect(controller.isWriteProtected("package.json")).toBe(false) | ||
| expect(controller.isWriteProtected("README.md")).toBe(false) | ||
| }) | ||
|
|
||
| it("should not protect files that contain 'roo' but don't start with .roo", () => { | ||
| expect(controller.isWriteProtected("src/roo-utils.ts")).toBe(false) | ||
| expect(controller.isWriteProtected("config/roo.config.js")).toBe(false) | ||
| }) | ||
|
|
||
| it("should handle nested paths correctly", () => { | ||
| expect(controller.isWriteProtected("src/.roo/config.json")).toBe(true) // .roo/** matches anywhere | ||
| expect(controller.isWriteProtected("nested/.rooignore")).toBe(true) // .rooignore matches anywhere by default | ||
| }) | ||
|
|
||
| it("should handle absolute paths by converting to relative", () => { | ||
| const absolutePath = path.join(TEST_CWD, ".rooignore") | ||
| expect(controller.isWriteProtected(absolutePath)).toBe(true) | ||
| }) | ||
|
|
||
| it("should handle paths with different separators", () => { | ||
| expect(controller.isWriteProtected(".roo\\config.json")).toBe(true) | ||
| expect(controller.isWriteProtected(".roo/config.json")).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe("getProtectedFiles", () => { | ||
| it("should return set of protected files from a list", () => { | ||
| const files = ["src/index.ts", ".rooignore", "package.json", ".roo/config.json", "README.md"] | ||
|
|
||
| const protectedFiles = controller.getProtectedFiles(files) | ||
|
|
||
| expect(protectedFiles).toEqual(new Set([".rooignore", ".roo/config.json"])) | ||
| }) | ||
|
|
||
| it("should return empty set when no files are protected", () => { | ||
| const files = ["src/index.ts", "package.json", "README.md"] | ||
|
|
||
| const protectedFiles = controller.getProtectedFiles(files) | ||
|
|
||
| expect(protectedFiles).toEqual(new Set()) | ||
| }) | ||
| }) | ||
|
|
||
| describe("annotatePathsWithProtection", () => { | ||
| it("should annotate paths with protection status", () => { | ||
| const files = ["src/index.ts", ".rooignore", ".roo/config.json", "package.json"] | ||
|
|
||
| const annotated = controller.annotatePathsWithProtection(files) | ||
|
|
||
| expect(annotated).toEqual([ | ||
| { path: "src/index.ts", isProtected: false }, | ||
| { path: ".rooignore", isProtected: true }, | ||
| { path: ".roo/config.json", isProtected: true }, | ||
| { path: "package.json", isProtected: false }, | ||
| ]) | ||
| }) | ||
| }) | ||
|
|
||
| describe("getProtectionMessage", () => { | ||
| it("should return appropriate protection message", () => { | ||
| const message = controller.getProtectionMessage() | ||
| expect(message).toBe("This is a Roo configuration file and requires approval for modifications") | ||
| }) | ||
| }) | ||
|
|
||
| describe("getInstructions", () => { | ||
| it("should return formatted instructions about protected files", () => { | ||
| const instructions = controller.getInstructions() | ||
|
|
||
| expect(instructions).toContain("# Protected Files") | ||
| expect(instructions).toContain("write-protected") | ||
| expect(instructions).toContain(".rooignore") | ||
| expect(instructions).toContain(".roo/**") | ||
| expect(instructions).toContain("\u{1F6E1}") // Shield symbol | ||
| }) | ||
| }) | ||
|
|
||
| describe("getProtectedPatterns", () => { | ||
| it("should return the list of protected patterns", () => { | ||
| const patterns = RooProtectedController.getProtectedPatterns() | ||
|
|
||
| expect(patterns).toEqual([".rooignore", ".roo/**", ".rooprotected", ".roo*"]) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.