Skip to content

Commit 7025111

Browse files
committed
adds server-validated white-list rethinkdb queries
1 parent eb6870e commit 7025111

4 files changed

Lines changed: 64 additions & 22 deletions

File tree

‎server/App.ls‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ require! {
2222
pe = new PrettyError!
2323
env = process.env.NODE_ENV or \development
2424

25-
# connect to rethinkdb
26-
[keys, db, db-host, db-port, http-path] =
27-
[[process.env.npm_package_config_keys_0 or \AEeaEUA3152589], # XXX only using first
25+
[keys, db, db-host, db-port] =
26+
[process.env.npm_package_config_keys_0 or \AEeaEUA3152589],
2827
process.env.npm_package_config_database or \test,
2928
process.env.npm_package_config_domain or \develop.com,
30-
process.env.npm_package_config_rethinkdb_port or 28015,
31-
'/db']
32-
connection = rethinkdb {db, db-host, db-port}
33-
store = new mw.rethinkdb-koa-session {connection, db}
34-
co <| init-rethinkdb db, connection # init rethinkdb tables & indexes
29+
process.env.npm_package_config_rethinkdb_port or 28015
30+
31+
# connect to rethinkdb
32+
connection = rethinkdb {db, db-host, db-port}
33+
store = new mw.rethinkdb-koa-session {connection, db}
34+
global.run-query = -> connection.db db # for whitelist, etc...
35+
co <| init-rethinkdb db, connection # init rethinkdb tables & indexes
3536

3637
### App's purpose is to abstract instantiation from starting & stopping
3738
module.exports =

‎server/query-whitelist.ls‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,61 @@ require! {
33
\rethinkdb-websocket-server : {r, RP}
44
}
55

6+
# XXX rethinkdb-websocket-server uses Promises
7+
68
module.exports = [
9+
# everyone table
10+
r.table \everyone
11+
.order-by {index: (r.desc \date)}
12+
.changes {+include-states, +include-initial}
13+
r.table \everyone # new TODO
14+
.insert (refs [\name \title \completed \date])
15+
.validate session-name
16+
r.table \everyone # update TODO
17+
.get (RP.ref \id)
18+
.update (refs [\completed \date \id \name \title])
19+
.validate session-name
20+
r.table \everyone
21+
.get (RP.ref \id)
22+
.update (refs [\completed \date \id \name \title \completed-at])
23+
.validate session-name
24+
25+
# session table
726
r.table \sessions
8-
.filter {sid: RP.ref \sid}
9-
.validate (({sid}, session) -> session.sid === sid)
27+
.get (RP.ref \sid)
28+
.changes {+include-states, +include-initial}
1029
r.table \sessions
1130
.changes {+include-states, +include-initial}
12-
.validate (({sid}, session) -> console.log \sid: sid, \ref: (RP.ref \sid), \session: session; session.sid === sid)
13-
# // List moves for a game with changefeed
14-
# RQ(
15-
# RQ.CHANGES(
16-
# RQ.FILTER(
17-
# RQ.TABLE("moves"),
18-
# {"gameId": x => typeof x === 'string'}
19-
# )
20-
# ).opt("include_states", true).opt("include_initial", true)
21-
# ).opt("db", RQ.DB(cfg.dbName)),
31+
.validate session-id
32+
r.table \sessions # add TODO
33+
.get (RP.ref \id)
34+
.update (refs [\cookie \id \onPage \sid \todos])
35+
.validate session-auth-token
36+
r.table \sessions # update TODO
37+
.get (RP.ref \id)
38+
.update (refs [\cookie \id \onPage \sid \name \todos])
39+
.validate session-auth-token
40+
r.table \sessions # update name
41+
.get (RP.ref \id)
42+
.update (refs [\cookie \id \onPage \sid \name])
43+
.validate session-auth-token
44+
r.table \sessions
45+
.filter {sid: RP.ref \sid}
46+
.validate session-id
2247
]
48+
49+
function session-id {sid} session
50+
session.sid is sid
51+
52+
function session-auth-token {sid}, {auth-token}:rethinkdb-session
53+
console.log sid, auth-token
54+
auth-token is sid
55+
56+
function session-name {name}, {auth-token}:rethinkdb-session
57+
[session] <- run-query!table \sessions .get-all auth-token, {index: \sid} .then
58+
session.name ||= \Anonymous is name
59+
60+
function refs names=[]
61+
prev, cur <- fold _, {} names
62+
prev[cur] = RP.ref cur
63+
prev

‎shared/features.ls‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ module.exports = {
1919
+static-assets # disable to run in a separate process
2020
offline: false # disable offline unless necessary (prevent glorious caching)
2121

22-
unsafely-allow-any-query: dev # when true, whitelist is used
22+
unsafely-allow-any-query: dev # when true, whitelist is used to server-validate rethinkdb queries
2323
}

‎shared/react/App.ls‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = component \App (props) ->
3636
props.get-in [\locals \domain]
3737
port = if window?location.port then that else (if secure then 443 else 80)
3838
unless global.RethinkSession then global.RethinkSession = new Session!
39-
..connect {host, port, secure, path: '/db', auto-reconnect-delay-ms: 2000ms}
39+
..connect {host, port, secure, path: '/db', auto-reconnect-delay-ms: 1500ms}
4040
..once-done-loading ~>
4141
# XXX on server, response already sent without session
4242

0 commit comments

Comments
 (0)