initSdk fails and shows WOD if no consumer keys are provided

Created on 27 March 2024, 3 months ago

Problem/Motivation

If there is no consumer key and consumer secret provided, attempting to add a Twitter user via a user's profile will fail fatally. This also occurs when you try to instantiate the SDK via the network manager like so using the plugin.network.manager service:

$networkManager->createInstance('social_post_twitter')->getSdk();

I believe this issue partially stems because the module expects there to be a value for these two fields. The fields are marked as required. However, this does not stop some unsuspecting user trying to add an account anyway which then triggers the White Screen of Death (WOD).

The stacktrace is:

TypeError: Abraham\TwitterOAuth\TwitterOAuth::__construct(): Argument #1 ($consumerKey) must be of type string, null given, called in /app/web/modules/contrib/social_post_twitter/src/Plugin/Network/TwitterPost.php on line 41 in Abraham\TwitterOAuth\TwitterOAuth->__construct() (line 53 of /app/vendor/abraham/twitteroauth/src/TwitterOAuth.php).

Drupal\social_post_twitter\Plugin\Network\TwitterPost->initSdk() (Line: 151)
Drupal\social_api\Plugin\NetworkBase->getSdk() (Line: 127)
Drupal\social_post_twitter\Controller\TwitterPostController->redirectToProvider()
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Steps to reproduce

Install the module and make sure not to enter any values in the configuration form where you provide the consumer API keys. Go to a user account and attempt to add an account. You should encounter the error.

Proposed resolution

I propose that there are guards in the SDK instantiation to ensure the consumer API keys are not empty and also to remove the button to add an account if the SDK cannot be instantiated.

User interface changes

Remove the button if no Twitter API keys have been provided.

API changes

Add guards in the SDK instantiation to ensure the consumer API keys are not empty.

Remaining tasks

N/A

πŸ› Bug report
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom cwales95

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @cwales95
  • πŸ‡¬πŸ‡§United Kingdom cwales95

    I've provided a quick patch that fixes the issues I addressed. initSdk() returns NULL if no keys are provided and the button to add an account no longer shows if the SDK cannot be initialised. It shows a message explaining that keys need added before this can happen.

Production build 0.69.0 2024