Skip to content

feat: add apps clear command#202

Open
gmegidish wants to merge 1 commit intomainfrom
feat/apps-clear
Open

feat: add apps clear command#202
gmegidish wants to merge 1 commit intomainfrom
feat/apps-clear

Conversation

@gmegidish
Copy link
Copy Markdown
Member

clears app data (cache, preferences, databases) without uninstalling.

android: adb shell pm clear.
ios simulator: simctl get_app_container + filesystem delete.
real ios: not supported.

clears app data (cache, preferences, databases) without uninstalling.
android: adb shell pm clear. ios simulator: simctl get_app_container + filesystem delete. real ios: not supported.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 25, 2026

Walkthrough

This pull request adds support for clearing installed app data across the codebase. The changes include a new CLI command mobilecli apps clear [bundle_id] in the command-line interface, a corresponding ClearAppCommand in the commands package, and implementations of the ClearApp method across device types: Android using adb shell pm clear, iOS Simulator by removing app container contents, real iOS returning an unsupported error, and remote devices via RPC. Supporting infrastructure includes interface updates to ControllableDevice, server-side request handling through AppsClearParams and handleAppsClear, and dispatch registry configuration.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title accurately summarizes the main change: adding a new 'apps clear' command to the codebase, which is the primary focus of the changeset.
Description check ✅ Passed The pull request description is directly related to the changeset, providing implementation details for clearing app data across Android, iOS simulator, and real iOS devices.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/apps-clear

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@devices/simulator.go`:
- Around line 839-855: The code unconditionally deletes everything under
containerPath (from get_app_container output) which is dangerous; before calling
os.RemoveAll in the loop, resolve and sanitize the path (use filepath.Abs and
filepath.Clean on containerPath and each joined path) and verify it is strictly
inside the expected simulator app-data root (e.g., compare that containerPath
has the expected container root prefix with a trailing separator or use
filepath.Rel to ensure the relative path does not start with ".."), also reject
obvious unsafe values (empty string or root "/"); only after these checks
proceed with s.TerminateApp and the RemoveAll calls (references: containerPath,
s.TerminateApp, filepath.Join, os.RemoveAll).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0c82d11c-1377-4228-9089-c8f0d93b2df3

📥 Commits

Reviewing files that changed from the base of the PR and between 079dfc7 and 0349f41.

📒 Files selected for processing (10)
  • README.md
  • cli/apps.go
  • commands/apps.go
  • devices/android.go
  • devices/common.go
  • devices/ios.go
  • devices/remote.go
  • devices/simulator.go
  • server/dispatch.go
  • server/server.go

Comment thread devices/simulator.go
Comment on lines +839 to +855
containerPath := strings.TrimSpace(string(output))
if containerPath == "" {
return fmt.Errorf("no data container found for %s", bundleID)
}

_ = s.TerminateApp(bundleID)

entries, err := os.ReadDir(containerPath)
if err != nil {
return fmt.Errorf("failed to read data container: %w", err)
}

for _, entry := range entries {
if err := os.RemoveAll(filepath.Join(containerPath, entry.Name())); err != nil {
return fmt.Errorf("failed to remove %s: %w", entry.Name(), err)
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Validate container path before recursive deletion.

Lines 839-855 perform recursive deletion based on get_app_container output without constraining the target path. Add a strict prefix/sanity check before os.RemoveAll to prevent accidental deletion outside the simulator app-data container.

🛡️ Suggested hardening patch
 func (s SimulatorDevice) ClearApp(bundleID string) error {
 	output, err := runSimctl("get_app_container", s.UDID, bundleID, "data")
 	if err != nil {
 		return fmt.Errorf("failed to get data container for %s: %w", bundleID, err)
 	}
 
-	containerPath := strings.TrimSpace(string(output))
+	containerPath := filepath.Clean(strings.TrimSpace(string(output)))
 	if containerPath == "" {
 		return fmt.Errorf("no data container found for %s", bundleID)
 	}
+	expectedRoot := filepath.Join(
+		os.Getenv("HOME"),
+		"Library", "Developer", "CoreSimulator", "Devices", s.UDID, "data", "Containers", "Data", "Application",
+	)
+	if containerPath == "." || containerPath == "/" ||
+		!strings.HasPrefix(containerPath+string(os.PathSeparator), expectedRoot+string(os.PathSeparator)) {
+		return fmt.Errorf("refusing to clear unexpected container path: %s", containerPath)
+	}
 
 	_ = s.TerminateApp(bundleID)
 
 	entries, err := os.ReadDir(containerPath)
 	if err != nil {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
containerPath := strings.TrimSpace(string(output))
if containerPath == "" {
return fmt.Errorf("no data container found for %s", bundleID)
}
_ = s.TerminateApp(bundleID)
entries, err := os.ReadDir(containerPath)
if err != nil {
return fmt.Errorf("failed to read data container: %w", err)
}
for _, entry := range entries {
if err := os.RemoveAll(filepath.Join(containerPath, entry.Name())); err != nil {
return fmt.Errorf("failed to remove %s: %w", entry.Name(), err)
}
}
containerPath := filepath.Clean(strings.TrimSpace(string(output)))
if containerPath == "" {
return fmt.Errorf("no data container found for %s", bundleID)
}
expectedRoot := filepath.Join(
os.Getenv("HOME"),
"Library", "Developer", "CoreSimulator", "Devices", s.UDID, "data", "Containers", "Data", "Application",
)
if containerPath == "." || containerPath == "/" ||
!strings.HasPrefix(containerPath+string(os.PathSeparator), expectedRoot+string(os.PathSeparator)) {
return fmt.Errorf("refusing to clear unexpected container path: %s", containerPath)
}
_ = s.TerminateApp(bundleID)
entries, err := os.ReadDir(containerPath)
if err != nil {
return fmt.Errorf("failed to read data container: %w", err)
}
for _, entry := range entries {
if err := os.RemoveAll(filepath.Join(containerPath, entry.Name())); err != nil {
return fmt.Errorf("failed to remove %s: %w", entry.Name(), err)
}
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@devices/simulator.go` around lines 839 - 855, The code unconditionally
deletes everything under containerPath (from get_app_container output) which is
dangerous; before calling os.RemoveAll in the loop, resolve and sanitize the
path (use filepath.Abs and filepath.Clean on containerPath and each joined path)
and verify it is strictly inside the expected simulator app-data root (e.g.,
compare that containerPath has the expected container root prefix with a
trailing separator or use filepath.Rel to ensure the relative path does not
start with ".."), also reject obvious unsafe values (empty string or root "/");
only after these checks proceed with s.TerminateApp and the RemoveAll calls
(references: containerPath, s.TerminateApp, filepath.Join, os.RemoveAll).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant