-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server: Added form tokens to prevent CSRF attacks
- Loading branch information
Showing
8 changed files
with
82 additions
and
16 deletions.
There are no files selected for viewing
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,37 @@ | ||
import { ErrorForbidden } from './errors'; | ||
import { escapeHtml } from './htmlUtils'; | ||
import { bodyFields, isApiRequest } from './requestUtils'; | ||
import { AppContext } from './types'; | ||
|
||
interface BodyWithCsrfToken { | ||
_csrf: string; | ||
} | ||
|
||
export async function csrfCheck(ctx: AppContext, isPublicRoute: boolean) { | ||
if (isApiRequest(ctx)) return; | ||
if (isPublicRoute) return; | ||
if (!['POST', 'PUT'].includes(ctx.method)) return; | ||
if (ctx.path === '/logout') return; | ||
|
||
const userId = ctx.joplin.owner ? ctx.joplin.owner.id : ''; | ||
if (!userId) return; | ||
|
||
const fields = await bodyFields<BodyWithCsrfToken>(ctx.req); | ||
if (!fields._csrf) throw new ErrorForbidden('CSRF token is missing'); | ||
|
||
if (!(await ctx.joplin.models.token().isValid(userId, fields._csrf))) { | ||
throw new ErrorForbidden(`Invalid CSRF token: ${fields._csrf}`); | ||
} | ||
|
||
await ctx.joplin.models.token().deleteByValue(userId, fields._csrf); | ||
} | ||
|
||
export async function createCsrfToken(ctx: AppContext) { | ||
if (!ctx.joplin.owner) throw new Error('Cannot create CSRF token without a user'); | ||
return ctx.joplin.models.token().generate(ctx.joplin.owner.id); | ||
} | ||
|
||
export async function createCsrfTag(ctx: AppContext) { | ||
const token = await createCsrfToken(ctx); | ||
return `<input type="hidden" name="_csrf" value="${escapeHtml(token)}"/>`; | ||
} |
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