-
Notifications
You must be signed in to change notification settings - Fork 0
Security/p0 hardening #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmvasheka
wants to merge
8
commits into
main
Choose a base branch
from
security/p0-hardening
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
57e205e
ci: GitHub Actions pipeline + Node 20 + lint debt cleanup
dmitrovasheka 88c2d9c
feat(observability): /api/health endpoint (DB+Redis) and optional Sen…
dmitrovasheka b1b3593
test: characterization tests for cursor parsing and movies autocomplete
dmitrovasheka 0cb419b
feat(ai): OpenAI usage accounting and daily budget guard
dmitrovasheka f1e9f76
docs: mark P1 (§18) progress, split CI build step to avoid .next/type…
dmitrovasheka ef8178f
chore(db): apply pending migrations to prod, sync migration drift bot…
dmitrovasheka 32a2a11
fix(ci,review): address CodeRabbit round 2 + unbreak pnpm setup in CI
dmitrovasheka 03d4cae
ci: api tests need @repo/db and @repo/ai built first
dmitrovasheka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Secrets must never enter the image build context | ||
| .env | ||
| .env.* | ||
| !.env.example | ||
|
|
||
| # VCS / local state | ||
| .git | ||
| .claude | ||
| .turbo | ||
| *.log | ||
|
|
||
| # Dependencies and build outputs (rebuilt inside the image) | ||
| node_modules | ||
| **/node_modules | ||
| **/.next | ||
| **/dist | ||
| coverage | ||
|
|
||
| # Not needed in the API image | ||
| apps/docs | ||
| docs | ||
| supabase | ||
| scripts | ||
| setup-production.sh | ||
| *.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [main] | ||
| push: | ||
| branches: [main] | ||
|
|
||
| # Least privilege: jobs only read the repo; nothing here pushes or comments. | ||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| checks: | ||
| name: Lint, typecheck, test, build | ||
| runs-on: ubuntu-latest | ||
| env: | ||
| # Dummy values: module-load env asserts (packages/ai, packages/db) and | ||
| # Next.js prerender need *some* value; no real services are called in CI. | ||
| OPENAI_API_KEY: ci-dummy | ||
| SUPABASE_URL: https://ci-dummy.supabase.co | ||
| SUPABASE_SERVICE_KEY: ci-dummy | ||
| SUPABASE_ANON_KEY: ci-dummy | ||
| NEXT_PUBLIC_SUPABASE_URL: https://ci-dummy.supabase.co | ||
| NEXT_PUBLIC_SUPABASE_ANON_KEY: ci-dummy | ||
| NEXT_PUBLIC_API_URL: http://localhost:3001 | ||
| steps: | ||
| - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| # pnpm version comes from packageManager in package.json (single source | ||
| # of truth) — specifying it here too makes action-setup error out. | ||
| - uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4 | ||
|
|
||
| - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 | ||
| with: | ||
| node-version: 20 | ||
| cache: pnpm | ||
|
|
||
| - name: Install dependencies | ||
| run: pnpm install --frozen-lockfile | ||
|
|
||
| # Two steps on purpose: next build rewrites .next/types while tsc reads | ||
| # them, so check-types must not run concurrently with build. | ||
| - name: Lint, typecheck, test | ||
| run: pnpm exec turbo run lint check-types test | ||
|
|
||
| - name: Build | ||
| run: pnpm exec turbo run build | ||
|
|
||
| security: | ||
| name: Secret scan & dependency audit | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| persist-credentials: false | ||
|
|
||
| - name: Gitleaks (secret scan) | ||
| uses: gitleaks/gitleaks-action@ff98106e4c7b2bc287b24eaf42907196329070c7 # v2 | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| # workflow has contents:read only — PR comments would need extra perms | ||
| GITLEAKS_ENABLE_COMMENTS: false | ||
|
|
||
| - uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4 | ||
|
|
||
| - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 | ||
| with: | ||
| node-version: 20 | ||
| cache: pnpm | ||
|
|
||
| - name: Install dependencies | ||
| run: pnpm install --frozen-lockfile | ||
|
|
||
| # Non-blocking for now: existing transitive vulns are tracked in IMPROVEMENTS §13.3 | ||
| - name: pnpm audit (high+) | ||
| run: pnpm audit --audit-level high | ||
| continue-on-error: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { ExecutionContext, HttpException } from '@nestjs/common'; | ||
| import { AiBudgetGuard } from '../ai-budget.guard'; | ||
| import { AiUsageService } from '../ai-usage.service'; | ||
|
|
||
| jest.mock('@repo/db', () => ({ supabase: { from: jest.fn() } })); | ||
|
|
||
| function ctx(): ExecutionContext { | ||
| return {} as ExecutionContext; | ||
| } | ||
|
|
||
| function usageServiceWithSpend(spend: number): AiUsageService { | ||
| return { getTodaySpendUsd: async () => spend } as unknown as AiUsageService; | ||
| } | ||
|
|
||
| describe('AiBudgetGuard', () => { | ||
| const originalBudget = process.env.OPENAI_DAILY_BUDGET_USD; | ||
|
|
||
| afterEach(() => { | ||
| if (originalBudget === undefined) { | ||
| delete process.env.OPENAI_DAILY_BUDGET_USD; | ||
| } else { | ||
| process.env.OPENAI_DAILY_BUDGET_USD = originalBudget; | ||
| } | ||
| }); | ||
|
|
||
| it('allows requests when no budget is configured', async () => { | ||
| delete process.env.OPENAI_DAILY_BUDGET_USD; | ||
| const guard = new AiBudgetGuard(usageServiceWithSpend(999)); | ||
| await expect(guard.canActivate(ctx())).resolves.toBe(true); | ||
| }); | ||
|
|
||
| it('allows requests while spend is under the budget', async () => { | ||
| process.env.OPENAI_DAILY_BUDGET_USD = '5'; | ||
| const guard = new AiBudgetGuard(usageServiceWithSpend(4.99)); | ||
| await expect(guard.canActivate(ctx())).resolves.toBe(true); | ||
| }); | ||
|
|
||
| it('rejects with 429 when the daily budget is exhausted', async () => { | ||
| process.env.OPENAI_DAILY_BUDGET_USD = '5'; | ||
| const guard = new AiBudgetGuard(usageServiceWithSpend(5)); | ||
|
|
||
| await expect(guard.canActivate(ctx())).rejects.toThrow(HttpException); | ||
| await guard.canActivate(ctx()).catch((e: HttpException) => { | ||
| expect(e.getStatus()).toBe(429); | ||
| }); | ||
| }); | ||
|
|
||
| it('ignores invalid budget values (fail-open)', async () => { | ||
| process.env.OPENAI_DAILY_BUDGET_USD = 'not-a-number'; | ||
| const guard = new AiBudgetGuard(usageServiceWithSpend(999)); | ||
| await expect(guard.canActivate(ctx())).resolves.toBe(true); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.