From c596e88f469b9467a295d4ebc6186a2c822f4ad0 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 5 Apr 2023 12:20:52 +0800 Subject: [PATCH] Set search query max size to 255 char --- packages/api/src/directives.ts | 38 +++++++++++++++++++++++ packages/api/src/generated/schema.graphql | 2 +- packages/api/src/schema.ts | 4 +-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/api/src/directives.ts b/packages/api/src/directives.ts index 292a546b8..15e212cb7 100644 --- a/packages/api/src/directives.ts +++ b/packages/api/src/directives.ts @@ -46,5 +46,43 @@ export const sanitizeDirectiveTransformer = (schema: GraphQLSchema) => { } return fieldConfig }, + [MapperKind.ARGUMENT]: (argConfig) => { + const sanitizeDirective = getDirective(schema, argConfig, 'sanitize')?.[0] + if (!sanitizeDirective) { + return argConfig + } + + const maxLength = sanitizeDirective.maxLength as number | undefined + const minLength = sanitizeDirective.minLength as number | undefined + const allowedTags = sanitizeDirective.allowedTags as string[] | undefined + const pattern = sanitizeDirective.pattern as string | undefined + + if ( + argConfig.type instanceof GraphQLNonNull && + argConfig.type.ofType instanceof GraphQLScalarType + ) { + argConfig.type = new GraphQLNonNull( + new SanitizedString( + argConfig.type.ofType, + allowedTags, + maxLength, + minLength, + pattern + ) + ) + } else if (argConfig.type instanceof GraphQLScalarType) { + argConfig.type = new SanitizedString( + argConfig.type, + allowedTags, + maxLength, + minLength, + pattern + ) + } else { + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Not a scalar type: ${argConfig.type}`) + } + return argConfig + }, }) } diff --git a/packages/api/src/generated/schema.graphql b/packages/api/src/generated/schema.graphql index 7490fb535..2149a5ebd 100644 --- a/packages/api/src/generated/schema.graphql +++ b/packages/api/src/generated/schema.graphql @@ -1,4 +1,4 @@ -directive @sanitize(allowedTags: [String], maxLength: Int, minLength: Int, pattern: String) on INPUT_FIELD_DEFINITION +directive @sanitize(allowedTags: [String], maxLength: Int, minLength: Int, pattern: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION type AddPopularReadError { errorCodes: [AddPopularReadErrorCode!]! diff --git a/packages/api/src/schema.ts b/packages/api/src/schema.ts index c409027c1..61f735b19 100755 --- a/packages/api/src/schema.ts +++ b/packages/api/src/schema.ts @@ -10,7 +10,7 @@ const schema = gql` maxLength: Int minLength: Int pattern: String - ) on INPUT_FIELD_DEFINITION + ) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION enum SortOrder { ASCENDING @@ -2542,7 +2542,7 @@ const schema = gql` search( after: String first: Int - query: String + query: String @sanitize(maxLength: 255) includeContent: Boolean format: String ): SearchResult!