-
Notifications
You must be signed in to change notification settings - Fork 64
Obfuscate credentials before rendering to the front-end #1047
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
+308
−6
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
96f7b22
Add a new CredentialObfuscator class that will take Provider credenti…
dkotter 2a73362
When returning settings to our settings page handler, ensure we pass …
dkotter 96a0710
When settings are saved, ensure we don't save obfuscated credentials
dkotter 01169b6
Merge branch 'develop' into feature/obfuscate-credentials
dkotter b8ec779
Merge branch 'develop' into feature/obfuscate-credentials
dkotter a2aa70d
Ensure our obfuscated string always has at least the minimum number o…
dkotter d0a84c5
Pull the Provider fields we should obfuscate from the ProviderProfile…
dkotter 17c2112
Obfuscate our ClassifAI license key as well
dkotter 077d5f6
Fix fatal error when settings aren't set
dkotter f16a21d
Ensure short values have enough asterisks.
peterwilsoncc 923824c
Merge branch 'develop' into feature/obfuscate-credentials
peterwilsoncc 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| <?php | ||
| /** | ||
| * Helper class for credential obfuscation functionality. | ||
| * | ||
| * @package Classifai | ||
| */ | ||
|
|
||
| namespace Classifai\Providers; | ||
|
|
||
| use Classifai\Providers\ProviderProfiles; | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| /** | ||
| * CredentialObfuscator class. | ||
| * | ||
| * Handles obfuscation of sensitive credentials when displayed in the admin UI | ||
| * and preserves original values when obfuscated values are submitted. | ||
| * | ||
| * @since x.x.x | ||
| */ | ||
| class CredentialObfuscator { | ||
|
|
||
| /** | ||
| * Number of characters to show at the beginning of obfuscated values. | ||
| * | ||
| * @var int | ||
| */ | ||
| const VISIBLE_PREFIX_LENGTH = 8; | ||
|
|
||
| /** | ||
| * Minimum number of consecutive asterisks to detect an obfuscated value. | ||
| * | ||
| * @var int | ||
| */ | ||
| const MIN_ASTERISKS_TO_DETECT = 3; | ||
|
|
||
| /** | ||
| * Obfuscate a credential value. | ||
| * | ||
| * Returns first N characters followed by asterisks. | ||
| * Example: "sk-abc123xyz789" becomes "sk-abc************" | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param string $value The credential value to obfuscate. | ||
| * @return string The obfuscated value, or original if too short. | ||
| */ | ||
| public static function obfuscate( string $value ): string { | ||
| if ( empty( $value ) ) { | ||
| return $value; | ||
| } | ||
|
|
||
| $length = strlen( $value ); | ||
|
|
||
| // If the value is too short, just return asterisks. | ||
| if ( $length <= self::VISIBLE_PREFIX_LENGTH && $length <= self::MIN_ASTERISKS_TO_DETECT ) { | ||
| return str_repeat( '*', self::MIN_ASTERISKS_TO_DETECT ); | ||
| } elseif ( $length <= self::VISIBLE_PREFIX_LENGTH ) { | ||
| return str_repeat( '*', $length ); | ||
| } | ||
|
|
||
| $prefix = substr( $value, 0, self::VISIBLE_PREFIX_LENGTH ); | ||
| $asterisks = str_repeat( '*', $length - self::VISIBLE_PREFIX_LENGTH ); | ||
|
|
||
| // If we don't have enough asterisks, add more. | ||
| if ( strlen( $asterisks ) < self::MIN_ASTERISKS_TO_DETECT ) { | ||
| $asterisks = str_repeat( '*', self::MIN_ASTERISKS_TO_DETECT ); | ||
| } | ||
|
|
||
| return $prefix . $asterisks; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a value appears to be obfuscated. | ||
| * | ||
| * Detects values containing 3 or more consecutive asterisks. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param string $value The value to check. | ||
| * @return bool True if the value appears to be obfuscated. | ||
| */ | ||
| public static function is_obfuscated( string $value ): bool { | ||
| if ( empty( $value ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| $pattern = '/\*{' . self::MIN_ASTERISKS_TO_DETECT . ',}/'; | ||
| return (bool) preg_match( $pattern, $value ); | ||
| } | ||
|
|
||
| /** | ||
| * Check if a field should be obfuscated. | ||
| * | ||
| * Returns false for non-sensitive fields like authenticated, endpoint_url, etc. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param string $field The field name to check. | ||
| * @param string $profile_id The profile ID. | ||
| * @return bool True if the field should be obfuscated. | ||
| */ | ||
| public static function should_obfuscate_field( string $field, string $profile_id ): bool { | ||
| $sensitive_fields = ProviderProfiles::get_sensitive_fields( $profile_id ); | ||
| return in_array( $field, $sensitive_fields, true ); | ||
| } | ||
|
|
||
| /** | ||
| * Obfuscate credential fields for a specific Provider. | ||
| * | ||
| * Uses ProviderProfiles to determine which fields are credentials. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param string $provider_id The Provider ID (e.g., 'openai_chatgpt'). | ||
| * @param array $settings The Provider settings array. | ||
| * @return array The settings with credentials obfuscated. | ||
| */ | ||
| public static function obfuscate_provider_settings( string $provider_id, array $settings ): array { | ||
| $profile_id = ProviderProfiles::get_profile_for_provider( $provider_id ); | ||
|
|
||
| if ( ! $profile_id ) { | ||
| return $settings; | ||
| } | ||
|
|
||
| $credential_fields = ProviderProfiles::get_credential_fields( $profile_id ); | ||
|
|
||
| foreach ( $credential_fields as $field ) { | ||
| if ( | ||
| isset( $settings[ $field ] ) && | ||
| is_string( $settings[ $field ] ) && | ||
| self::should_obfuscate_field( $field, $profile_id ) | ||
| ) { | ||
| $settings[ $field ] = self::obfuscate( $settings[ $field ] ); | ||
| } | ||
| } | ||
|
|
||
| return $settings; | ||
| } | ||
|
|
||
| /** | ||
| * Obfuscate all Provider credentials in Feature settings. | ||
| * | ||
| * Iterates through all Provider settings in the Feature and obfuscates | ||
| * their credential fields. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param array $settings The complete Feature settings array. | ||
| * @return array The settings with all Provider credentials obfuscated. | ||
| */ | ||
| public static function obfuscate_feature_settings( array $settings ): array { | ||
| $profiles = ProviderProfiles::get_all_profiles(); | ||
|
|
||
| // Get all Provider IDs from profiles. | ||
| $all_provider_ids = []; | ||
| foreach ( $profiles as $profile ) { | ||
| $all_provider_ids = array_merge( $all_provider_ids, $profile['provider_ids'] ); | ||
| } | ||
|
|
||
| // Obfuscate credentials for each Provider that has settings. | ||
| foreach ( $settings as $key => $value ) { | ||
| if ( is_array( $value ) && in_array( $key, $all_provider_ids, true ) ) { | ||
| $settings[ $key ] = self::obfuscate_provider_settings( $key, $value ); | ||
| } | ||
| } | ||
|
|
||
| return $settings; | ||
| } | ||
|
|
||
| /** | ||
| * Merge new credentials with existing credentials, preserving originals when obfuscated. | ||
| * | ||
| * If a new value is obfuscated, use the existing value instead. | ||
| * This prevents obfuscated placeholder values from being saved to the database. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param array $new_settings The new settings being saved. | ||
| * @param array $existing_settings The current saved settings. | ||
| * @param string $provider_id The Provider ID. | ||
| * @return array The merged settings. | ||
| */ | ||
| public static function merge_credentials( array $new_settings, array $existing_settings, string $provider_id ): array { | ||
| $profile_id = ProviderProfiles::get_profile_for_provider( $provider_id ); | ||
|
|
||
| if ( ! $profile_id ) { | ||
| return $new_settings; | ||
| } | ||
|
|
||
| $credential_fields = ProviderProfiles::get_credential_fields( $profile_id ); | ||
|
|
||
| foreach ( $credential_fields as $field ) { | ||
| // Skip non-sensitive fields. | ||
| if ( ! self::should_obfuscate_field( $field, $profile_id ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| // If the new value is obfuscated, preserve the existing value. | ||
| if ( | ||
| isset( $new_settings[ $field ] ) && | ||
| is_string( $new_settings[ $field ] ) && | ||
| self::is_obfuscated( $new_settings[ $field ] ) && | ||
| isset( $existing_settings[ $field ] ) | ||
| ) { | ||
| $new_settings[ $field ] = $existing_settings[ $field ]; | ||
| } | ||
| } | ||
|
|
||
| return $new_settings; | ||
| } | ||
|
|
||
| /** | ||
| * Merge credentials for all Providers in Feature settings. | ||
| * | ||
| * Iterates through all Provider settings and preserves existing credentials | ||
| * when obfuscated values are submitted. This ensures switching Providers | ||
| * doesn't save obfuscated values for inactive Providers. | ||
| * | ||
| * @since x.x.x | ||
| * | ||
| * @param array $new_settings The new Feature settings being saved. | ||
| * @param array $existing_settings The current saved Feature settings. | ||
| * @return array The settings with all Provider credentials properly merged. | ||
| */ | ||
| public static function merge_all_provider_credentials( array $new_settings, array $existing_settings ): array { | ||
| $profiles = ProviderProfiles::get_all_profiles(); | ||
|
|
||
| // Get all Provider IDs from profiles. | ||
| $all_provider_ids = []; | ||
| foreach ( $profiles as $profile ) { | ||
| $all_provider_ids = array_merge( $all_provider_ids, $profile['provider_ids'] ); | ||
| } | ||
|
|
||
| // Merge credentials for each Provider that has settings. | ||
| foreach ( $new_settings as $key => $value ) { | ||
| if ( is_array( $value ) && in_array( $key, $all_provider_ids, true ) ) { | ||
| $new_settings[ $key ] = self::merge_credentials( | ||
| $value, | ||
| $existing_settings[ $key ] ?? [], | ||
| $key | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return $new_settings; | ||
| } | ||
| } | ||
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.