From 0de81c2b3f56f57121f6a9f911c599cef70eeeb2 Mon Sep 17 00:00:00 2001
From: Tomas <tomasysh@gmail.com>
Date: 星期二, 05 九月 2023 14:23:27 +0800
Subject: [PATCH] Update: 0901-P5 URL manipulation

---
 PAMapp/shared/services/httpClient.ts |   31 +++++++++++++++++++++++++++----
 1 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/PAMapp/shared/services/httpClient.ts b/PAMapp/shared/services/httpClient.ts
index 7441584..db17491 100644
--- a/PAMapp/shared/services/httpClient.ts
+++ b/PAMapp/shared/services/httpClient.ts
@@ -6,14 +6,32 @@
 
 const notRequireInterceptorErrorUrl = [
   '/otp/verify',
-  '/otp/sendOtp',
+  // '/otp/sendOtp',
   '/eService/authenticate',
   '/login/validate/get_img_code',
   '/login/validate/verify_img_code',
+  '/api/access_analysis/insert'
 ];
 
+const BASE_URL = process.env.BASE_URL!;
+
+function sanitizeBaseUrl(baseUrl: string): string {
+  const pattern = /^(https?:\/\/).+/i;
+  if (!pattern.test(baseUrl)) {
+    throw new Error('Invalid BASE_URL');
+  }
+  const cleanedBaseUrl = cleanUrl(baseUrl);
+  return cleanedBaseUrl;
+}
+
+function cleanUrl(url: string): string {
+  const cleanedUrl = url.replace(/[^a-zA-Z0-9:/._-]/g, '');
+  return cleanedUrl;
+}
+
+
 export const http = axios.create({
-  baseURL: process.env.BASE_URL,
+  baseURL: sanitizeBaseUrl(BASE_URL),
   withCredentials: true
 });
 
@@ -48,7 +66,8 @@
 
 function addHttpHeader(config: AxiosRequestConfig): void {
   config.headers = {
-    Authorization: 'Bearer ' + localStorage.getItem('id_token')
+    Authorization: 'Bearer ' + localStorage.getItem('id_token'),
+    'content-type': 'application/json'
   }
 }
 
@@ -67,7 +86,11 @@
 function showErrorMessageBox(error: any): void {
   setTimeout(() => {
     // NOTE: 甇斤 HOT FIX 憿批��憭望������暹��� dialog [Tomas, 2022/7/20 14:21]
-    if(error.config.url.include('api/eService/authenticate')) return;
+    if(error.config.url.includes('/eService/authenticate')) return;
+    if (error.config.url.includes('/otp/sendOtp')) {
+      messageBoxService.showErrorMessage('', error);
+      return
+    }
     if (!_.includes(notRequireInterceptorErrorUrl, error.config.url)) {
       switch (error.response.status) {
         case 401:

--
Gitblit v1.8.0