Skip to content

Commit b223dfb

Browse files
committed
implement security and infrastructure improvements
1 parent 87e24b5 commit b223dfb

547 files changed

Lines changed: 17213 additions & 4864 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/.env.example

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Backend Server Configuration
2+
# Copy this to .env and customize for your environment
3+
4+
# Server Configuration
5+
PORT=3000
6+
NODE_ENV=development
7+
8+
# Rate Limiting Configuration
9+
# Global rate limiter: 100 requests per 15 minutes (900000 ms)
10+
RATE_LIMIT_WINDOW_MS=900000
11+
RATE_LIMIT_MAX_REQUESTS=100
12+
13+
# API endpoint rate limiter: 30 requests per minute (60000 ms)
14+
API_RATE_LIMIT_WINDOW_MS=60000
15+
API_RATE_LIMIT_MAX_REQUESTS=30
16+
17+
# Stellar RPC Configuration
18+
STELLAR_RPC_URL=https://soroban-testnet.stellar.org
19+
STELLAR_NETWORK=testnet
20+
STELLAR_NETWORK_PASSPHRASE=Test SDF Network ; September 2015
21+
22+
# Vault Configuration
23+
VAULT_CONTRACT_ID=
24+
25+
# Optional: Database Configuration (for future use)
26+
# DATABASE_URL=
27+
# DATABASE_POOL_SIZE=10
28+
29+
# Optional: Cache Configuration (for future use)
30+
# REDIS_URL=redis://localhost:6379
31+
# CACHE_TTL=300

backend/.eslintrc.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"es2020": true,
5+
"jest": true
6+
},
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/recommended"
10+
],
11+
"parser": "@typescript-eslint/parser",
12+
"parserOptions": {
13+
"ecmaVersion": 2020,
14+
"sourceType": "module"
15+
},
16+
"plugins": [
17+
"@typescript-eslint"
18+
],
19+
"rules": {
20+
"@typescript-eslint/explicit-function-return-types": "warn",
21+
"@typescript-eslint/no-explicit-any": "warn",
22+
"@typescript-eslint/no-unused-vars": [
23+
"error",
24+
{
25+
"argsIgnorePattern": "^_"
26+
}
27+
],
28+
"no-console": [
29+
"warn",
30+
{
31+
"allow": [
32+
"warn",
33+
"error",
34+
"log"
35+
]
36+
}
37+
]
38+
}
39+
}

backend/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
node_modules/
2+
dist/
3+
.env
4+
.env.local
5+
.env.*.local
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
.DS_Store
11+
coverage/
12+
.nyc_output/
13+
build/
14+
.vscode/
15+
.idea/
16+
*.swp
17+
*.swo
18+
*~
19+
.cache/

backend/.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "es5",
4+
"singleQuote": true,
5+
"printWidth": 100,
6+
"tabWidth": 2,
7+
"useTabs": false
8+
}

