Fix #1011 - Server allows empty query (#1096)

Signed-off-by: Guy Margalit <guymguym@gmail.com>
This commit is contained in:
Guy Margalit 2022-01-20 14:26:34 +02:00 committed by GitHub
parent 7b23b30ffc
commit dbbacf0398
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 16 deletions

View File

@ -33,13 +33,13 @@ import software.amazon.smithy.rust.codegen.rustlang.withBlock
import software.amazon.smithy.rust.codegen.rustlang.writable
import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency
import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType
import software.amazon.smithy.rust.codegen.server.smithy.generators.http.ServerRequestBindingGenerator
import software.amazon.smithy.rust.codegen.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.smithy.generators.StructureGenerator
import software.amazon.smithy.rust.codegen.smithy.generators.builderSymbol
import software.amazon.smithy.rust.codegen.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.server.smithy.generators.http.ServerRequestBindingGenerator
import software.amazon.smithy.rust.codegen.smithy.generators.protocol.MakeOperationGenerator
import software.amazon.smithy.rust.codegen.smithy.generators.protocol.ProtocolGenerator
import software.amazon.smithy.rust.codegen.smithy.generators.protocol.ProtocolTraitImplGenerator
@ -581,7 +581,8 @@ private class ServerHttpProtocolImplGenerator(
writable { rust("""todo!("streaming request bodies");""") }
} else {
writable {
rustTemplate("""
rustTemplate(
"""
{
let body = request.take_body().ok_or(#{SmithyHttpServer}::rejection::BodyAlreadyExtracted)?;
let bytes = #{Hyper}::body::to_bytes(body).await?;
@ -746,7 +747,7 @@ private class ServerHttpProtocolImplGenerator(
with(writer) {
rustTemplate(
"""
let query_string = request.uri().query().ok_or(#{SmithyHttpServer}::rejection::MissingQueryString)?;
let query_string = request.uri().query().unwrap_or("");
let pairs = #{SerdeUrlEncoded}::from_str::<Vec<(#{Cow}<'_, str>, #{Cow}<'_, str>)>>(query_string)?;
""".trimIndent(),
*codegenScope
@ -774,11 +775,11 @@ private class ServerHttpProtocolImplGenerator(
val memberName = symbolProvider.toMemberName(it.member)
rustTemplate(
"""
if !seen_${memberName} && k == "${it.locationName}" {
if !seen_$memberName && k == "${it.locationName}" {
input = input.${it.member.setterName()}(
#{deserializer}(&v)?
);
seen_${memberName} = true;
seen_$memberName = true;
}
""".trimIndent(),
"deserializer" to deserializer
@ -855,10 +856,10 @@ private class ServerHttpProtocolImplGenerator(
rustTemplate(
"""
input = input.${it.member.setterName()}(
if ${memberName}.is_empty() {
if $memberName.is_empty() {
None
} else {
Some(${memberName})
Some($memberName)
}
);
""".trimIndent()
@ -1006,5 +1007,4 @@ private class ServerHttpProtocolImplGenerator(
}
}
}
}

View File

@ -71,13 +71,6 @@ define_rejection! {
pub struct MissingXmlContentType;
}
define_rejection! {
#[status = BAD_REQUEST]
#[body = "Expected query string in URI but none found"]
/// Rejection type used if the URI has no query string and we need to deserialize data from it.
pub struct MissingQueryString;
}
define_rejection! {
#[status = BAD_REQUEST]
#[body = "Failed to parse request MIME type"]
@ -140,7 +133,6 @@ composite_rejection! {
BodyAlreadyExtracted,
HeadersAlreadyExtracted,
ExtensionsAlreadyExtracted,
MissingQueryString,
}
}