Sfoglia il codice sorgente

Add grob pattern path validation

Shun Miyazawa 1 anno fa
parent
commit
ade8f9500f

+ 11 - 1
apps/app/src/features/openai/server/routes/ai-assistant.ts

@@ -1,5 +1,6 @@
 import { type IUserHasId, GroupType } from '@growi/core';
 import { type IUserHasId, GroupType } from '@growi/core';
 import { ErrorV3 } from '@growi/core/dist/models';
 import { ErrorV3 } from '@growi/core/dist/models';
+import { isGrobPatternPath, isCreatablePage } from '@growi/core/dist/utils/page-path-utils';
 import type { Request, RequestHandler } from 'express';
 import type { Request, RequestHandler } from 'express';
 import { type ValidationChain, body } from 'express-validator';
 import { type ValidationChain, body } from 'express-validator';
 
 
@@ -60,7 +61,16 @@ export const createAiAssistantFactory: CreateAssistantFactory = (crowi) => {
       .isString()
       .isString()
       .withMessage('pagePathPatterns must be an array of strings')
       .withMessage('pagePathPatterns must be an array of strings')
       .notEmpty()
       .notEmpty()
-      .withMessage('pagePathPatterns must not be empty'),
+      .withMessage('pagePathPatterns must not be empty')
+      .custom((value: string) => {
+
+        // check if the value is a grob pattern path
+        if (value.includes('*')) {
+          return isGrobPatternPath(value) && isCreatablePage(value.replace('*', ''));
+        }
+
+        return isCreatablePage(value);
+      }),
 
 
     body('grantedGroups')
     body('grantedGroups')
       .optional()
       .optional()

+ 6 - 0
packages/core/src/utils/page-path-utils/index.ts

@@ -305,5 +305,11 @@ export const getUsernameByPath = (path: string): string | null => {
   return username;
   return username;
 };
 };
 
 
+export const isGrobPatternPath = (path: string): boolean => {
+  // https://regex101.com/r/IBy7HS/1
+  const globPattern = /^(?:\/[^/*?[\]{}]+)*\/\*$/;
+  return globPattern.test(path);
+};
+
 
 
 export * from './is-top-page';
 export * from './is-top-page';