Cleartext HTTP endpoints in Axios

Cleartext HTTP endpoints in Axios requests

Description

When Axios sends a request to an http: API, someone on the network path can read or change the request and response. Browsers usually block these requests from HTTPS pages as mixed content before sending them. Check whether the code runs in a browser or on a server.

Potential impact

  • An HTTP request that is sent may expose credentials, tokens or personal data. Its response may be altered in transit.
  • If the browser blocks the request, features that rely on the API may fail.

Remediation

  • Confirm that the API serves the same resources over HTTPS and request the https: URL directly. An HTTP redirect cannot protect a request already sent.
  • Use valid server certificates and current TLS settings, reject cleartext API requests and apply HSTS on browser-facing HTTPS origins.
  • Keep local development addresses in environment-specific settings and check the production bundle for them. Keep certificate checks and browser mixed-content protections enabled.

Examples

Before

tsx
import axios from 'axios';

axios.post('http://api.example.com/login', credentials);

const api = axios.create({
  baseURL: 'http://api.example.com/v1',
});

After

tsx
import axios from 'axios';

axios.post('https://api.example.com/login', credentials);

const api = axios.create({
  baseURL: 'https://api.example.com/v1',
});

The example changes both the login URL in axios.post and the shared Axios instance’s baseURL to HTTPS.

References