- Fixed SQL injection ""issue""

And various code cleaning things for spell route
This commit is contained in:
Alexis
2020-05-14 21:02:38 +02:00
parent 412402404b
commit 0766487309
7 changed files with 171 additions and 95 deletions

View File

@@ -1 +1,33 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 100 125" enable-background="new 0 0 100 100" xml:space="preserve"><path d="M93,49.1c-2.4-0.5-4.5-1.5-5.1-4.4c-0.5-2.9,1.8-4,4.1-5.3c0.7-0.4,1.2-0.8,1.4-1.2c-0.2-0.7-0.4-1.4-0.6-2.1 c-0.5-0.3-1.1-0.5-2.1-0.4c-2.5,0.3-4.8,0-6.2-2.6c-1.4-2.6,0.5-4.3,2.2-6.3c0.5-0.6,0.8-1.1,0.9-1.4c-0.4-0.6-0.9-1.3-1.3-1.9 c-0.5-0.1-1.2,0-2,0.3c-2.3,1-4.6,1.5-6.7-0.6c-2.1-2-0.9-4.3,0.2-6.7c0.3-0.6,0.4-1.2,0.4-1.5c-0.6-0.5-1.3-1-2-1.5 c-0.5,0.1-1,0.4-1.6,0.9c-1.8,1.7-3.9,2.8-6.5,1.5s-2.2-3.8-1.9-6.4c0.1-0.7,0-1.2,0-1.5c-0.8-0.3-1.6-0.6-2.4-0.9 c-0.4,0.2-0.8,0.7-1.2,1.3c-1.2,2.1-2.8,3.9-5.7,3.5S53.7,9,53.1,6.5c-0.1-0.6-0.3-1.1-0.5-1.4C51.8,5,50.9,5,50,5c0,0,0,0,0,0 c-0.3,0.4-0.6,0.9-0.7,1.6c-0.5,2.4-1.5,4.5-4.4,5.1c-2.9,0.5-4-1.8-5.3-4.1c-0.3-0.5-0.6-0.9-0.9-1.2c-0.9,0.2-1.7,0.5-2.5,0.7 c-0.2,0.4-0.3,1-0.2,1.7c0.3,2.5,0,4.8-2.6,6.2c-2.6,1.4-4.3-0.5-6.3-2.2c-0.5-0.4-0.9-0.7-1.2-0.8c-0.7,0.5-1.5,1-2.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-2.9-1.4-3-5.2-1.7-6.5c1.3-1.3,3.2-2.5,6.1-1.1c4.8,2.1,11.2,7.6,12.3,10.1 c1.2,2.5,0.7,4.1-1,5.8C73.6,37.6,70.8,36.8,68.3,34.2z M82.7,53.8c-0.5,5.2-3.8,13-5.8,14.9c-2,1.9-3.7,1.9-5.9,0.8 c-2.1-1.1-2.2-4-0.5-7.1c1.6-3,3.1-7.2,3.5-10.4c0.4-3.2,4-4.4,5.6-3.6C81.4,49.1,83.1,50.6,82.7,53.8z"/><text x="0" y="115" fill="#000000" font-size="5px" font-weight="bold" font-family="'Helvetica Neue', Helvetica, Arial-Unicode, Arial, Sans-serif">Created by Johannes S</text><text x="0" y="120" fill="#000000" font-size="5px" font-weight="bold" font-family="'Helvetica Neue', Helvetica, Arial-Unicode, Arial, Sans-serif">from the Noun Project</text></svg>
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 24.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 100 125" style="enable-background:new 0 0 100 125;" xml:space="preserve">
<path d="M93,49.1c-2.4-0.5-4.5-1.5-5.1-4.4c-0.5-2.9,1.8-4,4.1-5.3c0.7-0.4,1.2-0.8,1.4-1.2c-0.2-0.7-0.4-1.4-0.6-2.1
c-0.5-0.3-1.1-0.5-2.1-0.4c-2.5,0.3-4.8,0-6.2-2.6c-1.4-2.6,0.5-4.3,2.2-6.3c0.5-0.6,0.8-1.1,0.9-1.4c-0.4-0.6-0.9-1.3-1.3-1.9
c-0.5-0.1-1.2,0-2,0.3c-2.3,1-4.6,1.5-6.7-0.6c-2.1-2-0.9-4.3,0.2-6.7c0.3-0.6,0.4-1.2,0.4-1.5c-0.6-0.5-1.3-1-2-1.5
c-0.5,0.1-1,0.4-1.6,0.9c-1.8,1.7-3.9,2.8-6.5,1.5s-2.2-3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-3.9,10.7-8.3,13.4-8.6
c2.7-0.3,4.2,0.6,5.2,2.8c1.1,2.1-0.6,4.5-3.8,6c-3.1,1.5-6.8,4-9,6.4C29.7,36.4,26.1,35.3,25.2,33.7z M60.7,77.5
c-0.3,1.8-1.1,3.9-4.3,4.5c-5.1,1.2-13.5,0.4-15.9-0.9s-3-3-2.6-5.3c0.4-2.4,3.1-3.3,6.6-2.6c3.4,0.6,7.8,0.8,11,0.1
C58.8,72.7,61,75.7,60.7,77.5z M50,68c-9.9,0-18-8.1-18-18s8.1-18,18-18s18,8.1,18,18S59.9,68,50,68z M68.3,34.2
c-2.4-2.5-5.9-5.2-8.8-6.6c-2.9-1.4-3-5.2-1.7-6.5c1.3-1.3,3.2-2.5,6.1-1.1c4.8,2.1,11.2,7.6,12.3,10.1c1.2,2.5,0.7,4.1-1,5.8
C73.6,37.6,70.8,36.8,68.3,34.2z M82.7,53.8c-0.5,5.2-3.8,13-5.8,14.9c-2,1.9-3.7,1.9-5.9,0.8c-2.1-1.1-2.2-4-0.5-7.1
c1.6-3,3.1-7.2,3.5-10.4s4-4.4,5.6-3.6C81.4,49.1,83.1,50.6,82.7,53.8z"/>
</svg>

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@@ -1,5 +1,5 @@
DROP DATABASE IF EXISTS spellsaurus;
CREATE DATABASE spellsaurus;
CREATE DATABASE spellsaurus CHARACTER SET utf8 COLLATE utf8_bin;
USE spellsaurus;
/* ==== PRIMARY TABLES ==== */

