Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Implemented origins & stricted validation for project

Blue Moused4513c23aaf8
diff --git a/src/project/dto/project.dto.ts b/src/project/dto/project.dto.ts
index f1cc9903a..180dc283e 100644
--- a/src/project/dto/project.dto.ts
+++ b/src/project/dto/project.dto.ts
@@ -21,9 +21,9 @@ export class ProjectDTO {
@ApiProperty({
example: 'localhost:3000,example.com',
required: false,
- description: 'Coma-separated allowed origins'
+ description: 'An array allowed origins'
})
- origins: string
+ origins: string[]
@ApiProperty({
required: false,
diff --git a/src/project/entity/project.entity.ts b/src/project/entity/project.entity.ts
index 9f4eb7b6e..e2ff45c3c 100644
--- a/src/project/entity/project.entity.ts
+++ b/src/project/entity/project.entity.ts
@@ -17,11 +17,8 @@ export class Project {
name: string
@ApiProperty()
- @Column('varchar', {
- length: 500,
- nullable: true,
- })
- origins: string
+ @Column('simple-array')
+ origins: string[]
@ApiProperty()
@Column({
@@ -35,4 +32,4 @@ export class Project {
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created: Date
-}
\ No newline at end of file
+}
diff --git a/src/project/project.controller.ts b/src/project/project.controller.ts
index e4c18ddcc..83aeb34a8 100644
--- a/src/project/project.controller.ts
+++ b/src/project/project.controller.ts
@@ -1,10 +1,11 @@
import {
Controller, Body, Query, Param, UseGuards, Get, Post, Put, Delete,
- BadRequestException, HttpCode, NotFoundException, UnprocessableEntityException,
+ BadRequestException, HttpCode, NotFoundException,
} from '@nestjs/common'
import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'
import * as _isEmpty from 'lodash/isEmpty'
-import * as _size from 'lodash/size'
+import * as _map from 'lodash/map'
+import * as _trim from 'lodash/trim'
import { ProjectService } from './project.service'
import { UserType } from '../user/entities/user.entity'
@@ -65,14 +66,14 @@ export class ProjectController {
@Roles(UserType.CUSTOMER, UserType.ADMIN)
async create(@Body() projectDTO: ProjectDTO, @CurrentUserId() userId: string): Promise<Project> {
this.logger.log({ projectDTO, userId }, 'POST /project')
- if (!isValidPID(projectDTO.id)) throw new UnprocessableEntityException('The provided Project ID (pid) is incorrect')
- if (_size(projectDTO.name) > 50) throw new UnprocessableEntityException('The project name is too long')
+ this.projectService.validateProject(projectDTO)
await this.projectService.checkIfIDUnique(projectDTO.id)
try {
const project = new Project()
Object.assign(project, projectDTO)
+ project.origins = _map(projectDTO.origins, _trim)
const newProject = await this.projectService.create(project)
const user = await this.userService.findOneWithRelations(userId, ['projects'])
@@ -99,8 +100,7 @@ export class ProjectController {
@ApiResponse({ status: 200, type: Project })
async update(@Param('id') id: string, @Body() projectDTO: ProjectDTO, @CurrentUserId() userId: string): Promise<any> {
this.logger.log({ projectDTO, userId, id }, 'PUT /project/:id')
- if (!isValidPID(id)) throw new BadRequestException('The provided Project ID (pid) is incorrect')
- if (_size(projectDTO.name) > 50) throw new UnprocessableEntityException('The project name is too long')
+ this.projectService.validateProject(projectDTO)
let project = await this.projectService.findOneWithRelations(id)
@@ -109,7 +109,7 @@ export class ProjectController {
}
project.active = projectDTO.active
- project.origins = projectDTO.origins
+ project.origins = _map(projectDTO.origins, _trim)
project.name = projectDTO.name
await this.projectService.allowedToManage(project, userId)
diff --git a/src/project/project.service.ts b/src/project/project.service.ts
index ac267d3fe..600f410da 100644
--- a/src/project/project.service.ts
+++ b/src/project/project.service.ts
@@ -1,12 +1,16 @@
-import { ForbiddenException, Injectable, BadRequestException } from '@nestjs/common'
+import { ForbiddenException, Injectable, BadRequestException, UnprocessableEntityException } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
+import * as _isEmpty from 'lodash/isEmpty'
+import * as _isArray from 'lodash/isArray'
+import * as _size from 'lodash/size'
+import * as _join from 'lodash/join'
import { Pagination, PaginationOptionsInterface } from '../common/pagination'
import { Project } from './entity/project.entity'
import { ProjectDTO } from './dto/project.dto'
-import { User, UserType } from '../user/entities/user.entity'
import { UserService } from '../user/user.service'
+import { isValidPID } from '../common/constants'
@Injectable()
export class ProjectService {
@@ -81,4 +85,11 @@ export class ProjectService {
throw new BadRequestException('Selected project ID is already in use')
}
}
+
+ validateProject(projectDTO: ProjectDTO) {
+ if (!isValidPID(projectDTO.id)) throw new UnprocessableEntityException('The provided Project ID (pid) is incorrect')
+ if (_size(projectDTO.name) > 50) throw new UnprocessableEntityException('The project name is too long')
+ if (!_isArray(projectDTO.origins)) throw new UnprocessableEntityException('The list of allowed origins has to be an array of strings')
+ if (_size(_join(projectDTO.origins, ',')) > 300) throw new UnprocessableEntityException('The list of allowed origins has to be smaller than 300 symbols')
+ }
}