Skip to content

Commit 49944eb

Browse files
authored
feat: added sanitization for route expressions (#1731)
* feat: added sanitization for route expressions * fix: added some fixes and integration tests * tests: removed integration tests for now * ci: added router_flavour in ci * tests: corrected test-utils * ci: corrected kong setup script * tests: corrected test-utils * tests: skipping older tests when router_flavour=expressions * tests: corrected test-utils * fix: improved expression sanitisation * tests: added test-case for not operator
1 parent ee32996 commit 49944eb

File tree

9 files changed

+567
-1
lines changed

9 files changed

+567
-1
lines changed

.ci/setup_kong_ee.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ KlBs7O9y+fc4AIIn6JD+9tymB1TWEn1B+3Vv6jmtzbztuCQTbJ6rTT3CFcE6TdyJ
6262
readonly KONG_IMAGE=${KONG_IMAGE:-kong/kong-gateway}
6363
readonly GATEWAY_CONTAINER_NAME=kong
6464

65+
KONG_ROUTER_FLAVOR=${KONG_ROUTER_FLAVOR:-'traditional_compatible'}
66+
6567
initNetwork
6668
initDb
6769
initMigrations "${KONG_IMAGE}" \
@@ -78,6 +80,7 @@ docker run \
7880
-e "KONG_LICENSE_DATA=$KONG_LICENSE_DATA" \
7981
-e "MY_SECRET_CERT=$MY_SECRET_CERT" \
8082
-e "MY_SECRET_KEY=$MY_SECRET_KEY" \
83+
-e "KONG_ROUTER_FLAVOR=${KONG_ROUTER_FLAVOR}" \
8184
-p 8000:8000 \
8285
-p 8443:8443 \
8386
-p 8001:8001 \

.github/workflows/integration-enterprise.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ jobs:
2828
- 'kong/kong-gateway:3.10'
2929
- 'kong/kong-gateway:3.11'
3030
- 'kong/kong-gateway-dev:latest'
31+
router_flavor:
32+
- 'traditional_compatible'
33+
- 'expressions'
3134
env:
3235
KONG_ANONYMOUS_REPORTS: "off"
3336
KONG_IMAGE: ${{ matrix.kong_image }}
37+
KONG_ROUTER_FLAVOR: ${{ matrix.router_flavor }}
3438

3539
runs-on: ubuntu-latest
3640
steps:

sanitize/custom_entity_specific.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
FCertificate = "FCertificate"
2525
CACertificate = "CACertificate"
2626
Key = "Key"
27+
Route = "Route"
2728
)
2829

2930
func (s *Sanitizer) handleEntity(entityName string, fieldValue reflect.Value) error {
@@ -38,6 +39,8 @@ func (s *Sanitizer) handleEntity(entityName string, fieldValue reflect.Value) er
3839
return s.handleCACertificate(fieldValue)
3940
case Key:
4041
return s.handleKey(fieldValue)
42+
case Route:
43+
return s.handleRoute(fieldValue)
4144
default:
4245
return fmt.Errorf("no specific handler for entity: %s", entityName)
4346
}
@@ -119,6 +122,23 @@ func (s *Sanitizer) handleKey(fieldValue reflect.Value) error {
119122
return s.setFieldValue(fieldValue, *sanitisedKey, Key)
120123
}
121124

125+
func (s *Sanitizer) handleRoute(fieldValue reflect.Value) error {
126+
route := fieldValue.Interface().(kong.Route)
127+
128+
if route.Expression == nil {
129+
// If the route does not have an expression, we can proceed with normal sanitization
130+
return nil
131+
}
132+
133+
sanitizedRoute := route.DeepCopy()
134+
originalExpression := *route.Expression
135+
sanitizedExpression := s.sanitizeExpression(originalExpression)
136+
s.sanitizedMap[originalExpression] = sanitizedExpression
137+
sanitizedRoute.Expression = &sanitizedExpression
138+
139+
return s.setFieldValue(fieldValue, *sanitizedRoute, Route)
140+
}
141+
122142
// generateTestCertAndKey generates a test certificate and key pair.
123143
// It returns the certificate PEM, key PEM, and certificate digest.
124144
// If isCA is true, it generates a CA certificate; otherwise, it generates a regular certificate.

sanitize/exempts.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var entityLevelExemptedFields = map[string]map[string]struct{}{
1818
"Partial": {"Type": {}},
1919
"PartialLink": {"Path": {}},
2020
"Plugin": {"Name": {}},
21-
"Route": {"Methods": {}},
21+
"Route": {"Methods": {}, "Expression": {}},
2222

2323
// Special handling
2424
"CACertificate": {"Cert": {}, "CertDigest": {}},
@@ -53,6 +53,7 @@ var entitiesToHandleDifferently = map[string]struct{}{
5353
"CACertificate": {},
5454
"FCertificate": {},
5555
"Key": {},
56+
"Route": {},
5657
}
5758

5859
// dynamically generated maps of exempted fields from schemas

0 commit comments

Comments
 (0)