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

Teach ruleset yaml parser to support yaml anchors/merge keys #1325

Closed
ioggstream opened this issue Sep 3, 2020 · 7 comments · Fixed by #1332
Closed

Teach ruleset yaml parser to support yaml anchors/merge keys #1325

ioggstream opened this issue Sep 3, 2020 · 7 comments · Fixed by #1332
Assignees
Labels
enhancement New feature or request

Comments

@ioggstream
Copy link

Describe the bug
The following yaml rule with anchor does not validate correctly

To Reproduce

  1. Go to online spectral parser
  2. paste the content of the following rules
rules:
  no-x-headers-request: &no-x-headers
    description: "All 'HTTP' headers SHOULD NOT include 'X-' headers (https://tools.ietf.org/html/rfc6648)."
    severity: warn
    given:
      - "$..parameters[?(@.in == 'header')].name"
    message: |-
      HTTP header '{{value}}' SHOULD NOT include 'X-' prefix in {{path}}
    recommended: true
    type: style
    then:
      function: pattern
      functionOptions:
        notMatch: "/^[xX]-/"
  no-x-headers-response:
    <<: *no-x-headers
    given:
      - $.[responses][*].headers.*~
    message: |-
      HTTP header '{{value}}' SHOULD NOT include 'X-' prefix in {{path}}

  1. See error
/rules/no-x-headers-response should have required property 'then'

Expected behavior
No errors

Environment (remove any that are not applicable):

  • Version: spectral 5.1.0
@ioggstream ioggstream added the t/bug Something isn't working label Sep 3, 2020
@nulltoken nulltoken added enhancement New feature or request and removed t/bug Something isn't working labels Sep 9, 2020
@nulltoken nulltoken changed the title Rules do not support yaml anchors Teach ruleset yaml parser to support yaml anchors Sep 9, 2020
@nulltoken nulltoken changed the title Teach ruleset yaml parser to support yaml anchors Teach ruleset yaml parser to support yaml anchors/merge keys Sep 9, 2020
@nulltoken
Copy link
Contributor

nulltoken commented Sep 9, 2020

@ioggstream Indeed, you're right. Document support merge keys... and teaching Spectral to also support merge keys/anchors in rulesets would be quite fair.

A quick look seems to indicate the culprit would be around this part of the code: https://github.com/stoplightio/spectral/blob/develop/src/rulesets/reader.ts#L18-L24

and maybe leveraging Yaml.parse() from https://github.com/stoplightio/spectral/blob/develop/src/parsers/yaml.ts would help us tackling this.

Would you feel like working on this, I'd be happy to support you.

@ioggstream
Copy link
Author

@nulltoken I'll have a look. In the meantime, a question:

@nulltoken
Copy link
Contributor

@ioggstream Good call. We may not need to special case it. AFAIR the document reader always use the yaml parser whatever the document kind.

@P0lip
Copy link
Contributor

P0lip commented Sep 11, 2020

as yaml is a superset of json, why do we need https://github.com/stoplightio/spectral/blob/develop/src/rulesets/reader.ts#L18-L21

It's much faster (and safer too).

Speaking of the issue, we need to pass mergeKeys here https://github.com/stoplightio/spectral/blob/develop/src/rulesets/reader.ts#L23

@P0lip P0lip self-assigned this Sep 11, 2020
@ioggstream
Copy link
Author

1- @P0lip thanks for the PR!

faster and safer
Faster: true (but on rule files I suspect the difference is minimal)
Safer: not sure as the attacker controls the filename extension.

Thanks again! This will DRY rule files where the same rule should be applied - with different error messages - to request parameters and response fields.

@P0lip
Copy link
Contributor

P0lip commented Sep 11, 2020

Faster: true (but on rule files I suspect the difference is minimal)

It depends, since we have these huge OAS2 and OAS3 JSON schemas referenced in our ruleset, so the impact might be a bit higher than it may seem. Moreover, the AsyncAPI ruleset references the AsyncAPI JSON schema which is not small either.
Either way, shouldn't be a big deal to use yaml everywhere.

Safer: not sure as the attacker controls the filename extension.

yeah, sure. Agreed.

Thanks again! This will DRY rule files where the same rule should be applied - with different error messages - to request parameters and response fields.

You are welcome. If there is any other spot that does not support merge keys, do let me know.
We should be supporting them almost everywhere now.

@P0lip
Copy link
Contributor

P0lip commented Sep 28, 2020

@ioggstream I just wanted to let you know 5.6.0 is out with the support for YAML merge keys inside of rulesets.

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

Successfully merging a pull request may close this issue.

3 participants