Skip to content

Commit

Permalink
Add warning for pattern traits missing ^ or $ anchors
Browse files Browse the repository at this point in the history
The pattern trait does not implicitly add a leading ^ or trailing $ to
match an entire string. If customer thinks the anchors are implicitly
added they may allow more values than expected. This WARNING will help
customers identify if they unintentionally forgot the anchors.
  • Loading branch information
gosar committed Mar 22, 2022
1 parent 4a1ec3a commit 4d4fa73
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ string DetailsValue
min: 1,
max: 64,
)
@pattern("[a-zA-Z0-9_.-]+")
@pattern("^[a-zA-Z0-9_.-]+$")
string EntityId

integer ErrorCode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.model.validation.validators;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.PatternTrait;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;

/**
* Emits a validation event if a pattern trait is not anchored.
*/
public final class PatternTraitValidator extends AbstractValidator {

@Override
public List<ValidationEvent> validate(Model model) {
List<ValidationEvent> events = new ArrayList<>();
for (Shape shape : model.getShapesWithTrait(PatternTrait.class)) {
validatePatternTrait(events, shape);
}

return events;
}

private void validatePatternTrait(List<ValidationEvent> events, Shape shape) {
PatternTrait trait = shape.expectTrait(PatternTrait.class);
String pattern = trait.getValue();
boolean leading = pattern.startsWith("^");
boolean trailing = pattern.endsWith("$");
if (!leading || !trailing) {
StringJoiner sj = new StringJoiner(" and ");
if (!leading) {
sj.add("leading '^'");
}
if (!trailing) {
sj.add("trailing '$'");
}
events.add(warning(shape, trait, String.format(
"A pattern trait is applied without a %s, meaning only part of the string must match the regular "
+ "expression. Explicitly anchoring regular expressions is preferable because it is more "
+ "restrictive by default and does not require modelers to understand that Smithy patterns are "
+ "not automatically anchored.", sj)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ software.amazon.smithy.model.validation.validators.MediaTypeValidator
software.amazon.smithy.model.validation.validators.NoInlineDocumentSupportValidator
software.amazon.smithy.model.validation.validators.OperationValidator
software.amazon.smithy.model.validation.validators.PaginatedTraitValidator
software.amazon.smithy.model.validation.validators.PatternTraitValidator
software.amazon.smithy.model.validation.validators.PrivateAccessValidator
software.amazon.smithy.model.validation.validators.RangeTraitValidator
software.amazon.smithy.model.validation.validators.ReferencesTraitValidator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static Collection<Object[]> data() {
{"ns.foo#String3", "\"foo\"", null},
{"ns.foo#String3", "\"bar\"", null},
{"ns.foo#String4", "\"ABC\"", null},
{"ns.foo#String4", "\"abc\"", new String[] {"String value provided for `ns.foo#String4` must match regular expression: [A-Z]+"}},
{"ns.foo#String4", "\"abc\"", new String[] {"String value provided for `ns.foo#String4` must match regular expression: ^[A-Z]+$"}},

// list
{"ns.foo#List", "[\"a\"]", null},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"Id": {
"target": "smithy.api#String",
"traits": {
"smithy.api#pattern": "foo",
"smithy.api#pattern": "^.*foo.*$",
"smithy.api#httpLabel": {},
"smithy.api#required": {}
}
Expand Down Expand Up @@ -225,13 +225,13 @@
"ns.foo#FooPatternString": {
"type": "string",
"traits": {
"smithy.api#pattern": "foo"
"smithy.api#pattern": "^.*foo.*$"
}
},
"ns.foo#BarPatternString": {
"type": "string",
"traits": {
"smithy.api#pattern": "bar"
"smithy.api#pattern": "^.*bar.*$"
}
},
"ns.foo#NestedAllowableConflicts": {
Expand Down Expand Up @@ -393,7 +393,7 @@
"target": "smithy.api#String",
"traits": {
"smithy.api#hostLabel": {},
"smithy.api#pattern": "foo",
"smithy.api#pattern": "^.*foo.*$",
"smithy.api#required": {}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[WARNING] ns.foo#Structure$WithoutAnchorsMember: A pattern trait is applied without a leading '^' and trailing '$', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait
[WARNING] ns.foo#Structure$WithoutLeadingAnchorMember: A pattern trait is applied without a leading '^', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait
[WARNING] ns.foo#Structure$WithoutTrailingAnchorMember: A pattern trait is applied without a trailing '$', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait
[WARNING] ns.foo#WithoutAnchors: A pattern trait is applied without a leading '^' and trailing '$', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait
[WARNING] ns.foo#WithoutLeadingAnchor: A pattern trait is applied without a leading '^', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait
[WARNING] ns.foo#WithoutTrailingAnchor: A pattern trait is applied without a trailing '$', meaning only part of the string must match the regular expression. Explicitly anchoring regular expressions is preferable | PatternTrait
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"smithy": "1.0",
"shapes": {
"ns.foo#Structure": {
"type": "structure",
"members": {
"AnchoredMember": {
"target": "smithy.api#String",
"traits": {
"smithy.api#pattern": "^[a-z]$"
}
},
"WithoutAnchorsMember": {
"target": "smithy.api#String",
"traits": {
"smithy.api#pattern": "[a-z]"
}
},
"WithoutLeadingAnchorMember": {
"target": "smithy.api#String",
"traits": {
"smithy.api#pattern": "[a-z]$"
}
},
"WithoutTrailingAnchorMember": {
"target": "smithy.api#String",
"traits": {
"smithy.api#pattern": "^[a-z]"
}
}
}
},
"ns.foo#Anchored": {
"type": "string",
"traits": {
"smithy.api#pattern": "^[a-z]$"
}
},
"ns.foo#WithoutAnchors": {
"type": "string",
"traits": {
"smithy.api#pattern": "[a-z]"
}
},
"ns.foo#WithoutLeadingAnchor": {
"type": "string",
"traits": {
"smithy.api#pattern": "[a-z]$"
}
},
"ns.foo#WithoutTrailingAnchor": {
"type": "string",
"traits": {
"smithy.api#pattern": "^[a-z]"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"ns.foo#String4": {
"type": "string",
"traits": {
"smithy.api#pattern": "[A-Z]+"
"smithy.api#pattern": "^[A-Z]+$"
}
},
"ns.foo#Blob1": {
Expand Down

0 comments on commit 4d4fa73

Please sign in to comment.