Skip to content

Commit

Permalink
Changes for callback url pattern validation 2x
Browse files Browse the repository at this point in the history
  • Loading branch information
kedarkhaire committed Sep 6, 2023
1 parent 415ca3e commit 5e42934
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/install/apigee_edge.common_app_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ default_products: []
analytics_environment: prod
analytics_available_environments:
- prod
callback_url_pattern: '/^https?:\/\/.*$/'
callback_url_pattern: '^https?:\/\/.*$'
callback_url_pattern_error_message: 'The Callback URL must start with http:// or https://'
callback_url_description: 'External site to which a consumer of this app is redirected to log in when using three-legged OAuth.'
callback_url_placeholder: ''
10 changes: 7 additions & 3 deletions src/Form/AppCallbackUrlSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => $this->t('Pattern'),
'#default_value' => $app_settings->get('callback_url_pattern'),
'#description' => $this->t('<a href="https://www.php.net/manual/en/function.preg-match.php" target="_blank">Regular expression </a> that a Callback URL should match. Default is "/^https?:\/\/.*$/" that ensures callback url starts with either <em>http://</em> or <em>https://</em>.'),
'#description' => $this->t('Regular expression that a Callback URL should match. Default is "^https?:\/\/.*$" that ensures callback url starts with either <em>http://</em> or <em>https://</em>.'),
'#required' => TRUE,
];
$form['callback_url']['pattern_error_message'] = [
Expand Down Expand Up @@ -94,8 +94,12 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);

$isRegExValid = FALSE;
$callback_url_pattern = $form_state->getValue(['callback_url', 'pattern']);
if (str_starts_with($callback_url_pattern, '/') && str_ends_with($callback_url_pattern, '/')) {
$callback_url_pattern = substr($callback_url_pattern, 1, -1);
}
try {
if (@preg_match($form_state->getValue(['callback_url', 'pattern']), '') !== FALSE) {
if (@preg_match('/' . $callback_url_pattern . '/', '') !== FALSE) {
$isRegExValid = TRUE;
}
}
Expand All @@ -104,7 +108,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
}

if (!$isRegExValid) {
$form_state->setError($form['callback_url']['pattern'], $this->t('The pattern should be a valid regular expression. It should /start and end/ with proper delimiters.'));
$form_state->setError($form['callback_url']['pattern'], $this->t('The pattern should be a valid regular expression.'));
}
}

Expand Down

0 comments on commit 5e42934

Please sign in to comment.