-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9182 from weseek/fix/154290-154296-add-validators…
…-to-lsx-api fix: Add validators to lsx API
- Loading branch information
Showing
4 changed files
with
60 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,54 @@ | ||
import type { Request, Response } from 'express'; | ||
import type { NextFunction, Request, Response } from 'express'; | ||
import { query, validationResult } from 'express-validator'; | ||
import { FilterXSS } from 'xss'; | ||
|
||
import type { LsxApiOptions } from '../interfaces/api'; | ||
|
||
import { listPages } from './routes/list-pages'; | ||
|
||
const loginRequiredFallback = (req: Request, res: Response) => { | ||
return res.status(403).send('login required'); | ||
}; | ||
|
||
const filterXSS = new FilterXSS(); | ||
|
||
const lsxValidator = [ | ||
query('pagePath').notEmpty().isString(), | ||
query('offset').optional().isInt(), | ||
query('limit').optional().isInt(), | ||
query('options') | ||
.optional() | ||
.customSanitizer((options) => { | ||
try { | ||
const jsonData: LsxApiOptions = JSON.parse(options); | ||
|
||
Object.keys(jsonData).forEach((key) => { | ||
jsonData[key] = filterXSS.process(jsonData[key]); | ||
}); | ||
|
||
return jsonData; | ||
} | ||
catch (err) { | ||
throw new Error('Invalid JSON format in options'); | ||
} | ||
}), | ||
query('options.*').optional().isString(), | ||
]; | ||
|
||
const paramValidator = (req: Request, _: Response, next: NextFunction) => { | ||
const errObjArray = validationResult(req); | ||
if (errObjArray.isEmpty()) { | ||
return next(); | ||
} | ||
return new Error('Invalid lsx parameter'); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any | ||
const middleware = (crowi: any, app: any): void => { | ||
const loginRequired = crowi.require('../middlewares/login-required')(crowi, true, loginRequiredFallback); | ||
const accessTokenParser = crowi.require('../middlewares/access-token-parser')(crowi); | ||
|
||
app.get('/_api/lsx', accessTokenParser, loginRequired, listPages); | ||
app.get('/_api/lsx', accessTokenParser, loginRequired, lsxValidator, paramValidator, listPages); | ||
}; | ||
|
||
export default middleware; |
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