From 7be4ac295ba9e96bb45e68e9b192272f9f446919 Mon Sep 17 00:00:00 2001
From: Joel von der Weid <joel.von-der-weid@hesge.ch>
Date: Wed, 6 Mar 2024 15:08:42 +0100
Subject: [PATCH] Correct boolean validator bug

---
 ExpressAPI/src/helpers/GlobalHelper.ts    |  9 ++++++++-
 ExpressAPI/src/routes/AssignmentRoutes.ts | 22 +++++++++++++---------
 ExpressAPI/src/routes/BaseRoutes.ts       |  3 ++-
 3 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/ExpressAPI/src/helpers/GlobalHelper.ts b/ExpressAPI/src/helpers/GlobalHelper.ts
index a4f6ef2..266108c 100644
--- a/ExpressAPI/src/helpers/GlobalHelper.ts
+++ b/ExpressAPI/src/helpers/GlobalHelper.ts
@@ -7,6 +7,7 @@ import { GitbeakerRequestError } from '@gitbeaker/requester-utils';
 import * as Gitlab               from '@gitbeaker/rest';
 import axios, { AxiosError } from 'axios';
 import SharedConfig     from '../shared/config/SharedConfig';
+import * as https from 'https';
 
 
 class GlobalHelper {
@@ -60,7 +61,13 @@ class GlobalHelper {
 
     async isSonarSupported(): Promise<boolean> {
         try {
-            const sonar = await axios.get(SharedConfig.sonar.url);
+            // Use custom instance to allow self-signed certificates
+            const instance = axios.create({
+                httpsAgent: new https.Agent({
+                    rejectUnauthorized: false
+                })
+            });
+            const sonar = await instance.get(SharedConfig.sonar.url);
             return SharedConfig.sonar.enabled && sonar.status == 200;
         } catch ( error ) {
             return false;
diff --git a/ExpressAPI/src/routes/AssignmentRoutes.ts b/ExpressAPI/src/routes/AssignmentRoutes.ts
index cc6231b..84de91f 100644
--- a/ExpressAPI/src/routes/AssignmentRoutes.ts
+++ b/ExpressAPI/src/routes/AssignmentRoutes.ts
@@ -27,23 +27,25 @@ import SharedConfig                from '../shared/config/SharedConfig.js';
 
 class AssignmentRoutes implements RoutesManager {
     private readonly assignmentValidator: ExpressValidator.Schema = {
-        name    : {
+        name     : {
             trim    : true,
             notEmpty: true
         },
-        members : {
+        members  : {
             trim           : true,
             notEmpty       : true,
             customSanitizer: DojoValidators.jsonSanitizer
         },
-        template: {
+        template : {
             trim           : true,
             custom         : DojoValidators.templateUrlValidator,
             customSanitizer: DojoValidators.templateUrlSanitizer
         },
-        useSonar: {
-            notEmpty: true
-        }
+        useSonar : {
+            trim     : true,
+            notEmpty : true,
+            isBoolean: true,
+        },
     };
 
     private readonly assignmentAddCorrigeValidator: ExpressValidator.Schema = {
@@ -131,12 +133,14 @@ class AssignmentRoutes implements RoutesManager {
 
     private async createAssignment(req: express.Request, res: express.Response) {
         const params: {
-            name: string, members: Array<Gitlab.UserSchema>, template: string, useSonar: boolean
+            name: string, members: Array<Gitlab.UserSchema>, template: string, useSonar: string
         } = req.body;
+        const useSonar = params.useSonar === 'true';
+
         params.members = [ await req.session.profile.gitlabProfile.value, ...params.members ];
         params.members = params.members.removeObjectDuplicates(gitlabUser => gitlabUser.id);
 
-        if (params.useSonar && !(await GlobalHelper.isSonarSupported())) {
+        if (useSonar && !(await GlobalHelper.isSonarSupported())) {
             return req.session.sendResponse(res, StatusCodes.UNPROCESSABLE_ENTITY, {}, `Sonar integration is not supported`, DojoStatusCode.ASSIGNMENT_CREATION_SONAR_ERROR);
         }
 
@@ -185,7 +189,7 @@ class AssignmentRoutes implements RoutesManager {
                                                                                                        gitlabCreationDate: new Date(),
                                                                                                        gitlabLastInfo    : repository as unknown as Prisma.JsonObject,
                                                                                                        gitlabLastInfoDate: new Date(),
-                                                                                                       useSonar          : params.useSonar,
+                                                                                                       useSonar          : useSonar,
                                                                                                        staff             : {
                                                                                                            connectOrCreate: [ ...params.members.map(gitlabUser => {
                                                                                                                return {
diff --git a/ExpressAPI/src/routes/BaseRoutes.ts b/ExpressAPI/src/routes/BaseRoutes.ts
index cf0746f..ee028a8 100644
--- a/ExpressAPI/src/routes/BaseRoutes.ts
+++ b/ExpressAPI/src/routes/BaseRoutes.ts
@@ -4,6 +4,7 @@ import { StatusCodes }             from 'http-status-codes';
 import RoutesManager               from '../express/RoutesManager.js';
 import Config                      from '../config/Config';
 import SharedConfig    from '../shared/config/SharedConfig';
+import GlobalHelper    from '../helpers/GlobalHelper';
 
 
 class BaseRoutes implements RoutesManager {
@@ -36,7 +37,7 @@ class BaseRoutes implements RoutesManager {
 
     private async sonar(req: express.Request, res: express.Response) {
         const data = {
-            sonarEnabled: SharedConfig.useSonar
+            sonarEnabled: await GlobalHelper.isSonarSupported()
         };
         return req.session.sendResponse(res, StatusCodes.OK, data);
     }
-- 
GitLab