-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: csrf and session integration using redis (#30)
- Loading branch information
Showing
94 changed files
with
2,090 additions
and
159 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -46,3 +46,4 @@ apps/site/public/images | |
docker/ | ||
!libs/docker/ | ||
*.log | ||
dump |
This file contains 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,22 @@ | ||
{ | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"decorators": true | ||
}, | ||
"transform": { | ||
"legacyDecorator": true, | ||
"decoratorMetadata": true | ||
}, | ||
"target": "es2017", | ||
"keepClassNames": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"@unteris/server/*": ["libs/server/*/src/"] | ||
} | ||
}, | ||
"module": { | ||
"type": "commonjs", | ||
"strictMode": true | ||
} | ||
} |
This file contains 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,5 @@ | ||
:80 { | ||
root * ./dist/apps/site | ||
file_server | ||
try_files {path} /index.html | ||
} |
This file contains 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 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 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,6 @@ | ||
DATABASE_NAME=test | ||
DATABASE_PORT=5432 | ||
DATABASE_USER=postgres | ||
DATABASE_HOST=localhost | ||
DATABASE_PASSWORD=postgres | ||
REDIS_URL=redis://localhost:6379 |
This file contains 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,18 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
This file contains 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,28 @@ | ||
{ | ||
"name": "server-e2e", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "apps/server-e2e/src", | ||
"projectType": "application", | ||
"targets": { | ||
"e2e": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"commands": [ | ||
"docker compose -f docker-compose.test.yml up -d", | ||
"while ! nc -q0 localhost 6379 < /dev/null > /dev/null 2>&1; do sleep 10; done", | ||
"while ! nc -q0 localhost 5432 < /dev/null > /dev/null 2>&1; do sleep 10; done", | ||
"NODE_ENV=development node -r @swc/register apps/server-e2e/src/main.ts", | ||
"docker compose -f docker-compose.test.yml down" | ||
] | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["apps/server-e2e/**/*.ts"] | ||
} | ||
} | ||
}, | ||
"tags": ["server", "e2e", "test"] | ||
} |
0
apps/server/src/app/.gitkeep → apps/server-e2e/src/assets/.gitkeep
100755 → 100644
File renamed without changes.
This file contains 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,28 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { OgmaService } from '@ogma/nestjs-module'; | ||
import { RootModule } from '@unteris/server/root'; | ||
import { request, spec } from 'pactum'; | ||
import { suite } from 'uvu'; | ||
import { csrfTests } from './tests/csrf'; | ||
|
||
type UvuContext = { app: INestApplication }; | ||
|
||
const ApplicationE2E = suite<UvuContext>('Unteris E2E test suite'); | ||
|
||
ApplicationE2E.before(async (context) => { | ||
const app = await NestFactory.create(RootModule, { bufferLogs: true }); | ||
app.useLogger(app.get(OgmaService)); | ||
await app.listen(0); | ||
const reqURL = await app.getUrl(); | ||
request.setBaseUrl(reqURL.replace('[::1]', 'localhost')); | ||
context.app = app; | ||
}); | ||
|
||
ApplicationE2E.after(async ({ app }) => { | ||
await app.close(); | ||
}); | ||
|
||
ApplicationE2E('CSRF Testing', csrfTests); | ||
|
||
ApplicationE2E.run(); |
This file contains 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 { parse } from 'lightcookie'; | ||
import { spec } from 'pactum'; | ||
import { Callback } from 'uvu'; | ||
import { ok, unreachable } from 'uvu/assert'; | ||
|
||
export const csrfTests: Callback<any> = async (_context: any) => { | ||
await spec() | ||
.get('/csrf') | ||
.expectStatus(200) | ||
.expectJsonLike({ csrfToken: /\w+/ }) | ||
.expect(({ res }) => { | ||
const cookies = res.headers['set-cookie']; | ||
if (!cookies || cookies.length === 0) { | ||
unreachable('Received no cookies from the server'); | ||
return; | ||
} | ||
cookies.forEach((cookie) => { | ||
const parsed = parse(cookie); | ||
ok( | ||
['sessionId', 'refreshId'].some((cookieName) => { | ||
return cookieName in parsed; | ||
}) | ||
); | ||
}); | ||
}) | ||
.stores((_req, res) => { | ||
const { csrfToken } = res.body; | ||
const cookies = res.headers['set-cookie']; | ||
if (!cookies || cookies.length === 0) { | ||
unreachable('Received no cookies from the server'); | ||
return; | ||
} | ||
const sessionId = cookies | ||
.map((c) => parse(c)) | ||
.find((cookie) => 'sessionId' in cookie)?.sessionId; | ||
const refreshId = cookies | ||
.map((c) => parse(c)) | ||
.find((cookie) => 'refreshId' in cookie)?.refreshId; | ||
return { | ||
csrfToken, | ||
sessionId, | ||
refreshId, | ||
}; | ||
}) | ||
.toss(); | ||
await spec() | ||
.post('/csrf/verify') | ||
.withHeaders('X-UNTERIS-CSRF-PROTECTION', '$S{csrfToken}') | ||
.withCookies('sessionId', '$S{sessionId}') | ||
.expectStatus(201) | ||
.expectJson({ success: true }) | ||
.toss(); | ||
}; |
This file contains 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,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["node"] | ||
}, | ||
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"], | ||
"include": ["src/**/*.ts"] | ||
} |
This file contains 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,13 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.app.json" | ||
} | ||
], | ||
"compilerOptions": { | ||
"esModuleInterop": true | ||
} | ||
} |
This file contains 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 @@ | ||
|
||
{ | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"decorators": true | ||
}, | ||
"transform": { | ||
"legacyDecorator": true, | ||
"decoratorMetadata": true | ||
}, | ||
"target": "es2017", | ||
"keepClassNames": true, | ||
"baseUrl": "." | ||
}, | ||
"module": { | ||
"type": "commonjs", | ||
"strictMode": true | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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,14 @@ | ||
version: '3' | ||
services: | ||
postgres: | ||
image: postgres | ||
ports: | ||
- '5432:5432' | ||
environment: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_DB: test | ||
redis: | ||
image: redis | ||
ports: | ||
- '6379:6379' |
This file contains 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 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 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,18 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
Oops, something went wrong.