An open API service indexing awesome lists of open source software.

https://github.com/genaker/cloudflare-nodejs-api-gateway

Cloudflare Worker API Proxy Gateway for Magento (Node.js)
https://github.com/genaker/cloudflare-nodejs-api-gateway

Last synced: 4 months ago
JSON representation

Cloudflare Worker API Proxy Gateway for Magento (Node.js)

Awesome Lists containing this project

README

          

# **Cloudflare 2 API Gateway Worker**

A **Cloudflare Worker** to secure and optimize API requests to a **Magento 2 store**. This worker:
- βœ… **Blocks unauthorized API requests** (requires `CF-API-Secret` header).
- βœ… **Implements rate limiting** to prevent abuse.
- βœ… **Restricts access** to whitelisted API endpoints.
- βœ… **Forwards allowed requests** to the Magento API.

---

## ** Supported Platforms**
- Magento 1-2 and forks
- WordPress
- Shopify
- ORO Commerce
- Word Press
- Shopware 6
- Sylius
- ODO
- other platforms

---

## **πŸš€ Features**
| Feature | Status |
|---------|--------|
| **Blocks unauthorized API requests** | βœ… Implemented |
| **Requires `CF-API-Secret` header** | βœ… Implemented |
| **Enforces rate limiting per IP** | βœ… Implemented |
| **Whitelists specific API endpoints** | βœ… Implemented |
| **Prevents Magento API abuse** | βœ… Implemented |

---

## **πŸ“Œ How It Works**
1. **Intercepts API requests** before they reach Magento.
2. **Checks the `CF-API-Secret` header** for authentication.
3. **Validates API paths** to prevent unauthorized access.
4. **Implements rate limiting** per IP.
5. **Forwards valid requests** to Magento's API.

---

## **πŸ“ Deployment Guide**
### **1️⃣ Deploy the Worker in Cloudflare**
1. **Go to Cloudflare Dashboard** β†’ **Workers & Pages**.
2. Click **Create Application** β†’ **Create Worker**.
3. Replace the default script with the worker (below).

---

### **2️⃣ Configure Environment Variables**
| **Variable** | **Default Value** | **Description** |
|-------------------|----------------|----------------|
| `MAGENTO_API_URL` | `"https://default-magento-store.com/rest/V1/"` | Magento API base URL. |
| `MAGENTO_API_KEY` | `"default-api-key"` | Magento API authentication key. |
| `WORKER_SECRET` | `"12345"` | Required `CF-API-Secret` header value. |
| `IP_LIMIT` | `10` | Max API requests per IP per time window. |
| `IP_WINDOW` | `60` | Time window (seconds) for rate limiting. |
| `ALLOWED_PATHS` | `["carts/mine","customers/me","products","orders","categories","payment-information"]` | Whitelisted API endpoints. |

---

## **πŸ“ Worker Code**
Create a file named **`worker.js`** and paste the code from this repo
---

## **πŸ“Œ Testing the Worker**
### **❌ Unauthorized Request (Blocked)**
```sh
curl -X GET "https://your-worker.cloudflare.com/rest/V1/orders"
```
πŸ”΄ **Response:**
```json
{
"message": "403 Forbidden: API access denied"
}
```

---

### **βœ… Authorized Request (Allowed)**
```sh
curl -X GET "https://your-worker.cloudflare.com/rest/V1/orders" -H "CF-API-Secret: 12345"
```
🟒 **Response:**
- **Forwards request** to Magento API.
- **Returns Magento API response**.

---

## **πŸš€ Deployment & Activation**
1️⃣ **Deploy the Worker**
```sh
wrangler deploy
```

2️⃣ **Set Up Environment Variables in Cloudflare**
- **Go to Cloudflare Dashboard** β†’ **Workers & Pages**.
- **Click Settings** β†’ **Add Environment Variables**.
- Add:
```
MAGENTO_API_URL = "https://your-magento-store.com/rest/V1/"
MAGENTO_API_KEY = "your-secure-api-key"
WORKER_SECRET = "super-secret-key"
```

3️⃣ **Activate Worker on Your Domain**
- **Go to Cloudflare Dashboard** β†’ **Workers & Pages**.
- **Set Worker Route:**
```
https://your-worker.cloudflare.com/rest/*
```

---

## **πŸ›‘οΈ Security Recommendations**
- **Change the `WORKER_SECRET` regularly**.
- **Limit API paths to prevent excessive exposure**.
- **Enable rate limiting to prevent API abuse**.

---

# **Magento 2 API Access Control Using Cloudflare Snippet**

## **πŸš€ Overview**
This repository contains a **Cloudflare Snippet** that **secures Magento 2 API access** by enforcing authentication through a custom **`CF-API-Secret`** header. It ensures that only authorized requests can access **Magento REST API endpoints**.

## **πŸ”’ Security Features**
- βœ… **Blocks unauthorized API requests** unless they include the correct `CF-API-Secret` header.
- βœ… **Works at the Cloudflare edge**, preventing unauthorized access before reaching the Magento server.
- βœ… **Lightweight and efficient**, with no impact on Magento’s backend.
- βœ… **Can be combined with additional security headers**.

---

## **πŸ“ Installation & Deployment**
### **1️⃣ Create a Cloudflare Snippet**
1. **Go to Cloudflare Dashboard** β†’ Select your domain.
2. Click **Rules** β†’ **Snippets**.
3. Click **Create Snippet**.
4. Name the snippet: **Magento API Security**.
5. Paste the code from [`snippet.js`](#-snippetjs) below.
6. **Set the snippet rule** to apply to:
```
(http.request.full_uri wildcard "*sub.domain.com/rest/*")
```
7. **Save & Deploy**.