Improve data validation for channel names
This commit is contained in:
parent
adf3cdf771
commit
29e7e2a297
5 changed files with 19 additions and 5 deletions
|
|
@ -1,16 +1,24 @@
|
||||||
import { ServerRoute } from "@hapi/hapi";
|
import { ServerRoute } from "@hapi/hapi";
|
||||||
import { log } from "$/main";
|
import { log } from "$/main";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import Joi from "joi";
|
||||||
|
import { channelSchema } from "$/schemas/general";
|
||||||
|
|
||||||
const route: ServerRoute = {
|
const route: ServerRoute = {
|
||||||
method: `GET`, path: `/{channel}/overlay/{theme}/{path*}`,
|
method: `GET`, path: `/{channel}/overlay/{theme}/{path*}`,
|
||||||
options: {
|
options: {
|
||||||
|
validate: {
|
||||||
|
params: Joi.object({
|
||||||
|
channel: channelSchema,
|
||||||
|
theme: Joi.string().pattern(/^[a-z0-9\-]+$/),
|
||||||
|
path: Joi.string().optional(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
files: {
|
files: {
|
||||||
relativeTo: path.join(process.cwd(), `site`),
|
relativeTo: path.join(process.cwd(), `site`),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler(request, h) {
|
handler(request, h) {
|
||||||
// const theme = request.query.theme;
|
|
||||||
const path = request.params.path;
|
const path = request.params.path;
|
||||||
const theme = request.params.theme.replace(/\-/g, `/`);
|
const theme = request.params.theme.replace(/\-/g, `/`);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { anonymizePhrase, convertToKey, spacePhrase } from "$/utils/game";
|
import { anonymizePhrase, convertToKey, spacePhrase } from "$/utils/game";
|
||||||
|
import { channelSchema } from "$/schemas/general";
|
||||||
import { config, database } from "$/main";
|
import { config, database } from "$/main";
|
||||||
import { ServerRoute } from "@hapi/hapi";
|
import { ServerRoute } from "@hapi/hapi";
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
|
|
@ -10,7 +11,7 @@ const route: ServerRoute = {
|
||||||
options: {
|
options: {
|
||||||
validate: {
|
validate: {
|
||||||
params: Joi.object({
|
params: Joi.object({
|
||||||
channel: Joi.string().alphanum(),
|
channel: channelSchema,
|
||||||
}),
|
}),
|
||||||
query: Joi.object({
|
query: Joi.object({
|
||||||
word_list: Joi.string(),
|
word_list: Joi.string(),
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { database } from "$/main";
|
import { channelSchema } from "$/schemas/general";
|
||||||
import { ServerRoute } from "@hapi/hapi";
|
import { ServerRoute } from "@hapi/hapi";
|
||||||
|
import { database } from "$/main";
|
||||||
import Joi from "joi";
|
import Joi from "joi";
|
||||||
|
|
||||||
const route: ServerRoute = {
|
const route: ServerRoute = {
|
||||||
|
|
@ -7,7 +8,7 @@ const route: ServerRoute = {
|
||||||
options: {
|
options: {
|
||||||
validate: {
|
validate: {
|
||||||
payload: Joi.object({
|
payload: Joi.object({
|
||||||
channel: Joi.string().alphanum(),
|
channel: channelSchema,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { channelSchema } from "$/schemas/general";
|
||||||
import { config, database } from "$/main";
|
import { config, database } from "$/main";
|
||||||
import { ServerRoute } from "@hapi/hapi";
|
import { ServerRoute } from "@hapi/hapi";
|
||||||
import Joi from "joi";
|
import Joi from "joi";
|
||||||
|
|
@ -7,7 +8,7 @@ const route: ServerRoute = {
|
||||||
options: {
|
options: {
|
||||||
validate: {
|
validate: {
|
||||||
params: Joi.object({
|
params: Joi.object({
|
||||||
channel: Joi.string().alphanum(),
|
channel: channelSchema,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
3
src/schemas/general.ts
Normal file
3
src/schemas/general.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
import Joi from "joi";
|
||||||
|
|
||||||
|
export const channelSchema = Joi.string().pattern(/^[a-z0-9_\-]$/i);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue