aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

awswafv2: SingleHeaderProperty in GO generates bad CF template (capitalized "Name" property)

Open Recad opened this issue 3 years ago • 7 comments

Describe the bug

The use of SingleHeaderProperty in GO CDK for waf header validatrion is synthesizing a nonvalid template.

this is the code used:

        ....rest of rule config...
        SearchString: aws.String(props.HeaderValue),

	FieldToMatch: &awswafv2.CfnWebACL_FieldToMatchProperty{

			SingleHeader: &awswafv2.CfnWebACL_SingleHeaderProperty{

				Name: aws.String(props.HeaderName),

			},

	},
        ....rest of rule config...

This is the generated template portion "FieldToMatch": { "SingleHeader": { "name": "issued" } },

and this is the error in deployment. #/Rules/3/Statement/NotStatement/Statement/ByteMatchStatement/FieldToMatch/SingleHeader: required key [Name] not found #/Rules/3/Statement/NotStatement/Statement/ByteMatchStatement/FieldToMatch/SingleHeader: extraneous key [name] is not permitted

Expected Behavior

synthesize a template with capitalized property "FieldToMatch": { "SingleHeader": { "Name": "issued" }

Current Behavior

This is the generated template portion "FieldToMatch": { "SingleHeader": { "name": "issued" } },

Reproduction Steps

create a waf with a rule that validates the header using ByteMathStament:

   webaclbug := awswafv2.NewCfnWebACL(scope, aws.String("BUGACL"), &awswafv2.CfnWebACLProps{
        Name: aws.String("WebACLBUG"),
        DefaultAction: awswafv2.CfnWebACL_DefaultActionProperty{
            Allow: awswafv2.CfnWebACL_AllowActionProperty{},
        },
        Scope: aws.String("REGIONAL"),
        Rules: []*awswafv2.CfnWebACL_RuleProperty{
            {
                Name: aws.String("HeaderValidation"),
                Statement: &awswafv2.CfnWebACL_StatementProperty{
                    NotStatement: &awswafv2.CfnWebACL_NotStatementProperty{
                        Statement: &awswafv2.CfnWebACL_StatementProperty{
                            ByteMatchStatement: &awswafv2.CfnWebACL_ByteMatchStatementProperty{
                                SearchString: aws.String("value"),
                                FieldToMatch: &awswafv2.CfnWebACL_FieldToMatchProperty{
                                    SingleHeader: &awswafv2.CfnWebACL_SingleHeaderProperty{
                                        Name: aws.String("header"),
                                    },
                                },
                                TextTransformations: []*awswafv2.CfnWebACL_TextTransformationProperty{
                                    {
                                        Priority: aws.Float64(0),
                                        Type:     aws.String("NONE"),
                                    },
                                },
                                PositionalConstraint: aws.String("EXACTLY"),
                            },
                        },
                    },
                },
                Action:   aws.String("COUNT"),
                Priority: aws.Float64(1),
                VisibilityConfig: awswafv2.CfnWebACL_VisibilityConfigProperty{
                    SampledRequestsEnabled:   aws.Bool(true),
                    CloudWatchMetricsEnabled: aws.Bool(true),
                    MetricName:               aws.String("HeaderParamsCbRuleMetric"),
                },
            },
        },
        VisibilityConfig: awswafv2.CfnWebACL_VisibilityConfigProperty{
            SampledRequestsEnabled:   aws.Bool(true),
            CloudWatchMetricsEnabled: aws.Bool(true),
            MetricName:               aws.String("WebACLMetric"),
        },
    })

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.110.0

Framework Version

No response

Node.js Version

18.14.0

OS

windows 10

Language

Go

Language Version

No response

Other information

https://github.com/aws/aws-cdk/discussions/27537 https://github.com/aws/aws-cdk/issues/23709

Recad avatar Nov 20 '23 15:11 Recad

This is probably a jsii-related issue. We'll look into this and see if there's any workaround.

pahud avatar Nov 21 '23 20:11 pahud

this is not just on Go, also a problem in Python. The "name" attribute should have an upper-case "Name":

      RedactedFields:
        - SingleHeader:
            name: Authorization
      ResourceArn:
        Fn::GetAtt:
          - WebACL
          - Arn

workaround is to just send in a dict instead:

wafv2.CfnLoggingConfiguration.FieldToMatchProperty(
                    single_header={
                        'Name': 'Authorization'
                    }
                )

trondhindenes avatar Mar 03 '25 15:03 trondhindenes

I experienced this inconsistent alignment, regarding capitilisation, with SingleHeader but relatedly also SingleQueryArgument.

Using TypeScript, aws-cdk v2.1017.1.

nmc-oua avatar Jul 23 '25 23:07 nmc-oua

Hi @Recad,

Thanks for opening this issue! After reviewing the details, we found some other discussions that may be closely related:

Related Issues

  • #23679: This issue reports the identical problem where the SingleHeader property in WAFv2 constructs generates lowercase "name" in the CloudFormation template when uppercase "Name" is required. Both issues show the same error message: "required key [Name] not found" and "extraneous key [name] is not permitted". The underlying cause is the same JSII-related issue affecting property capitalization, just demonstrated in TypeScript instead of Go in the current issue.

  • #26127: This issue demonstrates the exact same capitalization problem with SingleHeaderProperty in WAFv2, but in Java instead of Go. Both issues show identical error messages where CloudFormation requires the uppercase "Name" property but CDK generates lowercase "name". The root cause is the same property capitalization issue in the WAFv2 module across different language bindings.

  • #15133: This issue involves the same type of property capitalization problem in WAFv2 where lowercase "name" is generated instead of the required uppercase "Name", producing identical error messages. While the current issue affects SingleHeaderProperty, this issue affects SingleQueryArgument, showing the capitalization issue extends to multiple properties in the WAFv2 module.

  • #23709: This issue reports the same type of capitalization problem in WAFv2 where properties are generated with incorrect casing in the CloudFormation template. The current issue focuses on SingleHeaderProperty generating "name" instead of "Name", while this issue focuses on similar capitalization problems with LoggingFilterProperty, demonstrating this is a broader issue affecting multiple WAFv2 properties.

  • #27404: This issue describes the same class of capitalization problems in WAFv2 where lowercase property names like "defaultBehavior" and "name" are generated in the CloudFormation template when uppercase versions are required. The current issue and this one share the identical root cause but affect different specific properties within the WAFv2 module.

  • #27993: This issue covers the same WAFv2 property capitalization problem, specifically mentioning the SingleHeader property issue alongside its primary focus on ExcludedRuleProperty. Both issues demonstrate how the CDK generates lowercase property names that CloudFormation rejects because it expects uppercase versions, showing this is a pattern across multiple WAFv2 properties.

This message was generated automatically to help connect related conversations and improve discoverability

Please react with 👍 or 👎 to let us know if this response was helpful!

Thank you for helping improve CDK! 🙌

maintainer-for-aws avatar Aug 11 '25 18:08 maintainer-for-aws

I'm seeing the same behavior for dotnet/C# in CDK version 2.1025.0.

CDK Code:

ByteMatchStatement = new CfnWebACL.ByteMatchStatementProperty
{
    FieldToMatch = new CfnWebACL.FieldToMatchProperty
    {
        SingleHeader = new CfnWebACL.SingleHeaderProperty
        {
            Name = "MyHeader"
        }
    },
    PositionalConstraint = "EXACTLY",
    SearchString = "MySearchString"
    TextTransformations = new []
    {
        new CfnWebACL.TextTransformationProperty
        {
            Priority = 0,
            Type = "NONE"
        }
    }
}

Here is the synthed result.

Statement:
  ByteMatchStatement:
    FieldToMatch:
      SingleHeader:
        name: MyHeader
    PositionalConstraint: EXACTLY
    SearchString:
      'MySearchString'
    TextTransformations:
      - Priority: 0
        Type: NONE

Note the lower case "name" key in the SingleHeader property.

boyersnet avatar Aug 14 '25 16:08 boyersnet

I'm seeing the same behavior for dotnet/C# in CDK version 2.1025.0.

CDK Code:

ByteMatchStatement = new CfnWebACL.ByteMatchStatementProperty { FieldToMatch = new CfnWebACL.FieldToMatchProperty { SingleHeader = new CfnWebACL.SingleHeaderProperty { Name = "MyHeader" } }, PositionalConstraint = "EXACTLY", SearchString = "MySearchString" TextTransformations = new [] { new CfnWebACL.TextTransformationProperty { Priority = 0, Type = "NONE" } } } Here is the synthed result.

Statement: ByteMatchStatement: FieldToMatch: SingleHeader: name: MyHeader PositionalConstraint: EXACTLY SearchString: 'MySearchString' TextTransformations: - Priority: 0 Type: NONE Note the lower case "name" key in the SingleHeader property.

Changing SingleHeaderProperty to a Dictionary as suggested by @trondhindenes resolves the issue.

 SingleHeader = new Dictionary<string, string>
 {
     {"Name", "MyHeader"}
 }

boyersnet avatar Aug 14 '25 17:08 boyersnet

The bug is still present in 2026 in 2.1126.0 (build a90d578) using C#

new CfnWebACL.SingleHeaderProperty { Name = "X-My-Header" }

Will generate a template with incorrect capitalisation

"SingleHeader": {
  "name": "X-My-Header"
}

This will trigger unnecessary validation errors for every deployment, though CloudFormation will deploy it still.

Properties validation failed for resource Waf with message: 
#/Rules/6/Statement/ByteMatchStatement/FieldToMatch/SingleHeader: required key [Name] not found 
#/Rules/6/Statement/ByteMatchStatement/FieldToMatch/SingleHeader: extraneous key [name] is not permitted 

whereisaaron avatar Jun 25 '26 09:06 whereisaaron