Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
feat: suggest to encode disallowed URI Template characters
Browse files Browse the repository at this point in the history
  • Loading branch information
klokane authored Mar 16, 2020
1 parent 5229875 commit 32f5f87
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
20 changes: 18 additions & 2 deletions ext/snowcrash/src/UriTemplateParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace error_locator

struct state {
std::string message;
std::string encoded;
size_t position = 0;
size_t length = 0;
};
Expand Down Expand Up @@ -59,6 +60,15 @@ namespace error_locator
template <typename Rule>
struct report_action : pegtl::nothing<Rule> {};

std::string encodeString(const std::string& in) {
std::stringstream encoded;
encoded << std::hex << std::uppercase;
for (const unsigned char chr : in) {
encoded << "%" << int(chr);
}
return encoded.str();
}

template <>
struct report_action<invalid_char>
{
Expand All @@ -74,6 +84,7 @@ namespace error_locator
s.message = std::string{"character '"} + in.string() + std::string{"'"};
}

s.encoded = std::move(encodeString(in.string()));
}
};

Expand Down Expand Up @@ -119,8 +130,13 @@ namespace

std::stringstream ss;
ss << "URI template variable '" << s.content
<< "' contains invalid " << state.message
<<". Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters";
<< "' contains invalid " << state.message;

if (!state.encoded.empty()) {
ss << ", which should be encoded as '" << state.encoded << "'";
}

ss <<". Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters";

#if 0
/**
Expand Down
16 changes: 8 additions & 8 deletions ext/snowcrash/test/test-UriTemplateParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEST_CASE("Parse uri template for invalid format curly brackets (nested brackets
parser.parse(uri, sourceBlock, result);

REQUIRE(result.report.warnings.size() == 2);
CHECK(result.report.warnings[0].message == "URI template variable 'id{' contains invalid nested expression brace '{'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
CHECK(result.report.warnings[0].message == "URI template variable 'id{' contains invalid nested expression brace '{', which should be encoded as '%7B'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
CHECK(result.report.warnings[1].message == "URI template contains disallowed character '}'. Disallowed characters must be percent encoded.");
}

Expand Down Expand Up @@ -238,7 +238,7 @@ TEST_CASE(
parser.parse(uri, sourceBlock, result);

REQUIRE(result.report.warnings.size() == 1);
REQUIRE(result.report.warnings[0].message == "URI template variable ' vartwo' contains invalid character ' '. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
REQUIRE(result.report.warnings[0].message == "URI template variable ' vartwo' contains invalid character ' ', which should be encoded as '%20'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
}

TEST_CASE("Parse uri template for invalid variable name, contains hyphens",
Expand All @@ -253,8 +253,8 @@ TEST_CASE("Parse uri template for invalid variable name, contains hyphens",
parser.parse(uri, sourceBlock, result);

REQUIRE(result.report.warnings.size() == 2);
REQUIRE(result.report.warnings[0].message == "URI template variable 'var-one' contains invalid character '-'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
REQUIRE(result.report.warnings[1].message == "URI template variable 'var-two' contains invalid character '-'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
REQUIRE(result.report.warnings[0].message == "URI template variable 'var-one' contains invalid character '-', which should be encoded as '%2D'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
REQUIRE(result.report.warnings[1].message == "URI template variable 'var-two' contains invalid character '-', which should be encoded as '%2D'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
}

TEST_CASE("Parse uri template for invalid variable name, contains assignment",
Expand All @@ -269,7 +269,7 @@ TEST_CASE("Parse uri template for invalid variable name, contains assignment",
parser.parse(uri, sourceBlock, result);

REQUIRE(result.report.warnings.size() == 1);
REQUIRE(result.report.warnings[0].message == "URI template variable 'varone=vartwo' contains invalid character '='. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
REQUIRE(result.report.warnings[0].message == "URI template variable 'varone=vartwo' contains invalid character '=', which should be encoded as '%3D'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
}

TEST_CASE("Parse uri template for invalid variable name, invalid % encoded",
Expand Down Expand Up @@ -299,7 +299,7 @@ TEST_CASE("Parse uri template for invalid variable name, invalid % encoded with
parser.parse(uri, sourceBlock, result);

REQUIRE(result.report.warnings.size() == 1);
REQUIRE(result.report.warnings[0].message == "URI template variable 'varone%2' contains invalid character '%'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
REQUIRE(result.report.warnings[0].message == "URI template variable 'varone%2' contains invalid character '%', which should be encoded as '%25'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
}

TEST_CASE("Parse uri template for variable name containing dot", "[validvariablenamecontainingdot][issue][78]")
Expand Down Expand Up @@ -343,7 +343,7 @@ TEST_CASE("Parse uri template for consistent invalid character warning",
parser.parse(urione, sourceBlock, result);

REQUIRE(result.report.warnings.size() == 1);
REQUIRE(result.report.warnings[0].message == "URI template variable '$a' contains invalid character '$'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
REQUIRE(result.report.warnings[0].message == "URI template variable '$a' contains invalid character '$', which should be encoded as '%24'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");

}

Expand All @@ -358,5 +358,5 @@ TEST_CASE("Parse uri template for variable name containing multibyte utf-8 char"
parser.parse(uri, sourceBlock, result);

REQUIRE(result.report.warnings.size() == 1);
REQUIRE(result.report.warnings[0].message == "URI template variable 'var荒data' contains invalid character '荒'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
REQUIRE(result.report.warnings[0].message == "URI template variable 'var荒data' contains invalid character '荒', which should be encoded as '%E8%8D%92'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters");
}
2 changes: 1 addition & 1 deletion test/fixtures/api/issue-702.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@
]
}
},
"content": "URI template variable 'instance-id' contains invalid character '-'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters"
"content": "URI template variable 'instance-id' contains invalid character '-', which should be encoded as '%2D'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters"
},
{
"element": "annotation",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/mson/issue-713.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
]
}
},
"content": "URI template variable 'a&b' contains invalid character '&'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters"
"content": "URI template variable 'a&b' contains invalid character '&', which should be encoded as '%26'. Allowed characters for expressions are A-Z a-z 0-9 _ and percent encoded characters"
}
]
}

0 comments on commit 32f5f87

Please sign in to comment.