How to configure Axios for least‑privilege security in browser and Node.js apps
0 reputation · 13 Sept 2024, 23:00 UTC
0 reputation · 13 Sept 2024, 23:00 UTC
When using Axios to make HTTP requests, applying the principle of least privilege helps reduce the attack surface. Which configuration options should be set to limit data size, prevent cross‑site request forgery, and avoid sending unnecessary credentials?
maxContentLength and maxBodyLength to cap response and request body sizes.xsrfCookieName, xsrfHeaderName, and withXSRFToken.withCredentials false unless cross‑site authentication is required.Explain how these settings interact with Axios defaults, instance creation, and interceptors, and provide a minimal example that demonstrates a secure Axios instance.
35275 reputation · 14 Sept 2024, 06:23 UTC
To apply the principle of least privilege when using Axios, set the following options on every instance (or globally via axios.defaults):
Infinity).Infinity).false unless you must send cookies or HTTP auth on a cross‑origin request.These settings should be combined with a strict request‑whitelist interceptor to block any non‑approved endpoints before the network layer is touched.
Defaults
Axios ships with maxContentLength and maxBodyLength set to Infinity, withCredentials set to false, and no XSRF cookie or header names configured. The defaults therefore allow unlimited payloads, no credential leakage, and no automatic XSRF protection.
Instance Creation
Creating an instance with axios.create(config) merges the provided config with the defaults. The values you pass override the defaults for that instance only, keeping other instances unaffected.
Interceptors
Request interceptors run before Axios serialises and sends the request. By throwing or rejecting from the interceptor you can prevent any network traffic to disallowed URLs. Response interceptors can be used to double‑check size limits or to sanitize headers.
// 1️⃣ Create a whitelist of allowed base URLs
const allowedHosts = [
'https://api.myapp.com',
'https://auth.myapp.com'
];
// 2️⃣ Build a secure Axios instance
const secureAxios = axios.create({
// Size limits – 2 MB for responses, 1 MB for requests
maxContentLength: 2 * 1024 * 1024,
maxBodyLength: 1 * 1024 * 1024,
// XSRF protection – server must set a cookie named "XSRF-TOKEN"
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
// Do not send credentials by default
withCredentials: false
});
// 3️⃣ Enforce the whitelist with a request interceptor
secureAxios.interceptors.request.use(request => {
const url = new URL(request.url, request.baseURL || '');
const hostAllowed = allowedHosts.some(base => url.origin === base);
if (!hostAllowed) {
return Promise.reject(new Error(`Disallowed host: ${url.origin}`));
}
return request;
}, error => Promise.reject(error));
// 4️⃣ Example usage
secureAxios.get('/users')
.then(res => console.log('Data:', res.data))
.catch(err => console.error('Request blocked or failed:', err));
Is your application required to send credentials (cookies or HTTP auth) to a third‑party domain? If so, you would need to enable withCredentials for that specific request or instance, and ensure the target domain is on the whitelist.
Use comments to ask for clarification. Post a solution as an answer.
6,550 reputation · 14 Sept 2024, 09:08 UTC
Axios respects baseURL only for relative paths. If the url argument is an absolute URL, Axios sends it directly, bypassing the allow‑listed origin. This enables server‑side request forgery even when baseURL is configured.
url matches an external host not in your whitelist.maxRedirects: 0 combined with validateStatus can block redirect chains that might pivot to internal services.