Skip to content

Commit a3734f9

Browse files
authored
Create permission library (#26)
- conf: move session config into http.yml - permission: create,get,put,delete,destroy & tests, fixes #20 - chore(config): guard against prototype pollution
1 parent 62789cb commit a3734f9

File tree

14 files changed

+283
-145
lines changed

14 files changed

+283
-145
lines changed

.npmignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.github
2+
.DS_Store
3+
.editorconfig
4+
.gitignore
5+
.gitmodules
6+
.lgtm.yml
7+
appveyor.yml
8+
codecov.yml
9+
.release
10+
.travis.yml
11+
.eslintrc.yaml
12+
.eslintrc.json
13+
.codeclimate.yml
14+
test/
15+
DEVELOP.md
16+
.prettierrc.yml

conf.d/http.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,33 @@
22
default:
33
host: localhost
44
port: 3000
5+
cookie:
6+
# https://hapi.dev/module/cookie/api/?v=12.0.1
7+
name: sid-nictool
8+
password: af1b926a5e21f535c4f5b6c42941c4cf
9+
ttl: 3600000 # 1 hour
10+
# domain:
11+
path: /
12+
clearInvalid: true
13+
isSameSite: Strict
14+
isSecure: true
15+
isHttpOnly: true
16+
keepAlive: false
17+
# redirectTo:
18+
group: NicTool
19+
20+
production:
21+
port: 8080
22+
cookie:
23+
# Set your own secret password. hint: openssl rand -hex 16
24+
# password:
25+
26+
test:
27+
cookie:
28+
isSecure: false
29+
password: ^NicTool.Is,The#Best_Dns-Manager$
30+
31+
development:
32+
cookie:
33+
isSecure: false
34+
password: ^NicTool.Is,The#Best_Dns-Manager$

conf.d/session.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Config {
4848

4949
function applyDefaults(cfg = {}, defaults = {}) {
5050
for (const d in defaults) {
51+
/* c8 ignore next */
52+
if (d === '__proto__' || d === 'constructor') continue
5153
if ([undefined, null].includes(cfg[d])) {
5254
cfg[d] = defaults[d]
5355
} else if (typeof cfg[d] === 'object' && typeof defaults[d] === 'object') {

lib/config.test.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ describe('config', () => {
2727
process.env.NODE_DEBUG = ''
2828
})
2929

30-
it(`loads session test config`, async () => {
31-
const cfg = await Config.get('session', 'test')
32-
assert.deepEqual(cfg, sessCfg)
33-
})
34-
35-
it(`loads session test config syncronously`, () => {
36-
const cfg = Config.getSync('session', 'test')
37-
assert.deepEqual(cfg, sessCfg)
30+
it(`loads http test config`, async () => {
31+
const cfg = await Config.get('http', 'test')
32+
assert.deepEqual(cfg, httpCfg)
3833
})
3934

4035
it(`loads http test config syncronously`, () => {
@@ -68,7 +63,9 @@ const mysqlTestCfg = {
6863
decimalNumbers: true,
6964
}
7065

71-
const sessCfg = {
66+
const httpCfg = {
67+
host: 'localhost',
68+
port: 3000,
7269
cookie: {
7370
clearInvalid: true,
7471
isHttpOnly: true,
@@ -80,9 +77,5 @@ const sessCfg = {
8077
ttl: 3600000,
8178
},
8279
keepAlive: false,
83-
}
84-
85-
const httpCfg = {
86-
host: 'localhost',
87-
port: 3000,
80+
group: 'NicTool',
8881
}

lib/mysql.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class Mysql {
7373
}
7474

7575
whereConditions(query, params) {
76+
let newQuery = query
7677
let paramsArray = []
7778

7879
if (Array.isArray(params)) {
@@ -81,13 +82,13 @@ class Mysql {
8182
// Object to WHERE conditions
8283
let first = true
8384
for (const p in params) {
84-
if (!first) query += ' AND'
85-
query += ` ${p}=?`
85+
if (!first) newQuery += ' AND'
86+
newQuery += ` ${p}=?`
8687
paramsArray.push(params[p])
8788
first = false
8889
}
8990
}
90-
return [query, paramsArray]
91+
return [newQuery, paramsArray]
9192
}
9293

9394
async delete(query, params) {

0 commit comments

Comments
 (0)