-
Notifications
You must be signed in to change notification settings - Fork 0
Remote Uninstall for Chrome and Vscode #185
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
Open
SanderDeclerck
wants to merge
5
commits into
main
Choose a base branch
from
remote-uninstall-chrome-vscode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d24205f
Remote Uninstall for Chrome and Vscode
SanderDeclerck db272fe
Remove commented code
SanderDeclerck 8f1ef29
Use correct list for open_vsx
SanderDeclerck 774d9dc
Fix code quality comments
SanderDeclerck 33229d6
Move anonymous callback into its own function
SanderDeclerck 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package blocked_packages | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "time" | ||
|
|
||
| "github.com/AikidoSec/safechain-internals/internal/config" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultTimeout = 30 * time.Second | ||
| ) | ||
|
|
||
| var httpClient *http.Client | ||
|
|
||
| func Init() { | ||
| httpClient = &http.Client{ | ||
| Timeout: defaultTimeout, | ||
| } | ||
| } | ||
|
|
||
| func doRequest(ctx context.Context, method, path string, config *config.ConfigInfo, bodyObject any) (*http.Response, error) { | ||
| if config.Token == "" { | ||
| return nil, fmt.Errorf("token is not set") | ||
| } | ||
| if config.DeviceID == "" { | ||
| return nil, fmt.Errorf("device ID is not set") | ||
| } | ||
|
|
||
| url, err := url.JoinPath(config.GetBaseURL(), path) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to build URL for %s: %w", path, err) | ||
| } | ||
|
|
||
| var bodyReader *bytes.Reader | ||
| if bodyObject != nil { | ||
| body, err := json.MarshalIndent(bodyObject, "", " ") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal object: %w", err) | ||
| } | ||
| bodyReader = bytes.NewReader(body) | ||
| } | ||
|
|
||
| var req *http.Request | ||
| if bodyReader != nil { | ||
| req, err = http.NewRequestWithContext(ctx, method, url, bodyReader) | ||
| } else { | ||
| req, err = http.NewRequestWithContext(ctx, method, url, nil) | ||
| } | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create request: %w", err) | ||
| } | ||
| if bodyReader != nil { | ||
| req.Header.Set("Content-Type", "application/json") | ||
| } | ||
| req.Header.Set("Authorization", config.Token) | ||
| req.Header.Set("X-Device-Id", config.DeviceID) | ||
|
|
||
| return httpClient.Do(req) | ||
| } | ||
|
|
||
| type FetchPermissionsResponse struct { | ||
| PermissionGroup PermissionGroup `json:"permission_group"` | ||
| Ecosystems map[string]EcosystemInfo `json:"ecosystems"` | ||
| } | ||
|
|
||
| type PermissionGroup struct { | ||
| ID int `json:"id"` | ||
| Name string `json:"name"` | ||
| } | ||
|
|
||
| type EcosystemInfo struct { | ||
| Exceptions Exceptions `json:"exceptions"` | ||
| } | ||
|
|
||
| type Exceptions struct { | ||
| RejectedPackages []string `json:"rejected_packages"` | ||
| } | ||
|
|
||
| func GetBlockedPackages(ctx context.Context, config *config.ConfigInfo) (*FetchPermissionsResponse, error) { | ||
| resp, err := doRequest(ctx, http.MethodGet, "api/endpoint_protection/callbacks/fetchPermissions", config, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to fetch permissions: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("fetch permissions failed with status %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var result FetchPermissionsResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { | ||
| return nil, fmt.Errorf("failed to decode permissions response: %w", err) | ||
| } | ||
|
|
||
| return &result, nil | ||
| } | ||
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,130 @@ | ||
| package chrome | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/AikidoSec/safechain-internals/internal/platform" | ||
| ) | ||
|
|
||
| type extensionToUninstall struct { | ||
| extensionId string | ||
| profileDir string | ||
| } | ||
|
|
||
| func UninstallBlockedExtensions(ctx context.Context, extensionsToUninstall []string) { | ||
|
SanderDeclerck marked this conversation as resolved.
|
||
| log.Printf("Chrome extension uninstall check (homeDir=%s)", platform.GetConfig().HomeDir) | ||
|
|
||
| installations, err := findInstallations(ctx) | ||
| if err != nil { | ||
| log.Printf("Failed to find Chrome installations for extension uninstall check: %v", err) | ||
| return | ||
| } | ||
| if len(installations) == 0 { | ||
| log.Printf("Chrome extension uninstall check: no installations found") | ||
| } | ||
|
|
||
| extensionBlockSet := make(map[string]struct{}, len(extensionsToUninstall)) | ||
| for _, id := range extensionsToUninstall { | ||
| extensionBlockSet[id] = struct{}{} | ||
| } | ||
| var toUninstall []extensionToUninstall | ||
|
|
||
| for _, inst := range installations { | ||
|
SanderDeclerck marked this conversation as resolved.
|
||
| profiles := findProfilesWithExtensions(inst.DataPath) | ||
| for _, profile := range profiles { | ||
| profileDir := filepath.Join(inst.DataPath, profile) | ||
| extDir := filepath.Join(profileDir, "Extensions") | ||
| entries, err := os.ReadDir(extDir) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| for _, entry := range entries { | ||
| if !entry.IsDir() { | ||
| continue | ||
| } | ||
| if _, blocked := extensionBlockSet[entry.Name()]; blocked { | ||
| toUninstall = append(toUninstall, extensionToUninstall{ | ||
| extensionId: entry.Name(), | ||
| profileDir: profileDir, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(toUninstall) > 0 { | ||
| for _, ext := range toUninstall { | ||
| log.Printf("Uninstalling blocked Chrome extension %s from %s", ext.extensionId, ext.profileDir) | ||
| if err := removeExtension(ext.profileDir, ext.extensionId); err != nil { | ||
| log.Printf("Failed to remove Chrome extension %s: %v", ext.extensionId, err) | ||
| } else { | ||
| log.Printf("Successfully removed Chrome extension %s", ext.extensionId) | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| func removeExtension(profileDir, extensionId string) error { | ||
| if err := os.RemoveAll(filepath.Join(profileDir, "Extensions", extensionId)); err != nil { | ||
| return err | ||
| } | ||
| if err := os.RemoveAll(filepath.Join(profileDir, "Sync Extension Settings", extensionId)); err != nil { | ||
| return err | ||
| } | ||
| if err := removeFromPrefsFile(filepath.Join(profileDir, "Preferences"), extensionId); err != nil { | ||
| return err | ||
| } | ||
| return removeFromPrefsFile(filepath.Join(profileDir, "Secure Preferences"), extensionId) | ||
| } | ||
|
|
||
| func removeFromPrefsFile(prefsPath, extensionId string) error { | ||
|
SanderDeclerck marked this conversation as resolved.
|
||
| data, err := os.ReadFile(prefsPath) | ||
|
SanderDeclerck marked this conversation as resolved.
SanderDeclerck marked this conversation as resolved.
|
||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| var prefs map[string]interface{} | ||
| dec := json.NewDecoder(bytes.NewReader(data)) | ||
| dec.UseNumber() | ||
| if err := dec.Decode(&prefs); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Delete prefs["extensions"]["settings"][extensionId] | ||
| if ext, ok := prefs["extensions"].(map[string]interface{}); ok { | ||
| if settings, ok := ext["settings"].(map[string]interface{}); ok { | ||
| delete(settings, extensionId) | ||
| } | ||
| } | ||
|
|
||
| // Delete prefs["protection"]["macs"]["extensions"]["settings"][extensionId] | ||
| if protection, ok := prefs["protection"].(map[string]interface{}); ok { | ||
| if macs, ok := protection["macs"].(map[string]interface{}); ok { | ||
| if ext, ok := macs["extensions"].(map[string]interface{}); ok { | ||
| if settings, ok := ext["settings"].(map[string]interface{}); ok { | ||
| delete(settings, extensionId) | ||
| } | ||
| if settings, ok := ext["settings_encrypted_hash"].(map[string]interface{}); ok { | ||
| delete(settings, extensionId) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
SanderDeclerck marked this conversation as resolved.
|
||
| out, err := json.Marshal(prefs) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return os.WriteFile(prefsPath, out, 0600) | ||
| } | ||
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,80 @@ | ||
| package vscode | ||
|
|
||
| import ( | ||
| "context" | ||
| "log" | ||
|
|
||
| "github.com/AikidoSec/safechain-internals/internal/platform" | ||
| ) | ||
|
|
||
| type extensionToUninstall struct { | ||
| binaryPath string | ||
| extensionId string | ||
| } | ||
|
|
||
| func UninstallBlockedExtensions(ctx context.Context, vsCodeBlockList []string, openVsxBlockList []string) { | ||
| // Step 1: Collect SBOM - discover all editor installations and their extensions | ||
| installations, err := findInstallations(ctx) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doing this could be very expensive, we usuallly only collect sboms once every 24 hours |
||
| if err != nil { | ||
| log.Printf("Failed to find VSCode installations for extension uninstall check: %v", err) | ||
| return | ||
| } | ||
|
|
||
| if len(installations) == 0 { | ||
| return | ||
| } | ||
|
|
||
| vsCodeBlockSet := make(map[string]struct{}, len(vsCodeBlockList)) | ||
| for _, id := range vsCodeBlockList { | ||
| vsCodeBlockSet[id] = struct{}{} | ||
| } | ||
| openVsxBlockSet := make(map[string]struct{}, len(openVsxBlockList)) | ||
| for _, id := range openVsxBlockList { | ||
| openVsxBlockSet[id] = struct{}{} | ||
| } | ||
|
|
||
| scanner := &VSCodeExtensions{} | ||
| var toUninstall []extensionToUninstall | ||
|
|
||
| // Step 2: Cross-check all installed extensions against the blocklist | ||
| for _, inst := range installations { | ||
| packages, err := scanner.SBOM(ctx, inst) | ||
| if err != nil { | ||
| log.Printf("Failed to collect extensions for %s: %v", inst.Variant, err) | ||
| continue | ||
| } | ||
|
|
||
| var blockSet map[string]struct{} | ||
|
|
||
| switch inst.Ecosystem { | ||
| case "vscode": | ||
| blockSet = vsCodeBlockSet | ||
| case "open_vsx": | ||
| blockSet = openVsxBlockSet | ||
| } | ||
|
|
||
| for _, pkg := range packages { | ||
| if _, blocked := blockSet[pkg.Id]; blocked { | ||
| toUninstall = append(toUninstall, extensionToUninstall{ | ||
| binaryPath: inst.Path, | ||
| extensionId: pkg.Id, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Step 3: Uninstall blocked extensions one by one | ||
| for _, ext := range toUninstall { | ||
| log.Printf("Uninstalling blocked extension %s using %s", ext.extensionId, ext.binaryPath) | ||
| if err := uninstallExtension(ctx, ext.binaryPath, ext.extensionId); err != nil { | ||
| log.Printf("Failed to uninstall extension %s: %v", ext.extensionId, err) | ||
| continue | ||
| } | ||
| log.Printf("Successfully uninstalled extension %s", ext.extensionId) | ||
| } | ||
| } | ||
|
|
||
| func uninstallExtension(ctx context.Context, binaryPath string, extensionId string) error { | ||
| _, err := platform.RunAsCurrentUserWithPathEnv(ctx, binaryPath, "--uninstall-extension", extensionId) | ||
| return err | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be useful to start doing this & the http client building in a helper that's re-usable?