Tmeister / wp-api-jwt-auth

A simple plugin to add JSON Web Token (JWT) Authentication to WP REST API
GNU General Public License v2.0
546 stars 160 forks source link

Fix PHP notices by checking if request headers are set before using #259

Closed nsundberg closed 10 months ago

nsundberg commented 1 year ago

When sending an unauthenticated REST request this plugins generates the following two PHP notices:

PHP Notice:  Undefined index: HTTP_AUTHORIZATION in /wp-content/plugins/jwt-authentication-for-wp-rest-api/public/class-jwt-auth-public.php on line 222
PHP Notice:  Undefined index: REDIRECT_HTTP_AUTHORIZATION in /wp-content/plugins/jwt-authentication-for-wp-rest-api/public/class-jwt-auth-public.php on line 225

This is due to accessing $_SERVER['HTTP_AUTHORIZATION'] and $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] without using isset() first. This PR resolves this by first using empty() in order to check if the headers are set and not empty.

Steps to reproduce the issue

  1. Enable WP_DEBUG.
  2. Send an unauthenticated request to for example /wp-json/, for example by just visiting that endpoint in the URL.
  3. Note the two Undefined index notices.

With the changes in this PR this is resolved.

Tmeister commented 10 months ago

Thank you!