-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[7641] Update share path to {parent_id}/{note_id} #7759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are parts of #7828 here that should not be present in this PR. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ import searchService from "../services/search/services/search.js"; | |
| import SearchContext from "../services/search/search_context.js"; | ||
| import type SNote from "./shaca/entities/snote.js"; | ||
| import type SAttachment from "./shaca/entities/sattachment.js"; | ||
| import { getDefaultTemplatePath, renderNoteContent } from "./content_renderer.js"; | ||
| import { getDefaultTemplatePath, getSharedSubTreeRoot, renderNoteContent } from "./content_renderer.js"; | ||
| import utils from "../services/utils.js"; | ||
|
|
||
| function addNoIndexHeader(note: SNote, res: Response) { | ||
|
|
@@ -60,6 +60,20 @@ function checkNoteAccess(noteId: string, req: Request, res: Response) { | |
| const header = req.header("Authorization"); | ||
|
|
||
| if (!header?.startsWith("Basic ")) { | ||
| if (req.path.startsWith("/share/api") && note.contentAccessor) { | ||
| let contentAccessToken = "" | ||
| if (note.contentAccessor.type === "cookie") contentAccessToken += req.cookies["trilium.cat"] || "" | ||
| else if (note.contentAccessor.type === "query") contentAccessToken += req.query['cat'] || "" | ||
|
|
||
| if (contentAccessToken){ | ||
| if (note.contentAccessor.isTokenValid(contentAccessToken)){ | ||
| return note | ||
| } | ||
| res.status(401).send("Access is expired. Return back and update the page."); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -124,9 +138,14 @@ function register(router: Router) { | |
| return; | ||
| } | ||
|
|
||
| if (note.isLabelTruthy("shareExclude")) { | ||
| res.status(404); | ||
| render404(res); | ||
| return; | ||
| } | ||
|
|
||
| if (!checkNoteAccess(note.noteId, req, res)) { | ||
| requestCredentials(res); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
|
|
@@ -138,6 +157,10 @@ function register(router: Router) { | |
| return; | ||
| } | ||
|
|
||
| if (note.contentAccessor && note.contentAccessor.type === "cookie") { | ||
| res.cookie('trilium.cat', note.contentAccessor.getToken(), { maxAge: note.contentAccessor.getTokenExpiration() * 1000, httpOnly: true }) | ||
| } | ||
|
|
||
| res.send(renderNoteContent(note)); | ||
| } | ||
|
|
||
|
|
@@ -157,14 +180,29 @@ function register(router: Router) { | |
| renderNote(shaca.shareRootNote, req, res); | ||
| }); | ||
|
|
||
| router.get("/share/:parentShareId/:shareId", (req, res) => { | ||
| shacaLoader.ensureLoad(); | ||
|
|
||
| const { parentShareId, shareId } = req.params; | ||
|
|
||
| const note = shaca.aliasToNote[shareId] || shaca.notes[shareId]; | ||
| if (note){ | ||
| note.parentId = parentShareId | ||
| note.initContentAccessor() | ||
| } | ||
|
|
||
| renderNote(note, req, res); | ||
| }); | ||
|
|
||
| router.get("/share/:shareId", (req, res) => { | ||
| shacaLoader.ensureLoad(); | ||
|
|
||
| const { shareId } = req.params; | ||
|
|
||
| const note = shaca.aliasToNote[shareId] || shaca.notes[shareId]; | ||
| const parent = getSharedSubTreeRoot(note) | ||
|
|
||
| renderNote(note, req, res); | ||
| res.redirect(`${parent?.note?.noteId}/${shareId}`) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be nice here to not force the note ID of the parent, but to also allow the share alias. So instead of: |
||
| }); | ||
|
|
||
| router.get("/share/api/notes/:noteId", (req, res) => { | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is part of #7828 and should not be present in this PR. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import crypto from "crypto"; | ||
| import SNote from "./snote"; | ||
| import utils from "../../../services/utils"; | ||
|
|
||
| const DefaultAccessTimeoutSec = 10 * 60; // 10 minutes | ||
|
|
||
| export class ContentAccessor { | ||
| note: SNote; | ||
| token: string; | ||
| timestamp: number; | ||
| type: string; | ||
| timeout: number; | ||
| key: Buffer; | ||
|
|
||
| constructor(note: SNote) { | ||
| this.note = note; | ||
| this.key = crypto.randomBytes(32); | ||
| this.token = ""; | ||
| this.timestamp = 0; | ||
| this.timeout = Number(this.note.getAttributeValue("label", "shareAccessTokenTimeout") || DefaultAccessTimeoutSec) | ||
|
|
||
| switch (this.note.getAttributeValue("label", "shareContentAccess")) { | ||
| case "basic": this.type = "basic"; break | ||
| case "query": this.type = "query"; break | ||
| default: this.type = "cookie"; break | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| __encrypt(text: string) { | ||
| const iv = crypto.randomBytes(16); | ||
| const cipher = crypto.createCipheriv('aes-256-cbc', this.key, iv); | ||
| let encrypted = cipher.update(text, 'utf8', 'hex'); | ||
| encrypted += cipher.final('hex'); | ||
| return iv.toString('hex') + encrypted; | ||
| } | ||
|
|
||
| __decrypt(encryptedText: string) { | ||
| try { | ||
| const iv = Buffer.from(encryptedText.slice(0, 32), 'hex'); | ||
| const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, iv); | ||
| let decrypted = decipher.update(encryptedText.slice(32), 'hex', 'utf8'); | ||
| decrypted += decipher.final('utf8'); | ||
| return decrypted; | ||
| } catch { | ||
| return "" | ||
| } | ||
| } | ||
|
|
||
| __compare(originalText: string, encryptedText: string) { | ||
| return originalText === this.__decrypt(encryptedText) | ||
| } | ||
|
|
||
| update() { | ||
| if (new Date().getTime() < this.timestamp + this.getTimeout() * 1000) return | ||
| this.token = utils.randomString(36); | ||
| this.key = crypto.randomBytes(32); | ||
| this.timestamp = new Date().getTime(); | ||
| } | ||
|
|
||
| isTokenValid(encToken: string) { | ||
| return this.__compare(this.token, encToken) && new Date().getTime() < this.timestamp + this.getTimeout() * 1000; | ||
| } | ||
|
|
||
| getToken() { | ||
| return this.__encrypt(this.token); | ||
| } | ||
|
|
||
| getTokenExpiration() { | ||
| return (this.timestamp + (this.timeout * 1000) - new Date().getTime()) /1000; | ||
| } | ||
|
|
||
| getTimeout() { | ||
| return this.timeout; | ||
| } | ||
|
|
||
| getContentAccessType() { | ||
| return this.type; | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is part of #7828 and should not be present in this PR.