Skip to content
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

Added endpoint to generate csrf tokens #1281

Merged
merged 10 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/run-ci-cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
- name: Check for uncommitted changes
run: |
git diff --exit-code || (echo 'Unstaged changes detected. \
Run `make check-all` and use `git add` to address it.' && exit 1)
Run `make check` and use `git add` to address it.' && exit 1)

spellcheck:
name: Run spell check
Expand Down Expand Up @@ -241,6 +241,7 @@ jobs:
run: |
touch frontend/.env
echo "VITE_API_URL=${{ secrets.VITE_API_URL }}" >> frontend/.env
echo "VITE_CSRF_URL=${{ secrets.VITE_CSRF_URL }}" >> frontend/.env
echo "VITE_ENVIRONMENT=${{ secrets.VITE_ENVIRONMENT }}" >> frontend/.env
echo "VITE_GRAPHQL_URL=${{ secrets.VITE_GRAPHQL_URL }}" >> frontend/.env
echo "VITE_IDX_URL=${{ secrets.VITE_IDX_URL }}" >> frontend/.env
Expand Down Expand Up @@ -404,6 +405,7 @@ jobs:
run: |
touch frontend/.env
echo "VITE_API_URL=${{ secrets.VITE_API_URL }}" >> frontend/.env
echo "VITE_CSRF_URL=${{ secrets.VITE_CSRF_URL }}" >> frontend/.env
echo "VITE_ENVIRONMENT=${{ secrets.VITE_ENVIRONMENT }}" >> frontend/.env
echo "VITE_GRAPHQL_URL=${{ secrets.VITE_GRAPHQL_URL }}" >> frontend/.env
echo "VITE_IDX_URL=${{ secrets.VITE_IDX_URL }}" >> frontend/.env
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Check for uncommitted changes
run: |
git diff --exit-code || (echo 'Unstaged changes detected. \
Run `make check-all` and use `git add` to address it.' && exit 1)
Run `make check` and use `git add` to address it.' && exit 1)

code-ql:
name: CodeQL
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Please follow these contribution guidelines for OWASP Schema-related changes:
Nest enforces code quality standards to ensure consistency and maintainability. You can run automated checks locally before pushing your changes:

```bash
make check-all
make check
```

This command runs linters and other static analysis tools for both the frontend and backend.
Expand All @@ -245,7 +245,7 @@ This command runs linters and other static analysis tools for both the frontend
Our CI/CD pipelines automatically run tests against every Pull Request. You can run tests locally before submitting a PR:

```bash
make test-all
make test
```

This command runs tests and checks that coverage threshold requirements are satisfied for both backend and frontend.
Expand Down Expand Up @@ -286,7 +286,7 @@ git checkout -b feature/my-feature-name
- Run the code quality checks and tests:

```bash
make check-test-all
make check-test
```

- Write meaningful commit messages:
Expand Down
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ include schema/Makefile
build:
@docker compose build

check-all: \
clean: \
clean-backend \
clean-frontend \
clean-schema

check: \
check-backend \
check-frontend \
check-spelling

check-backend: \
pre-commit

check-test-all: \
check-all \
test-all
check-test: \
check \
test

check-test-backend: \
pre-commit \
Expand All @@ -33,7 +38,7 @@ pre-commit:
run:
@COMPOSE_BAKE=true docker compose -f docker/docker-compose-local.yaml up --build --remove-orphans

test-all: \
test: \
test-nest-app \
test-schema

Expand Down
5 changes: 5 additions & 0 deletions backend/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
clean-backend:
@rm -rf backend/.cache
@rm -rf backend/.local
@rm -rf backend/.venv

exec-backend-command:
@docker exec -i nest-backend $(CMD)

