Skip to content

Commit

Permalink
Fix permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent de Chefdebien committed Feb 21, 2023
1 parent 04ac937 commit ac3799b
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 125 deletions.
4 changes: 4 additions & 0 deletions 1_cookie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Pour démarrer le serveur
- npm i
- npm run dev
- ouvrir http://localhost:3000 dans Firefox
4 changes: 4 additions & 0 deletions 2_vanilla-xss/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Pour démarrer le serveur
- npm i
- npm run dev
- ouvrir http://localhost:3000 dans Firefox
37 changes: 8 additions & 29 deletions 3_vue-xss/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
# vue-xss

This template should help get you started developing with Vue 3 in Vite.

## Recommended IDE Setup

[VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin).

## Customize configuration

See [Vite Configuration Reference](https://vitejs.dev/config/).

## Project Setup

```sh
npm install
```

### Compile and Hot-Reload for Development

```sh
npm run dev
```

### Compile and Minify for Production

```sh
npm run build
```
Pour démarrer le backend
- cd 2_vanilla-xss
- npm run dev

Pour démarrer le frontend
- npm i
- npm run dev
- ouvrir http://localhost:4000 dans Firefox
6 changes: 6 additions & 0 deletions 4_csrf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Pour démarrer le serveur
- npm i
- npm run dev
- ouvrir http://localhost:3000 dans Firefox

Copier le code de csrf.html sur codepen.io pour tester une requete cross-site
13 changes: 13 additions & 0 deletions 5_sqli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Pour installer la BDD
- docker run --detach --name sqlidb -p 5000:3306 --env MARIADB_USER=mdbuser --env MARIADB_PASSWORD=mdbpassword --env MARIADB_ROOT_PASSWORD=mdbroot mariadb:latest

Pour réinitialiser le contenu de la BDD
- docker exec -i sqlidb mysql -u root -pmdbroot < init.sql

Pour démarrer la BDD les fois suivantes
- docker start sqlidb

Pour démarrer le serveur
- npm i
- npm run dev
- ouvrir http://localhost:3000 dans Firefox
13 changes: 13 additions & 0 deletions 6_exceptions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Pour installer la BDD
- docker run --detach --name sqlidb -p 5000:3306 --env MARIADB_USER=mdbuser --env MARIADB_PASSWORD=mdbpassword --env MARIADB_ROOT_PASSWORD=mdbroot mariadb:latest

Pour réinitialiser le contenu de la BDD
- docker exec -i sqlidb mysql -u root -pmdbroot < init.sql

Pour démarrer la BDD les fois suivantes
- docker start sqlidb

Pour démarrer le serveur
- npm i
- npm run dev
- ouvrir http://localhost:3000 dans Firefox
4 changes: 4 additions & 0 deletions 7_xxe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Pour démarrer le serveur
- npm i
- npm run dev
- ouvrir http://localhost:3000 dans Firefox
16 changes: 16 additions & 0 deletions 8_unittests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Pour installer la BDD
- docker run --detach --name sqlidb -p 5000:3306 --env MARIADB_USER=mdbuser --env MARIADB_PASSWORD=mdbpassword --env MARIADB_ROOT_PASSWORD=mdbroot mariadb:latest

Pour réinitialiser le contenu de la BDD
- docker exec -i sqlidb mysql -u root -pmdbroot < init.sql

Pour démarrer la BDD les fois suivantes
- docker start sqlidb

Pour démarrer le serveur
- npm i
- npm run dev
- ouvrir http://localhost:3000 dans Firefox

Pour lancer les tests
- npm run test
8 changes: 8 additions & 0 deletions 9_cypress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Pour démarrer le serveur
- npm i
- npm run dev
- ouvrir http://localhost:3000 dans Firefox

Pour lancer les tests cypress
- npm run test
- e2e tests dans chromium
2 changes: 1 addition & 1 deletion 9_cypress/cypress/e2e/routes.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("Get Endpoints", () => {
cy.get("[data-cy='xssPage']").click()

// find textarea and set text into it
cy.get("textarea").type("mon texte")
cy.get("#saisie").type("mon texte")
// click on button to send
cy.contains("Envoyer").click()
// check that message is displayed
Expand Down
3 changes: 2 additions & 1 deletion 9_cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "supervisor server.js"
"dev": "supervisor server.js",
"test": "cypress open"
},
"author": "",
"license": "ISC",
Expand Down
11 changes: 11 additions & 0 deletions a_permissions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Code adapté de https://github.com/cornflourblue/node-role-based-authorization-api.git

Pour lancer le backend
- cd backend
- npm i
- npm run dev

Pour lancer le frontend
- cd frontend
- npm i
- npm run dev
29 changes: 20 additions & 9 deletions a_permissions/backend/_helpers/authorize.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
const jwt = require('express-jwt');
const { secret } = require('config.json');
const Role = require('./role');
const Permission = require('./permission');

module.exports = authorize;

function authorize(roles = []) {
// roles param can be a single role string (e.g. Role.User or 'User')
// or an array of roles (e.g. [Role.Admin, Role.User] or ['Admin', 'User'])
if (typeof roles === 'string') {
roles = [roles];
}
function authorize(permission) {

return [
// authenticate JWT token and attach user to request object (req.user)
jwt({ secret, algorithms: ['HS256'] }),

// authorize based on user role
(req, res, next) => {
if (roles.length && !roles.includes(req.user.role)) {
let isAuthorized = false
if (permission === Permission.FREE_ACCESS) {
// route without permission => allow every authenticated users
isAuthorized = true
}
else if (req.user.role) {
// retrieve permissions for role
const role = Role[req.user.role]
if (role && role.permissions.includes(permission)) {
isAuthorized = true
}
}
if (!isAuthorized) {
// user's role is not authorized
return res.status(401).json({ message: 'Unauthorized' });
}
else {
// authentication and authorization successful
next();
}

// authentication and authorization successful
next();
}
];
}
6 changes: 6 additions & 0 deletions a_permissions/backend/_helpers/permission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
LIST_USERS: "list_all_users",
VIEW_PROFILE: "view_user_profile",
FREE_ACCESS: "everybody"
}

23 changes: 21 additions & 2 deletions a_permissions/backend/_helpers/role.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
const Permission = require("./permission")

module.exports = {
Admin: 'Admin',
User: 'User'
Admin: {
name: "Admin",
permissions: [
Permission.LIST_USERS,
Permission.VIEW_PROFILE
]
},
User: {
name: "User",
permissions: [
Permission.VIEW_PROFILE
]
},
Guest: {
name: "Guest",
permissions: [
// nothing. Only FREE_ACCESS will be allowed for guests
]
}
}
4 changes: 2 additions & 2 deletions a_permissions/backend/users/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const Role = require('_helpers/role');

// users hardcoded for simplicity, store in a db for production applications
const users = [
{ id: 1, username: 'admin', password: 'admin', firstName: 'Admin', lastName: 'User', role: Role.Admin },
{ id: 2, username: 'user', password: 'user', firstName: 'Normal', lastName: 'User', role: Role.User }
{ id: 1, username: 'admin', password: 'admin', firstName: 'Admin', lastName: 'User', role: Role.Admin.name },
{ id: 2, username: 'user', password: 'user', firstName: 'Normal', lastName: 'User', role: Role.User.name }
];

module.exports = {
Expand Down
16 changes: 10 additions & 6 deletions a_permissions/backend/users/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
const router = express.Router();
const userService = require('./user.service');
const authorize = require('_helpers/authorize')
const Permission = require('_helpers/permission');
const Role = require('_helpers/role');

// routes
router.post('/authenticate', authenticate); // public route
router.get('/', authorize(Role.Admin), getAll); // admin only
router.get('/:id', authorize(), getById); // all authenticated users
router.get('/', authorize(Permission.LIST_USERS), getAll); // admin only
router.get('/:id', authorize(Permission.VIEW_PROFILE), getById); // all authenticated users
module.exports = router;

function authenticate(req, res, next) {
Expand All @@ -23,12 +24,15 @@ function getAll(req, res, next) {
}

function getById(req, res, next) {
const currentUser = req.user;
const currentUserId = req.user.sub;
const id = parseInt(req.params.id);

// only allow admins to access other user records
if (id !== currentUser.sub && currentUser.role !== Role.Admin) {
return res.status(401).json({ message: 'Unauthorized' });
// only allow users with LIST_USERS permission to access other users records
if (id !== currentUserId) {
const role = Role[req.user.role]
if (!role || !role.permissions.includes(Permission.LIST_USERS)) {
return res.status(401).json({ message: 'Unauthorized' });
}
}

userService.getById(req.params.id)
Expand Down
74 changes: 0 additions & 74 deletions a_permissions/frontend/src/_helpers/fake-backend.js

This file was deleted.

1 change: 0 additions & 1 deletion a_permissions/frontend/src/_helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './fake-backend';
export * from './handle-response';
export * from './request-options';
export * from './role';
Expand Down

0 comments on commit ac3799b

Please sign in to comment.