From ff2c1fe8133f9556f6aaa52058cd8b83c40085e6 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Tue, 22 May 2018 19:43:13 +0200 Subject: feature: IP filtering on signup page disable registration form on IP not in range checking the CIDR list before filtering with it placing the cidr filters as an attribute object in the config --- server/controllers/api/config.ts | 6 ++++-- server/controllers/api/users.ts | 2 ++ server/helpers/utils.ts | 36 ++++++++++++++++++++++++++++++++++ server/initializers/checker.ts | 4 +++- server/initializers/constants.ts | 8 +++++++- server/middlewares/validators/users.ts | 19 ++++++++++++++++-- 6 files changed, 69 insertions(+), 6 deletions(-) (limited to 'server') diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts index 12074a80e..f678e3c4a 100644 --- a/server/controllers/api/config.ts +++ b/server/controllers/api/config.ts @@ -4,7 +4,7 @@ import { ServerConfig, UserRight } from '../../../shared' import { About } from '../../../shared/models/server/about.model' import { CustomConfig } from '../../../shared/models/server/custom-config.model' import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils' -import { isSignupAllowed } from '../../helpers/utils' +import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils' import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers' import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares' import { customConfigUpdateValidator } from '../../middlewares/validators/config' @@ -36,6 +36,7 @@ configRouter.delete('/custom', async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) { const allowed = await isSignupAllowed() + const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip) const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS) .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true) @@ -54,7 +55,8 @@ async function getConfig (req: express.Request, res: express.Response, next: exp }, serverVersion: packageJSON.version, signup: { - allowed + allowed, + allowedForCurrentIP }, transcoding: { enabledResolutions diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 0a591f11d..8dff4b87c 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -19,6 +19,7 @@ import { authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, + ensureUserRegistrationAllowedForIP, paginationValidator, setDefaultPagination, setDefaultSort, @@ -106,6 +107,7 @@ usersRouter.post('/', usersRouter.post('/register', asyncMiddleware(ensureUserRegistrationAllowed), + ensureUserRegistrationAllowedForIP, asyncMiddleware(usersRegisterValidator), asyncMiddleware(registerUserRetryWrapper) ) diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index 058c3211e..e4556fa12 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -1,4 +1,6 @@ import { Model } from 'sequelize-typescript' +import * as ipaddr from 'ipaddr.js' +const isCidr = require('is-cidr') import { ResultList } from '../../shared' import { VideoResolution } from '../../shared/models/videos' import { CONFIG } from '../initializers' @@ -48,6 +50,39 @@ async function isSignupAllowed () { return totalUsers < CONFIG.SIGNUP.LIMIT } +function isSignupAllowedForCurrentIP (ip: string) { + const addr = ipaddr.parse(ip) + let excludeList = [ 'blacklist' ] + let matched: string + + // if there is a valid, non-empty whitelist, we exclude all unknown adresses too + if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) { + excludeList.push('unknown') + } + + if (addr.kind() === 'ipv4') { + const addrV4 = ipaddr.IPv4.parse(ip) + const rangeList = { + whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr)) + .map(cidr => ipaddr.IPv4.parseCIDR(cidr)), + blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr)) + .map(cidr => ipaddr.IPv4.parseCIDR(cidr)) + } + matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown') + } else if (addr.kind() === 'ipv6') { + const addrV6 = ipaddr.IPv6.parse(ip) + const rangeList = { + whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr)) + .map(cidr => ipaddr.IPv6.parseCIDR(cidr)), + blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr)) + .map(cidr => ipaddr.IPv6.parseCIDR(cidr)) + } + matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown') + } + + return !excludeList.includes(matched) +} + function computeResolutionsToTranscode (videoFileHeight: number) { const resolutionsEnabled: number[] = [] const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS @@ -99,6 +134,7 @@ export { generateRandomString, getFormattedObjects, isSignupAllowed, + isSignupAllowedForCurrentIP, computeResolutionsToTranscode, resetSequelizeInstance, getServerActor, diff --git a/server/initializers/checker.ts b/server/initializers/checker.ts index 5a9c603b5..6259c7b6c 100644 --- a/server/initializers/checker.ts +++ b/server/initializers/checker.ts @@ -27,7 +27,9 @@ function checkMissedConfig () { 'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache', 'log.level', 'user.video_quota', - 'cache.previews.size', 'admin.email', 'signup.enabled', 'signup.limit', 'transcoding.enabled', 'transcoding.threads', + 'cache.previews.size', 'admin.email', + 'signup.enabled', 'signup.limit', 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist', + 'transcoding.enabled', 'transcoding.threads', 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', 'instance.default_nsfw_policy', 'instance.robots', 'services.twitter.username', 'services.twitter.whitelisted' diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 424947590..a35306730 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -150,7 +150,13 @@ const CONFIG = { }, SIGNUP: { get ENABLED () { return config.get('signup.enabled') }, - get LIMIT () { return config.get('signup.limit') } + get LIMIT () { return config.get('signup.limit') }, + FILTERS: { + CIDR: { + get WHITELIST () { return config.get('signup.filters.cidr.whitelist') }, + get BLACKLIST () { return config.get('signup.filters.cidr.blacklist') } + } + } }, USER: { get VIDEO_QUOTA () { return config.get('user.video_quota') } diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 247b704c4..4ad0e33da 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -16,8 +16,8 @@ import { } from '../../helpers/custom-validators/users' import { isVideoExist } from '../../helpers/custom-validators/videos' import { logger } from '../../helpers/logger' -import { isSignupAllowed } from '../../helpers/utils' -import { CONSTRAINTS_FIELDS } from '../../initializers' +import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils' +import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' import { Redis } from '../../lib/redis' import { UserModel } from '../../models/account/user' import { areValidationErrors } from './utils' @@ -177,6 +177,20 @@ const ensureUserRegistrationAllowed = [ } ] +const ensureUserRegistrationAllowedForIP = [ + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + const allowed = isSignupAllowedForCurrentIP(req.ip) + + if (allowed === false) { + return res.status(403) + .send({ error: 'You are not on a network authorized for registration.' }) + .end() + } + + return next() + } +] + const usersAskResetPasswordValidator = [ body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'), @@ -230,6 +244,7 @@ export { usersUpdateMeValidator, usersVideoRatingValidator, ensureUserRegistrationAllowed, + ensureUserRegistrationAllowedForIP, usersGetValidator, usersUpdateMyAvatarValidator, usersAskResetPasswordValidator, -- cgit v1.2.3