Skip to content

Commit cc22efd

Browse files
committed
docs(server): add some comments and correct value
Change wrong value in README Describe server code in server.ts Add one line in postAction.ts
1 parent 7e1c374 commit cc22efd

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This project is just a hobby project with potential bugs and security flaws. Do
2020

2121
The server assumes that the communication is over HTTP and is insecure, thus it uses JWT HS256-signed tokens to communicate and a session counter so that a request can't be copied by an attacker.
2222

23-
There is a built-in session counter that provides basic security. The session counter has a session limit of 10 and blocks new sessions after 10 sessions but you can increase this limit in the code. This is to protect against memory attacks.
23+
There is a built-in session counter that provides basic security. The session counter has a session limit of 20 and blocks new sessions after 10 sessions but you can increase this limit in the code. This is to protect against memory attacks.
2424

2525
The counter is incremented with each request and the counter value is included in the JWT which makes request forging impossible without the secret.
2626

server/routes/postAction.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ import jwt from "jwt-simple"
44

55
export function postAction(req: Request, res: Response) {
66
let result = false
7+
// assume request is verified
78
const request = jwt.decode(req.body.request, "", true)
89
const keycode = request.keycode
910
if (keycode) {
11+
// parseInt makes it impossible to inject code here.
1012
exec(
1113
`osascript -l JavaScript -e "Application('System Events').keyCode(${parseInt(
1214
keycode
1315
)})"`
1416
)
1517
res.json({ message: "Your keycode has been sent" })
1618
result = true
19+
// debug
1720
console.log(req.body.keycode)
1821
}
22+
// to make sure a response is sent back
1923
if (!result) res.json({ message: "noop" })
2024
}

server/server.ts

+12
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,34 @@ const sessionCounter: { [key: string]: number } = {}
1313
app.use(bodyParser.json())
1414
app.use(bodyParser.urlencoded({ extended: true }))
1515

16+
// sessionId middleware
1617
app.use((req, res, next) => {
18+
// sessionId is in the body?
1719
if (req.body && req.body.sessionId) {
1820
if (req.body.sessionId.length > 300)
21+
// we don't want overflow issues
1922
return res.status(403).send({
2023
message: "SessionId length is too long",
2124
code: Errors.SessionIdTooLong,
2225
})
26+
// if the sessionId is already being tracked
2327
if (req.body.sessionId in sessionCounter) next()
2428
else {
2529
if (Object.keys(sessionCounter).length > 20)
30+
// too many sessions, abort
2631
res.status(500).send({
2732
message:
2833
"Session limit exceeded, please restart server or manually purge sessions",
2934
code: Errors.SessionLimitExceeded,
3035
})
36+
// if not, start tracking with counter 0
3137
else {
3238
sessionCounter[req.body.sessionId] = 0
3339
next()
3440
}
3541
}
3642
} else {
43+
// cant do anything without a sessionId, sorry
3744
res.status(403).send({
3845
message: "You are missing the sessionId in the body",
3946
code: Errors.MissingSessionId,
@@ -44,11 +51,14 @@ app.use((req, res, next) => {
4451
app.use((req, res, next) => {
4552
if (req.body && req.body.request) {
4653
try {
54+
// verify JWT
4755
let payload = jwt.decode(req.body.request, secret, false, "HS256")
4856
if (payload.counter === sessionCounter[req.body.sessionId]) {
57+
// increment session counter and go on, everything is fine
4958
sessionCounter[req.body.sessionId]++
5059
next()
5160
} else {
61+
// the counter value is unsynchronized, or somebody is trying to forge requests, in any case reject
5262
res.status(403).send({
5363
message:
5464
"Sorry, you sent the wrong counter value, your current session counter is at " +
@@ -58,12 +68,14 @@ app.use((req, res, next) => {
5868
})
5969
}
6070
} catch (e) {
71+
// hmm, wrong token
6172
res.status(403).send({
6273
message: "Could not verify the token.",
6374
code: Errors.FailedTokenVerify,
6475
})
6576
}
6677
} else {
78+
// no request JWT was sent?
6779
res.status(403).send({
6880
message:
6981
"You need to send a request in the body as a JWT verified with the agreed secret with HS256.",

0 commit comments

Comments
 (0)