Skip to content

Commit 601adde

Browse files
mtdowlingMichael Dowling
authored andcommitted
Validate that http uri uses ASCII characters
Closes #975
1 parent 320c1e0 commit 601adde

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.smithy.model.validation.validators;
17+
18+
import java.nio.charset.CharsetEncoder;
19+
import java.nio.charset.StandardCharsets;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import software.amazon.smithy.model.Model;
23+
import software.amazon.smithy.model.shapes.Shape;
24+
import software.amazon.smithy.model.traits.HttpTrait;
25+
import software.amazon.smithy.model.validation.AbstractValidator;
26+
import software.amazon.smithy.model.validation.ValidationEvent;
27+
28+
public final class HttpUriFormatValidator extends AbstractValidator {
29+
@Override
30+
public List<ValidationEvent> validate(Model model) {
31+
List<ValidationEvent> events = new ArrayList<>();
32+
CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
33+
34+
for (Shape shape : model.getShapesWithTrait(HttpTrait.class)) {
35+
HttpTrait trait = shape.expectTrait(HttpTrait.class);
36+
String uri = trait.getUri().toString();
37+
if (!encoder.canEncode(uri)) {
38+
events.add(error(shape, trait, "@http trait `uri` is invalid: " + uri));
39+
}
40+
}
41+
return events;
42+
}
43+
}

smithy-model/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ software.amazon.smithy.model.validation.validators.HttpQueryParamsTraitValidator
1616
software.amazon.smithy.model.validation.validators.HttpQueryTraitValidator
1717
software.amazon.smithy.model.validation.validators.HttpResponseCodeSemanticsValidator
1818
software.amazon.smithy.model.validation.validators.HttpUriConflictValidator
19+
software.amazon.smithy.model.validation.validators.HttpUriFormatValidator
1920
software.amazon.smithy.model.validation.validators.JsonNameValidator
2021
software.amazon.smithy.model.validation.validators.LengthTraitValidator
2122
software.amazon.smithy.model.validation.validators.MediaTypeValidator
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] ns.foo#SayHello: @http trait `uri` is invalid: /service/小路/💙/طريق | HttpUriFormat
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"smithy": "1.0",
3+
"shapes": {
4+
"ns.foo#MyService": {
5+
"type": "service",
6+
"version": "2017-01-17",
7+
"operations": [
8+
{
9+
"target": "ns.foo#SayHello"
10+
}
11+
]
12+
},
13+
"ns.foo#SayHello": {
14+
"type": "operation",
15+
"traits": {
16+
"smithy.api#http": {
17+
"method": "POST",
18+
"uri": "/service/小路/\uD83D\uDC99/طريق"
19+
}
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)