View File

@@ -1,3 +1,5 @@
SET NAMES utf8;
-- META SCHOOLS
INSERT INTO `meta_school` VALUES
(1, 'Magies blanches', 'Magies disciplinant les arts de soins et de lumières.'),

View File

@@ -23,9 +23,3 @@ const server = app.listen( port, () => {console.log(`App listening on port ${por
// Routing
app.use('/api/spells', routes.spells)
// On process kill with SIGINT
process.on('SIGINT', () => {
db.end()
server.close()
})

31
package-lock.json generated
View File

@@ -1,6 +1,8 @@
{
"requires": true,
"name": "auracle-api",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"accepts": {
"version": "1.3.7",
@@ -235,16 +237,16 @@
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
},
"mime-db": {
"version": "1.43.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.43.0.tgz",
"integrity": "sha512-+5dsGEEovYbT8UY9yD7eE4XTc4UwJ1jBYlgaQQF38ENsKR3wj/8q8RFZrF9WIZpB2V1ArTVFUva8sAul1NzRzQ=="
"version": "1.44.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz",
"integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg=="
},
"mime-types": {
"version": "2.1.26",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.26.tgz",
"integrity": "sha512-01paPWYgLrkqAyrlDorC1uDwl2p3qZT7yl806vW7DvDoxwXi46jsjFbg+WdwotBIk6/MbEhO/dh5aZ5sNj/dWQ==",
"version": "2.1.27",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz",
"integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==",
"requires": {
"mime-db": "1.43.0"
"mime-db": "1.44.0"
}
},
"ms": {
@@ -268,19 +270,6 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
"node": {
"version": "13.10.1",
"resolved": "https://registry.npmjs.org/node/-/node-13.10.1.tgz",
"integrity": "sha512-xVeLmv6T+Cf1iPGDRhi4JOxGeV7jSYjacAWXDuHcZpc38qHfGaTdb6EpPYM9pKFvTThaYbb6/sq8JLv7DS+MHA==",
"requires": {
"node-bin-setup": "^1.0.0"
}
},
"node-bin-setup": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/node-bin-setup/-/node-bin-setup-1.0.6.tgz",
"integrity": "sha512-uPIxXNis1CRbv1DwqAxkgBk5NFV3s7cMN/Gf556jSw6jBvV7ca4F9lRL/8cALcZecRibeqU+5dFYqFFmzv5a0Q=="
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",

34
package.json Normal file
View File

@@ -0,0 +1,34 @@
{
"name": "auracle-api",
"version": "1.0.0",
"description": "API for Auracle database",
"main": "index.js",
"scripts": {
"test": "start"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AlexisNP/spellsaurus.git"
},
"keywords": [
"api",
"rpg",
"spells",
"magic",
"database",
"ttrpg",
"dices",
"worldbuilding"
],
"author": "AlexisNP",
"license": "ISC",
"bugs": {
"url": "https://github.com/AlexisNP/spellsaurus/issues"
},
"homepage": "https://github.com/AlexisNP/spellsaurus#readme",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"mysql": "^2.18.1"
}
}

View File

@@ -6,23 +6,64 @@ let router = express.Router()
const connection = require('../database/connection')
const db = connection.db
const getSpells = (params) => {
// Builds the associated infos for a given spell object
const buildSpell = async (spell) => {
// Fetches the spell's schools
let fetchSpellSchoolData = new Promise((resolve, reject) => {
let query =
"SELECT school.id, school.name " +
"FROM spells_schools AS sc " +
"INNER JOIN school AS school ON sc.id_school = school.id " +
"WHERE sc.id_spell = " + spell.id
db.query(query, (err, result) => {
if (err) return reject
resolve(result)
})
})
// Fetches the spell's variables
let fetchSpellVariablesData = new Promise((resolve, reject) => {
let query =
"SELECT variable.id, variable.description " +
"FROM spells_variables AS sv " +
"INNER JOIN variable AS variable ON sv.id_variable = variable.id " +
"WHERE sv.id_spell = " + spell.id
db.query(query, (err, result) => {
if (err) console.log(err)
resolve(result)
})
})
// Fetches the spell's ingredients
let fetchSpellIngredientsData = new Promise((resolve, reject) => {
let query =
"SELECT ingredient.id, ingredient.name " +
"FROM spells_ingredients AS si " +
"INNER JOIN ingredient AS ingredient ON si.id_ingredient = ingredient.id " +
"WHERE si.id_spell = " + spell.id
db.query(query, (err, result) => {
if (err) console.log(err)
resolve(result)
})
})
// Builds the spell and returns it
spell.schools = await fetchSpellSchoolData
spell.variables = await fetchSpellVariablesData
spell.ingredients = await fetchSpellIngredientsData
return spell
}
const getSpells = () => {
let fetchSpellsData = new Promise((resolve, reject) => {
let query =
"SELECT DISTINCT * " +
"FROM spell " +
"WHERE 1 = 1 "
// Checks if the params object has items
if (!(Object.keys(params).length === 0 && params.constructor === Object)) {
if (params.id) {
query += "AND id = " + params.id + " "
}
if (params.name) {
query += "AND name = " + params.name + " "
}
}
"FROM spell "
db.query(query, async (err, result) => {
if (err) { return reject }
@@ -30,68 +71,35 @@ const getSpells = (params) => {
// Loops over the results to fetch the associated tables
for (let i = 0; i < result.length; i++) {
let currentSpell_ID = result[i].id
// Fetches the spell's schools
let fetchSpellSchoolData = new Promise((resolve, reject) => {
let query =
"SELECT school.name " +
"FROM spells_schools AS sc " +
"INNER JOIN school AS school ON sc.id_school = school.id " +
"WHERE sc.id_spell = " + currentSpell_ID
db.query(query, (err, result) => {
if (err) return reject
resolve(result)
})
})
// Fetches the spell's variables
let fetchSpellVariablesData = new Promise((resolve, reject) => {
let query =
"SELECT variable.description " +
"FROM spells_variables AS sv " +
"INNER JOIN variable AS variable ON sv.id_variable = variable.id " +
"WHERE sv.id_spell = " + currentSpell_ID
db.query(query, (err, result) => {
if (err) console.log(err)
resolve(result)
})
})
// Fetches the spell's ingredients
let fetchSpellIngredientsData = new Promise((resolve, reject) => {
let query =
"SELECT ingredient.name " +
"FROM spells_ingredients AS si " +
"INNER JOIN ingredient AS ingredient ON si.id_ingredient = ingredient.id " +
"WHERE si.id_spell = " + currentSpell_ID
db.query(query, (err, result) => {
if (err) console.log(err)
resolve(result)
})
})
result[i].schools = await fetchSpellSchoolData
result[i].variables = await fetchSpellVariablesData
result[i].ingredients = await fetchSpellIngredientsData
result[i] = await buildSpell(result[i])
}
resolve(result)
})
})
return fetchSpellsData
}
router
.get('/', async (req, res, next) => {
const getSpell = (id) => {
let fetchSpellData = new Promise((resolve, reject) => {
let query =
"SELECT FROM spell where id = " + id
db.query(query, async (err, result) => {
if (err) { return reject }
resolve(result);
})
})
return fetchSpellData
}
// ROUTES
// ALL SPELLS METHODS
// Router
router.get('/', async (req, res, next) => {
getSpells(req.query)
.then(v => {
res.setHeader('Content-Type', 'application/json')
res.setHeader('Content-Type', 'application/json;charset=utf-8')
res.end(JSON.stringify(v))
})
.catch(err => {
@@ -100,5 +108,22 @@ router
})
})
// ONE SPELL METHODS
// Regex for param validation
const regex = RegExp(/^[1-9]\d*$/);
// Param validation
router.param('id', (req, res, next, id) => {
if (regex.test(id)) {
next()
} else {
res.status(403).send('The id parameter should be an integer.')
}
})
// Router
router.get('/:id/', async (req, res, next) => {
console.log("id")
})
module.exports = router