hardis:org:diagnose:unsecure-connected-apps
Description
Command Behavior
Detects unsecured Connected Apps and External Client Apps in a Salesforce org and generates detailed reports for security analysis.
This command is a critical security diagnostic tool that helps administrators identify Connected Apps and External Client Apps that may pose security risks due to improper OAuth authorization configuration. It provides comprehensive analysis of OAuth tokens and app security settings to ensure proper access control.
Key functionalities:
- OAuth Token Analysis: Queries all OAuth tokens in the org using SOQL to retrieve comprehensive token information including app names, users, authorization status, and usage statistics.
- Connected App and External Client App Coverage: Checks both Connected Apps (via
AppMenuItem.IsUsingAdminAuthorization) and External Client Apps (viaExtlClntAppOauthPlcyCnfg.PermittedUsersPolicyType) for proper admin pre-approval settings. - App Type Column: Each report row includes an
App Typecolumn indicating whether the app is aConnected ApporExt Client App. - AppName-based Fallback Matching: When an OAuth token has no
AppMenuItemlink (common for External Client App tokens), the command falls back to matching byAppNameagainstExternalClientApplication.MasterLabelorDeveloperName. - Ignore List Support: Skips warning/escalation for apps configured in
monitoringUnsecureConnectedAppsIgnore(project config) orMONITORING_UNSECURE_CONNECTED_APPS_IGNORE(environment variable). Matching OAuth tokens are marked as Ignored. - Unsecured App Detection: Identifies apps that allow users to authorize themselves without admin approval, which can pose security risks.
- Phantom App Cleanup (Optional): Detects unsecured apps not present in the installed Connected Apps or External Client Apps list and offers an interactive option to revoke their OAuth tokens (forces re-authentication if still needed).
- Stale Token Cleanup (Optional): Detects secured apps that still have old unsecured OAuth tokens (authorized before proper hardening) and offers an interactive option to delete them.
- Detailed Reporting: Generates two comprehensive CSV reports:
- OAuth Tokens Report: Lists all OAuth tokens with security status, app type, user information, and usage data
- Connected Apps Summary: Aggregates unsecured apps with counts of associated OAuth tokens and app type
- Visual Indicators: Uses status icons (❌ for unsecured, ✅ for secured, ⚪ for ignored) to provide immediate visual feedback on security status.
- Security Recommendations: Provides actionable guidance on how to secure Connected Apps and External Client Apps through proper configuration.
- Notifications: Sends alerts to configured channels (Grafana, Slack, MS Teams) with security findings and attached reports.
This command is part of sfdx-hardis Monitoring and can output Grafana, Slack and MsTeams Notifications.
Technical explanations
The command's technical implementation involves:
- SOQL Query Execution: Executes a comprehensive SOQL query on the
OauthTokenobject, joining withAppMenuItemandUserobjects to gather complete security context. - Connected App Security Logic: Analyzes the
AppMenuItem.IsUsingAdminAuthorizationfield to determine if a Connected App requires admin pre-approval for user authorization. - External Client App Security Logic: Queries
ExtlClntAppOauthPlcyCnfgfor each External Client App and checksPermittedUsersPolicyType === 'AdminApprovedPreAuthorized'to determine if admin pre-approval is required. Falls back to AppName-based matching whenAppMenuItem.ApplicationIdis not populated. - Ignore Handling: Normalizes app names and marks matching OAuth tokens as Ignored so they do not contribute to unsecured app counts and notifications.
- Data Transformation: Processes raw SOQL results to add security status indicators, app type, and reorganizes data for optimal reporting and analysis.
- Aggregation Processing: Groups OAuth tokens by app name to provide summary statistics and identify the most problematic applications.
- Token Revocation: Optionally calls Salesforce OAuth revoke endpoint using each token's
DeleteTokenvalue to revoke OAuth tokens for selected phantom apps or stale unsecured tokens on secured apps. - Report Generation: Uses
generateCsvFileto create structured CSV reports with proper formatting and metadata for easy analysis and sharing. - Notification Integration: Integrates with the
NotifProviderto send security alerts with detailed metrics, including the number of unsecured apps and associated OAuth tokens. - File Management: Generates multiple output formats (CSV, XLSX) and manages file paths using
generateReportPathfor consistent report organization. - Connection Management: Uses
setConnectionVariablesto ensure proper authentication context for notification providers that require org connection details.
Parameters
| Name | Type | Description | Default | Required | Options |
|---|---|---|---|---|---|
| debug -d |
boolean | Activate debug mode (more logs) | |||
| flags-dir | option | undefined | |||
| json | boolean | Format output as json. | |||
| outputfile -f |
option | Force the path and name of output report file. Must end with .csv | |||
| skipauth | boolean | Skip authentication check when a default username is required | |||
| target-org -o |
option | undefined | |||
| websocket | option | Websocket host:port for VsCode SFDX Hardis UI integration |
Examples
$ sf hardis:org:diagnose:unsecure-connected-apps