Skip to content

Commit 2c113fb

Browse files
committed
Worked security
1 parent 3f3297a commit 2c113fb

26 files changed

Lines changed: 758 additions & 200 deletions

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ APP_ENV=development
22
SECRET_KEY=replace-with-a-long-random-string
33
FRONTEND_URL=http://localhost:5173
44
SESSION_TTL_SECONDS=3600
5+
SESSION_STORE_TYPE=sqlite # or 'redis'
56
SESSIONS_DB_PATH=sessions.db
7+
REDIS_URL=redis://localhost:6379/0
68
GITHUB_CLIENT_ID=your_github_client_id
79
GITHUB_CLIENT_SECRET=your_github_client_secret
810
GITHUB_TOKEN=your_github_token

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* text=auto
1+
* text=auto

.github/workflows/ci.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: ["main", "master"]
6+
pull_request:
7+
branches: ["main", "master"]
8+
9+
jobs:
10+
backend:
11+
name: Backend tests
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: 3.11
21+
22+
- name: Install Python dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install -r requirements.txt
26+
27+
- name: Run formatting + lint checks (pre-commit)
28+
env:
29+
SKIP: eslint,prettier
30+
run: pre-commit run --all-files
31+
32+
- name: Run backend tests
33+
run: python -m unittest discover -s tests -v
34+
35+
frontend:
36+
name: Frontend lint
37+
runs-on: ubuntu-latest
38+
steps:
39+
- name: Checkout repository
40+
uses: actions/checkout@v4
41+
42+
- name: Set up Node.js
43+
uses: actions/setup-node@v5
44+
with:
45+
node-version: 18
46+
cache: "npm"
47+
48+
- name: Install frontend dependencies
49+
working-directory: frontend
50+
run: npm ci
51+
52+
- name: Run frontend lint
53+
working-directory: frontend
54+
run: npm run lint

.pre-commit-config.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.6.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-added-large-files
9+
10+
- repo: https://github.com/psf/black
11+
rev: 26.3.1
12+
hooks:
13+
- id: black
14+
language_version: python3.11
15+
16+
- repo: https://github.com/PyCQA/isort
17+
rev: 8.0.1
18+
hooks:
19+
- id: isort
20+
language_version: python3.11

PRODUCTION_READINESS.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Production Readiness Checklist
22

33
## 1. Security
4+
45
- [x] `.env` excluded from git.
56
- [x] `.env.example` provided with non-secret placeholders.
67
- [x] Backend enforces `SECRET_KEY` outside development mode.
@@ -9,32 +10,37 @@
910
- [ ] Move secrets to deployment secret manager (not file-based on server).
1011

1112
## 2. Auth & Sessions
13+
1214
- [x] Session cookie uses environment-based `secure` and `samesite`.
1315
- [x] Session TTL configurable (`SESSION_TTL_SECONDS`).
1416
- [x] Replace `sessions.json` with SQLite-backed session store (`sessions.db`).
15-
- [ ] Add CSRF protections for state-changing endpoints.
16-
- [ ] Optional: move sessions from SQLite to Redis for multi-instance horizontal scaling.
17+
- [x] Add CSRF protections for state-changing endpoints (enforced in non-development environments).
18+
- [x] Optional: move sessions from SQLite to Redis for multi-instance horizontal scaling.
1719

1820
## 3. Reliability
21+
1922
- [x] AI model fallback and status endpoint (`/api/ai-status`) added.
2023
- [x] Recursive file tree endpoint with graceful fallback.
2124
- [x] Better GitHub/Ollama error messages exposed to frontend.
22-
- [ ] Add circuit-breaker/retry policy for repeated upstream failures.
25+
- [x] Add circuit-breaker/retry policy for repeated upstream failures.
2326

2427
## 4. Observability
28+
2529
- [x] Request ID added on all responses (`X-Request-ID`).
2630
- [x] Basic request latency logging middleware added.
2731
- [ ] Centralized structured logging sink (ELK/Datadog/etc.).
2832
- [ ] Error alerting and uptime checks.
2933

3034
## 5. Quality Gates
35+
3136
- [x] Static checks currently passing (`py_compile`, frontend lint).
3237
- [x] Add backend API tests for auth/session lifecycle (`tests/test_auth_sessions.py`).
3338
- [ ] Extend backend API tests to files/chat endpoints.
3439
- [ ] Add frontend integration tests (repo tree + file preview + chat).
35-
- [ ] CI pipeline enforcing lint/tests before merge.
40+
- [x] CI pipeline enforcing lint/tests before merge.
3641

3742
## Recommended Next Sprint
43+
3844
1. Replace file-based session store with Redis.
3945
2. Add automated tests for core flows.
4046
3. Add deployment config for managed secrets and observability.

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
# 🤖 CodeScribeAI
22

33
CodeScribeAI is a local-first coding assistant with:
4+
45
- FastAPI backend
56
- React + Vite frontend
67
- GitHub repository analysis
78
- Ollama-powered AI responses
89

910
## Current Architecture
11+
1012
- Backend: `main.py` (FastAPI)
1113
- Frontend: `frontend/` (React, Vite)
1214
- Session store: SQLite (`sessions.db`)
1315
- Optional async workers: Celery + Redis
1416

1517
## Prerequisites
18+
1619
- Python 3.10+
1720
- Node.js 18+
1821
- Ollama installed and running
1922
- (Optional) Redis for async chat endpoints
2023

2124
## Environment Setup
25+
2226
1. Copy `.env.example` to `.env`
2327
2. Fill real credentials and secrets
2428