Expand Down
13 changes: 13 additions & 0 deletions backend/apps/core/api/csrf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""CSRF token API."""

from django.http import JsonResponse
from django.middleware.csrf import get_token
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.decorators.http import require_GET


@require_GET
@ensure_csrf_cookie
def get_csrf_token(request):
"""Return a response with the CSRF token."""
return JsonResponse({"csrftoken": get_token(request)})
8 changes: 5 additions & 3 deletions backend/settings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.csrf import csrf_protect
from graphene_django.views import GraphQLView
from rest_framework import routers

from apps.core.api.algolia import algolia_search
from apps.core.api.csrf import get_csrf_token
from apps.github.api.urls import router as github_router
from apps.owasp.api.urls import router as owasp_router
from apps.slack.apps import SlackConfig
Expand All @@ -22,8 +23,9 @@
router.registry.extend(owasp_router.registry)

urlpatterns = [
path("idx/", csrf_exempt(algolia_search)),
path("graphql/", csrf_exempt(GraphQLView.as_view(graphiql=settings.DEBUG))),
path("csrf/", get_csrf_token),
path("idx/", csrf_protect(algolia_search)),
path("graphql/", csrf_protect(GraphQLView.as_view(graphiql=settings.DEBUG))),
path("api/v1/", include(router.urls)),
path("a/", admin.site.urls),
]
Expand Down
1 change: 1 addition & 0 deletions frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VITE_API_URL=http://localhost:8000/api/v1/
VITE_CSRF_URL=http://localhost:8000/csrf/
VITE_ENVIRONMENT=local
VITE_GRAPHQL_URL=http://localhost:8000/graphql/
VITE_GTM_AUTH=your-google-tag-manager-auth
Expand Down
8 changes: 6 additions & 2 deletions frontend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ check-frontend: \
format-frontend-code \
lint-frontend-code

clean-frontend:
@rm -rf frontend/.pnpm-store
@rm -rf frontend/node_modules

exec-frontend-command:
@docker exec -t nest-frontend $(CMD)

Expand All @@ -19,8 +23,8 @@ shell-frontend:
@CMD="/bin/sh" $(MAKE) exec-frontend-command-it

test-frontend: \
test-frontend-e2e \
test-frontend-unit
test-frontend-unit \
test-frontend-e2e

test-frontend-e2e:
@DOCKER_BUILDKIT=1 docker build \
Expand Down
9 changes: 9 additions & 0 deletions frontend/__tests__/e2e/pages/About.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ test.describe('About Page', () => {
}
})

await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])

await page.goto('/about')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/ChapterDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ test.describe('Chapter Details Page', () => {
json: { data: { chapter: mockChapterDetailsData } },
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/chapters/test-chapter')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/Chapters.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ test.describe('Chapters Page', () => {
}),
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/chapters')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/CommitteeDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ test.describe('Committee Details Page', () => {
json: { data: mockCommitteeDetailsData },
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/committees/test-committee')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/Committees.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ test.describe('Committees Page', () => {
}),
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/committees')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/Contribute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ test.describe('Contribute Page', () => {
}),
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/projects/contribute')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/Home.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ test.describe('Home Page', () => {
json: mockHomeData,
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/ProjectDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ test.describe('Project Details Page', () => {
json: { data: mockProjectDetailsData },
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/projects/test-project', { timeout: 60000 })
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/Projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ test.describe('Projects Page', () => {
}),
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/projects')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/RepositoryDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ test.describe('Repository Details Page', () => {
json: { data: mockRepositoryData },
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/repositories/test-repository')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/UserDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ test.describe('User Details Page', () => {
json: { data: mockUserDetailsData },
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('community/users/test-user')
})
test('should have a heading and summary', async ({ page }) => {
Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/Users.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ test.describe('Users Page', () => {
}),
})
})
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/community/users')
})

Expand Down
8 changes: 8 additions & 0 deletions frontend/__tests__/e2e/pages/footerIcons.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { test, expect } from '@playwright/test'

test.describe('Footer Social Media Icons', () => {
test.beforeEach(async ({ page }) => {
await page.context().addCookies([
{
name: 'csrftoken',
value: 'abc123',
domain: 'localhost',
path: '/',
},
])
await page.goto('/')
})

Expand Down
Loading