-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
169 additions
and
57 deletions.
There are no files selected for viewing
Submodule plugin
updated
from 4ead2c to 961634
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { validateOrReject, ValidationError } from "class-validator"; | ||
import { NextFunction, Request, Response } from "express"; | ||
import { deleteNote, getNote } from "../../db/note.dao"; | ||
import checkId from "../../lib/checkUserId"; | ||
import EventLogger, { WriteEvent } from "../../logging/EventLogger"; | ||
import { getConnectingIp, getNoteSize } from "../../util"; | ||
import { NoteDeleteRequest } from "../../validation/Request"; | ||
|
||
export async function deleteNoteController( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
): Promise<void> { | ||
const event: WriteEvent = { | ||
success: false, | ||
host: getConnectingIp(req), | ||
user_id: req.body.user_id, | ||
user_plugin_version: req.body.plugin_version, | ||
}; | ||
|
||
// Validate request body | ||
const noteDeleteRequest = new NoteDeleteRequest(); | ||
Object.assign(noteDeleteRequest, req.body); | ||
try { | ||
await validateOrReject(noteDeleteRequest); | ||
} catch (_err: any) { | ||
const err = _err as ValidationError; | ||
res.status(400).send(err.toString()); | ||
event.error = err.toString(); | ||
await EventLogger.deleteEvent(event); | ||
return; | ||
} | ||
|
||
// Validate user ID, if present | ||
if (noteDeleteRequest.user_id && !checkId(noteDeleteRequest.user_id)) { | ||
console.log("invalid user id"); | ||
res.status(400).send("Invalid user id (checksum failed)"); | ||
event.error = "Invalid user id (checksum failed)"; | ||
EventLogger.writeEvent(event); | ||
return; | ||
} | ||
|
||
// get note from db | ||
const note = await getNote(req.params.id); | ||
if (!note) { | ||
res.status(404).send("Note not found"); | ||
event.error = "Note not found"; | ||
await EventLogger.deleteEvent(event); | ||
return; | ||
} | ||
|
||
// Validate secret token | ||
if (note.secret_token !== req.body.secret_token) { | ||
res.status(401).send("Invalid token"); | ||
event.error = "Invalid secret token"; | ||
await EventLogger.deleteEvent(event); | ||
return; | ||
} | ||
|
||
// Delete note | ||
try { | ||
await deleteNote(note.id); | ||
res.status(200); | ||
event.success = true; | ||
event.note_id = note.id; | ||
event.size_bytes = getNoteSize(note); | ||
await EventLogger.deleteEvent(event); | ||
} catch (err) { | ||
event.error = (err as Error).toString(); | ||
await EventLogger.deleteEvent(event); | ||
next(err); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { crc16 as crc } from "crc"; | ||
|
||
/** | ||
* @param id {string} a 16 character base16 string with 12 random characters and 4 CRC characters | ||
* @returns {boolean} true if the id is valid, false otherwise | ||
*/ | ||
export default function checkId(id: string): boolean { | ||
// check length | ||
if (id.length !== 16) { | ||
return false; | ||
} | ||
// extract the random number and the checksum | ||
const random = id.slice(0, 12); | ||
const checksum = id.slice(12, 16); | ||
|
||
// compute the CRC of the random number | ||
const computedChecksum = crc(random).toString(16).padStart(4, "0"); | ||
|
||
// compare the computed checksum with the one in the id | ||
return computedChecksum === checksum; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
IsBase64, | ||
IsHexadecimal, | ||
IsNotEmpty, | ||
Matches, | ||
ValidateIf, | ||
} from "class-validator"; | ||
|
||
abstract class NoteRequestBody { | ||
@ValidateIf((o) => o.user_id != null) | ||
@IsHexadecimal() | ||
user_id: string | undefined; | ||
|
||
@ValidateIf((o) => o.plugin_version != null) | ||
@Matches("^[0-9]+\\.[0-9]+\\.[0-9]+$") | ||
plugin_version: string | undefined; | ||
} | ||
|
||
export class NotePostRequest extends NoteRequestBody { | ||
@IsBase64() | ||
@IsNotEmpty() | ||
ciphertext: string | undefined; | ||
|
||
@IsBase64() | ||
@ValidateIf((o) => !o.iv) | ||
hmac?: string | undefined; | ||
|
||
@IsBase64() | ||
@ValidateIf((o) => !o.hmac) | ||
iv?: string | undefined; | ||
|
||
@Matches("^v[0-9]+$") | ||
crypto_version: string = "v1"; | ||
} | ||
|
||
export class NoteDeleteRequest extends NoteRequestBody { | ||
@IsBase64() | ||
@IsNotEmpty() | ||
secret_token: string | undefined; | ||
} |