@@ -27,12 +31,15 @@ cp .env.example .env
2731
```
2832

2933
Required keys:
34+
3035
```env
3136
APP_ENV=development
3237
SECRET_KEY=replace-with-a-long-random-string
3338
FRONTEND_URL=http://localhost:5173
3439
SESSION_TTL_SECONDS=3600
40+
SESSION_STORE_TYPE=sqlite # or "redis"
3541
SESSIONS_DB_PATH=sessions.db
42+
REDIS_URL=redis://localhost:6379/0
3643
GITHUB_CLIENT_ID=your_github_client_id
3744
GITHUB_CLIENT_SECRET=your_github_client_secret
3845
GITHUB_TOKEN=your_github_token
@@ -42,42 +49,60 @@ OLLAMA_FALLBACK_MODELS=
4249
```
4350

4451
Optional async keys:
52+
4553
```env
4654
CELERY_BROKER_URL=redis://localhost:6379/0
4755
CELERY_RESULT_BACKEND=redis://localhost:6379/0
4856
```
4957

5058
## Install
59+
5160
### Backend
61+
5262
```bash
5363
# from repo root
5464
python -m venv venv
5565
# Windows PowerShell
5666
.\venv\Scripts\Activate.ps1
57-
pip install fastapi uvicorn httpx python-dotenv itsdangerous pydantic celery
67+
pip install -r requirements.txt
68+
```
69+
70+
### Pre-commit (format + lint hooks)
71+
72+
```bash
73+
# after installing requirements
74+
pre-commit install
75+
pre-commit run --all-files
5876
```
5977

78+
(If you're using a different shell, use the same commands with the appropriate activation step.)
79+
6080
### Frontend
81+
6182
```bash
6283
cd frontend
6384
npm install
6485
```
6586

6687
## Run Locally
88+
6789
### 1) Start Ollama
90+
6891
```bash
6992
ollama serve
7093
ollama pull phi3:mini
7194
```
7295

7396
### 2) Start backend
97+
7498
```bash
7599
# from repo root
76100
.\venv\Scripts\Activate.ps1
77101
uvicorn main:app --reload --host 127.0.0.1 --port 8000
78102
```
79103

80104
### 3) Start frontend
105+
81106
```bash
82107
cd frontend
83108
npm run dev
@@ -86,6 +111,7 @@ npm run dev
86111
Open: `http://localhost:5173`
87112

88113
## Optional: Async Worker (Celery)
114+
89115
Only needed if you use async task endpoints.
90116

91117
```bash
@@ -95,18 +121,22 @@ celery -A main:celery_app worker --loglevel=info --pool=solo
95121
```
96122

97123
## Testing and Quality
124+
98125
### Backend tests
126+
99127
```bash
100128
python -m unittest discover -s tests -v
101129
```
102130

103131
### Frontend lint
132+
104133
```bash
105134
cd frontend
106135
npm run lint
107136
```
108137

109138
## Key API Endpoints
139+
110140
- `GET /health`
111141
- `GET /api/ai-status`
112142
- `POST /api/chat`
@@ -116,11 +146,13 @@ npm run lint
116146
- `GET /repos/{owner}/{repo}/file-content?path=...`
117147

118148
## Security Notes
149+
119150
- Never commit `.env`.
120151
- Rotate any leaked credentials immediately.
121152
- For non-development environments:
122153
- set a strong `SECRET_KEY`
123154
- use secure deployment secrets management
124155

125156
## Production Readiness
157+
126158
See `PRODUCTION_READINESS.md` for checklist and current status.

frontend/eslint.config.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import js from '@eslint/js'
2-
import globals from 'globals'
3-
import reactHooks from 'eslint-plugin-react-hooks'
4-
import reactRefresh from 'eslint-plugin-react-refresh'
5-
import { defineConfig, globalIgnores } from 'eslint/config'
1+
import js from "@eslint/js";
2+
import globals from "globals";
3+
import reactHooks from "eslint-plugin-react-hooks";
4+
import reactRefresh from "eslint-plugin-react-refresh";
5+
import { defineConfig, globalIgnores } from "eslint/config";
66

77
export default defineConfig([
8-
globalIgnores(['dist']),
8+
globalIgnores(["dist"]),
99
{
10-
files: ['**/*.{js,jsx}'],
10+
files: ["**/*.{js,jsx}"],
1111
extends: [
1212
js.configs.recommended,
13-
reactHooks.configs['recommended-latest'],
14-
reactRefresh.configs.vite,
13+
reactHooks.configs["recommended-latest"],
14+
reactRefresh.configs.vite
1515
],
1616
languageOptions: {
1717
ecmaVersion: 2020,
1818
globals: globals.browser,
1919
parserOptions: {
20-
ecmaVersion: 'latest',
20+
ecmaVersion: "latest",
2121
ecmaFeatures: { jsx: true },
22-
sourceType: 'module',
23-
},
22+
sourceType: "module"
23+
}
2424
},
2525
rules: {
26-
'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
27-
},
28-
},
29-
])
26+
"no-unused-vars": ["error", { varsIgnorePattern: "^[A-Z_]" }]
27+
}
28+
}
29+
]);

frontend/postcss.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
export default {
33
plugins: {
44
"@tailwindcss/postcss": {},
5-
autoprefixer: {},
6-
},
5+
autoprefixer: {}
6+
}
77
};

frontend/public/vite.svg

Lines changed: 1 addition & 1 deletion
Loading

frontend/src/App.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ export default function App() {
1616
<Route
1717
path="/playground"
1818
element={
19-
<ProtectedRoute>
20-
<Playground />
21-
</ProtectedRoute>
19+
(
20+
<ProtectedRoute>
21+
<Playground />
22+
</ProtectedRoute>
23+
)
2224
}
2325
/>
2426
<Route path="/" element={<Home />} />

0 commit comments

Comments
 (0)