Stop Leaking API Keys: The Backend for Frontend (BFF) Pattern Explained
Blog post from GitGuardian
Frontend applications, such as single-page applications (SPAs), mobile apps, and desktop clients, are inherently insecure for storing secrets like API keys, as they can be easily extracted by users and attackers. The Backend for Frontend (BFF) pattern addresses this issue by introducing a server-side layer between the frontend and third-party APIs, keeping secrets hidden from the frontend. This pattern, initially popularized by SoundCloud, enhances security by ensuring that the frontend communicates only with the BFF, which then interacts with the external services. The BFF can be integrated directly into the frontend project for web apps or exist as a standalone service for mobile apps, offering benefits such as data aggregation and leaner frontend code. Proper BFF implementation involves using a secrets manager for handling sensitive information, securing the connection between the frontend and BFF through cookies, and implementing rate limiting and input validation to prevent abuse. While the BFF adds infrastructure complexity, it provides crucial security advantages, particularly for APIs with financial or administrative implications, by ensuring that secrets are managed within a controlled server environment rather than exposed to the client-side.