backend/README.md

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# YieldVault Backend API
2+
3+
Express.js backend server for YieldVault Stellar RWA platform with rate limiting and health monitoring.
4+
5+
## Features
6+
7+
- **Health Check Endpoint** (`/health`) - Real-time service health status
8+
- **Readiness Endpoint** (`/ready`) - Dependency status for deployment orchestration
9+
- **Rate Limiting** - Per-IP and per-API-key rate limiting to prevent abuse
10+
- **Dependency Monitoring** - Checks for cache and Stellar RPC availability
11+
- **Error Handling** - Consistent JSON error responses
12+
- **TypeScript** - Full type safety with TypeScript
13+
14+
## Quick Start
15+
16+
### Setup
17+
18+
```bash
19+
# Install dependencies
20+
npm install
21+
22+
# Create environment file
23+
cp .env.example .env
24+
```
25+
26+
### Development
27+
28+
```bash
29+
# Start development server with auto-reload
30+
npm run dev
31+
```
32+
33+
The server will start on `http://localhost:3000`.
34+
35+
### Production
36+
37+
```bash
38+
# Build TypeScript
39+
npm run build
40+
41+
# Start production server
42+
npm start
43+
```
44+
45+
## Configuration
46+
47+
Rate limiting and other settings are configurable via environment variables:
48+
49+
| Variable | Default | Description |
50+
|----------|---------|-------------|
51+
| `PORT` | 3000 | Server port |
52+
| `NODE_ENV` | development | Environment mode |
53+
| `RATE_LIMIT_WINDOW_MS` | 900000 | Global rate limit window (15 min) |
54+
| `RATE_LIMIT_MAX_REQUESTS` | 100 | Global requests per window |
55+
| `API_RATE_LIMIT_WINDOW_MS` | 60000 | API rate limit window (1 min) |
56+
| `API_RATE_LIMIT_MAX_REQUESTS` | 30 | API requests per window |
57+
| `STELLAR_RPC_URL` | https://soroban-testnet.stellar.org | Stellar RPC endpoint |
58+
59+
## API Endpoints
60+
61+
### Health Check
62+
63+
```
64+
GET /health
65+
```
66+
67+
Returns service health status with dependency checks.
68+
69+
**Response (200 OK):**
70+
```json
71+
{
72+
"status": "healthy",
73+
"timestamp": "2026-03-26T10:30:00.000Z",
74+
"uptime": 3600.5,
75+
"environment": "development",
76+
"checks": {
77+
"api": "up",
78+
"cache": "up",
79+
"stellarRpc": "up"
80+
}
81+
}
82+
```
83+
84+
### Readiness Check
85+
86+
```
87+
GET /ready
88+
```
89+
90+
Returns service readiness state. Checks all critical dependencies before reporting ready.
91+
92+
**Response (200 OK - Ready):**
93+
```json
94+
{
95+
"ready": true,
96+
"timestamp": "2026-03-26T10:30:00.000Z",
97+
"dependencies": {
98+
"cache": true,
99+
"stellarRpc": true
100+
}
101+
}
102+
```
103+
104+
**Response (503 Unavailable - Not Ready):**
105+
```json
106+
{
107+
"ready": false,
108+
"timestamp": "2026-03-26T10:30:00.000Z",
109+
"dependencies": {
110+
"cache": false,
111+
"stellarRpc": false
112+
}
113+
}
114+
```
115+
116+
### Rate Limit Exceeded
117+
118+
```
119+
Status: 429 Too Many Requests
120+
```
121+
122+
```json
123+
{
124+
"error": "Too many requests",
125+
"status": 429,
126+
"message": "Rate limit exceeded. Please try again later.",
127+
"retryAfter": 1711432200000
128+
}
129+
```
130+
131+
## Rate Limiting
132+
133+
### Global Rate Limiting
134+
135+
Applied to all requests except `/health` and `/ready`:
136+
- Window: 15 minutes (configurable)
137+
- Max: 100 requests per window (configurable)
138+
- Per: IP address
139+
140+
### API Endpoint Rate Limiting
141+
142+
Stricter limits for API endpoints (e.g., `/api/vault/summary`):
143+
- Window: 1 minute (configurable)
144+
- Max: 30 requests per window (configurable)
145+
- Per: API key (from `x-api-key` header) or IP address
146+
147+
## Testing
148+
149+
```bash
150+
# Run all tests
151+
npm test
152+
153+
# Run tests in watch mode
154+
npm test -- --watch
155+
156+
# Run with coverage
157+
npm test -- --coverage
158+
```
159+
160+
## Issues Addressed
161+
162+
### Issue #145: Rate Limiting
163+
- ✅ Global rate limiting per IP
164+
- ✅ Per-user/API-key rate limiting
165+
- ✅ Configurable via environment variables
166+
- ✅ Clear 429 responses with retry information
167+
- ✅ Tests included for rate limiting behavior
168+
169+
### Issue #148: Health & Readiness Endpoints
170+
-`/health` endpoint for service health
171+
-`/ready` endpoint for deployment readiness
172+
- ✅ Dependency health checks (cache, RPC)
173+
- ✅ CI smoke test setup via npm scripts
174+
- ✅ Consistent response formats
175+
176+
## CI/CD Integration
177+
178+
### Smoke Test (CI Pipeline)
179+
180+
```bash
181+
# Build and start server
182+
npm run test:smoke
183+
184+
# The server will start in background, ready for health checks
185+
# Call: curl http://localhost:3000/health
186+
# Call: curl http://localhost:3000/ready
187+
```
188+
189+
### Docker Deployment
190+
191+
Example Dockerfile:
192+
193+
```dockerfile
194+
FROM node:20-alpine
195+
WORKDIR /app
196+
COPY package*.json ./
197+
RUN npm ci --only=production
198+
COPY dist ./dist
199+
EXPOSE 3000
200+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
201+
CMD node -e "require('http').get('http://localhost:3000/health', (r) => r.statusCode === 200 ? process.exit(0) : process.exit(1))"
202+
CMD ["npm", "start"]
203+
```
204+
205+
## Monitoring
206+
207+
Headers returned in responses:
208+
209+
- `RateLimit-Limit` - Request limit
210+
- `RateLimit-Remaining` - Requests remaining
211+
- `RateLimit-Reset` - Reset timestamp
212+
213+
Example:
214+
```
215+
RateLimit-Limit: 100
216+
RateLimit-Remaining: 95
217+
RateLimit-Reset: 1711432200
218+
```
219+
220+
## License
221+
222+
MIT

backend/jest.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
roots: ['<rootDir>/src'],
5+
testMatch: ['**/__tests__/**/*.test.ts'],
6+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
7+
collectCoverageFrom: [
8+
'src/**/*.ts',
9+
'!src/**/*.d.ts',
10+
'!src/__tests__/**',
11+
],
12+
coverageThreshold: {
13+
global: {
14+
branches: 50,
15+
functions: 50,
16+
lines: 50,
17+
statements: 50,
18+
},
19+
},
20+
setupFilesAfterEnv: ['<rootDir>/src/__tests__/setup.ts'],
21+
};

backend/package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "yieldvault-backend",
3+
"version": "1.0.0",
4+
"description": "YieldVault Stellar RWA Backend API",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"dev": "tsx watch src/index.ts",
8+
"build": "tsc",
9+
"start": "node dist/index.js",
10+
"test": "jest",
11+
"test:smoke": "npm run build && npm run start &",
12+
"lint": "eslint src",
13+
"format": "prettier --write src"
14+
},
15+
"keywords": [
16+
"stellar",
17+
"vault",
18+
"rwa",
19+
"yield"
20+
],
21+
"author": "",
22+
"license": "MIT",
23+
"dependencies": {
24+
"express": "^4.18.2",
25+
"express-rate-limit": "^7.0.0",
26+
"dotenv": "^16.3.1",
27+
"node-cache": "^5.1.2"
28+
},
29+
"devDependencies": {
30+
"@types/express": "^4.17.17",
31+
"@types/node": "^20.0.0",
32+
"@types/jest": "^29.5.0",
33+
"typescript": "^5.1.0",
34+
"tsx": "^4.0.0",
35+
"jest": "^29.5.0",
36+
"ts-jest": "^29.1.0",
37+
"supertest": "^6.3.3",
38+
"@types/supertest": "^2.0.12",
39+
"eslint": "^8.40.0",
40+
"@typescript-eslint/eslint-plugin": "^5.59.0",
41+
"@typescript-eslint/parser": "^5.59.0",
42+
"prettier": "^3.0.0"
43+
}
44+
}

0 commit comments

Comments
 (0)