Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate Inline struct attributes #830

Open
johakoch opened this issue Jun 22, 2024 · 0 comments
Open

Validate Inline struct attributes #830

johakoch opened this issue Jun 22, 2024 · 0 comments

Comments

@johakoch
Copy link
Collaborator

"Regular" config struct attributes are validated at config load. Several Inline attributes, however, are not validated at all.

E.g. proxy:

func (p Proxy) Inline() interface{} {
	type Inline struct {
		meta.RequestHeadersAttributes
		meta.ResponseHeadersAttributes
		meta.FormParamsAttributes
		meta.QueryParamsAttributes
		Backend        *Backend    `hcl:"backend,block" ...`
		ExpectedStatus []int       `hcl:"expected_status,optional" ...`
		URL            string      `hcl:"url,optional" ...`
		Websockets     *Websockets `hcl:"websockets,block" ...`
	}

	return &Inline{}
}

proxy {
  url = 42
}

URL is validated at runtime by producer.NewURLFromAttribute(). OK


proxy {
  expected_status = 200
}

ExpectedStatus with an invalid type (e.g. int instead of []int) is quietly ignored:
handler/proxy.go

	expStatusVal, err := eval.ValueFromBodyAttribute(hclCtx, p.context, "expected_status")
	if err != nil {
		return nil, err
	}

	outCtx = context.WithValue(outCtx, request.EndpointExpectedStatus, seetie.ValueToIntSlice(expStatusVal))

with

func ValueToIntSlice(src cty.Value) []int64 {
	var n []int64
	if !src.IsKnown() || src.IsNull() || !src.CanIterateElements() {
		return n
	}

returning an empty slice, then ignored in handler/producer/result.go because len() of the empty slice is not > 0

	if expStatus, ok := req.Context().Value(request.EndpointExpectedStatus).([]int64); beresp != nil &&
		ok && len(expStatus) > 0 {
...

proxy {
  set_request_headers = 42
}

even panics.


As it seems, the Inline is only used to create another BodySchema to find unsupported attributes/blocks in (config/configload/schema.go)

func completeSchemaComponents(body hcl.Body, schema *hcl.BodySchema,
	blocks hcl.Blocks, errors hcl.Diagnostics) (hcl.Blocks, hcl.Diagnostics) {

	content, diags := body.Content(schema)

How can we use the type information from the Inline structs returned by .Inline() to validate attribute values?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant