-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestrict_unicode.go
90 lines (80 loc) · 3.01 KB
/
restrict_unicode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2024 H0llyW00dz All rights reserved.
//
// License: BSD 3-Clause License
package validator
import (
"encoding/xml"
"fmt"
"reflect"
"github.com/gofiber/fiber/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// RestrictUnicode is a Restrictor implementation that restricts the use of Unicode characters
// in specified fields of the request body.
type RestrictUnicode struct {
// Fields specifies the fields to check for Unicode characters.
Fields []string
}
// Restrict implements the Restrictor interface for RestrictUnicode.
// It checks the specified fields in the request body for Unicode characters based on the content type.
func (r RestrictUnicode) Restrict(c *fiber.Ctx) error {
return restrictByContentType(c, r.restrictJSON, r.restrictXML, r.restrictOther)
}
// restrictJSON checks the specified fields in the JSON request body for Unicode characters.
func (r RestrictUnicode) restrictJSON(c *fiber.Ctx) error {
var body map[string]interface{}
if err := c.BodyParser(&body); err != nil {
return NewError(fiber.StatusBadRequest, ErrInvalidJSONBody)
}
for _, field := range r.Fields {
value, ok := body[field]
if ok {
if str, ok := value.(string); ok {
if containsUnicode(str) {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrUnicodeNotAllowedInField, field))
}
}
}
}
return nil
}
// restrictXML checks the specified fields in the XML request body for Unicode characters.
//
// Note: This function harnesses the power of magic reflection to conjure up a struct type from the given fields
// and breathe life into it with data from the XML request body. Magic reflection is a double-edged sword—
// powerful yet intricate—that allows for shapeshifting object types at runtime. It's no walk in the park, especially when wrestling with XML Hahaha.
func (r RestrictUnicode) restrictXML(c *fiber.Ctx) error {
fields := make([]reflect.StructField, len(r.Fields))
caser := cases.Title(language.English)
for i, field := range r.Fields {
fields[i] = reflect.StructField{
Name: caser.String(field),
Type: reflect.TypeOf(""),
Tag: reflect.StructTag(`xml:"` + field + `"`),
}
}
bodyType := reflect.StructOf(fields)
bodyValue := reflect.New(bodyType).Elem()
if err := xml.Unmarshal(c.Body(), bodyValue.Addr().Interface()); err != nil {
return NewError(fiber.StatusBadRequest, ErrInvalidXMLBody)
}
for _, field := range r.Fields {
value := bodyValue.FieldByName(caser.String(field)).String()
if containsUnicode(value) {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrUnicodeNotAllowedInField, field))
}
}
return nil
}
// restrictOther checks the specified fields in the request body of other content types for Unicode characters.
func (r RestrictUnicode) restrictOther(c *fiber.Ctx) error {
body := string(c.Body())
for _, field := range r.Fields {
fieldValue := extractFieldValue(body, field, r)
if containsUnicode(fieldValue) {
return NewError(fiber.StatusBadRequest, fmt.Sprintf(ErrUnicodeNotAllowedInField, field))
}
}
return nil
}