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

API Security 2023 #51

Merged
merged 4 commits into from
Feb 5, 2024
Merged

API Security 2023 #51

merged 4 commits into from
Feb 5, 2024

Conversation

philsturgeon
Copy link
Contributor

@philsturgeon philsturgeon commented Jan 21, 2024

Updating the Spectral OWASP ruleset to contain as much of the OWASP API Security Top 10 (2023) as possible with a tool that focuses on scanning the interface.

Motivation and Context

Times change, the world gets more complicated, and OWASP like to move things around. 😋

Description

Updating the Spectral ruleset to match the changes, add new rules where possible, and delete things which no longer seem as relevant.

https://owasp.org/API-Security/editions/2023/en/0x00-header/

How Has This Been Tested?

Testing has been done through unit tests for all new rules.

src/ruleset.ts Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@philsturgeon
Copy link
Contributor Author

@frankkilcommins fixed all feedback, and upgraded npm dependencies too whilst I'm here.

@mnaumanali94
Copy link
Collaborator

@philsturgeon We should probably get these rules as part of this done too:
#5
#3
#12

What do you think?

@mnaumanali94
Copy link
Collaborator

For API7:2023 Server Side Request Forgery I was thinking we could enforce that a parameter is named as something like URL i.e e.g., callbackUrl, redirectUri we should
1: flag it for review and/or have a URL format on it + validation pattern

@mnaumanali94
Copy link
Collaborator

mnaumanali94 commented Jan 25, 2024

For API9:2023 Improper Inventory Management
We could add rule(s) to add Good descriptions, version number, contact info e.t.c
I think some of our other rulesets cover that but maybe we can refer them here or recommend using the docs style guide with this 🤔

description:
"OWASP API Security recommends defining schemas for all responses, even errors. The 401 describes what happens when a request is unauthorized, so its important to define this not just for documentation, but to empower contract testing to make sure the proper JSON structure is being returned instead of leaking implementation details in backtraces.",
severity: DiagnosticSeverity.Warning,
given: "$.paths..responses",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can make these JSON paths a bit more specific i.e. target various places that have responses property rather than doing ..responses. This can lead to erroneous messages and be relatively slow.

Don't need to do this in this PR but probably good to create an issue for this 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be a little bit specific because there have been several issues popping up where JSON in examples, where the example JSON might be { responses: { ... } } so I've always tried to point it somewhat in the right direction. I would love somebody better versed with nimma performance to help me understand the right balance of specific and fast but I don't know it insde and out.

@philsturgeon
Copy link
Contributor Author

For API9:2023 Improper Inventory Management We could add rule(s) to add Good descriptions, version number, contact info e.t.c I think some of our other rulesets cover that but maybe we can refer them here or recommend using the docs style guide with this 🤔

I don't believe anything in OWASP API Security Top 10 has any opinion on this. IT definitely doens't care about contact info or description, and the OpenAPI version number might be a bit of a stretch because that doesn't necessarily mean it's the API version number, and even if it does the problem its describing is having too many API versions hosted seperately and this one API document doesn't have any way to know what other APIs are hosted.

I would be happier leaving those sort of "make good docs" rules to the Documentation ruleset, so we can let people mix and match things in a modular way, and it means people wont be annoyed by the ruleset telling them irrelevant stuff.

@philsturgeon
Copy link
Contributor Author

For API7:2023 Server Side Request Forgery I was thinking we could enforce that a parameter is named as something like URL i.e e.g., callbackUrl, redirectUri we should 1: flag it for review and/or have a URL format on it + validation pattern

Good reminder. I've made this #55 with a bit more context, as I really wanted to add this but have been a bit stuck with the direction, and if it's useful to even attempt.

@philsturgeon
Copy link
Contributor Author

@philsturgeon We should probably get these rules as part of this done too: #5 #3 #12

What do you think?

Happy to give #5 a quick go because you're absolutely right, that needs doing.

#12 do we think people are going to have actual back traces in their examples? Perhaps this would happen in some sort of automated OpenAPI learning scenario? I cannot imagine this happening any other way, so I do not know if this is helpful.

#3 This one would be lovely to do, and it's something I got working with LintGPT as it's using AI to figure out if any of the field names seem like PII, but I am pretty concerned about adding checks for every single schema to see if it has any fields from a massive list that might be PII.

If I have to check every response for id, email, password, address, credit card, gender, etc then that'll be slow. Secondly, is that leaking PII, or is that PII right where its meant to be? If I have a user directory and its VPNed off from the rest of the world, that is not leaking PII it's just a system that has PII.

What am I actually looking for here, and how can I implement that in a way which isn't horrendously slow.

@frankkilcommins
Copy link

#12 do we think people are going to have actual back traces in their examples? Perhaps this would happen in some sort of automated OpenAPI learning scenario? I cannot imagine this happening any other way, so I do not know if this is helpful.

I think it would indeed be strange to intentionally put such traces etc. into an example. What's the thinking around trying to give some use feedback on an API design description?

Perhaps:

  • warn if no error response schemas (already covered)
  • warn if error response schema is very wide ( {} )
  • warn if not following RFC 9457 (formally RFC 7807)
  • warn if leaky properties are defined in error response schemas. For example, properties like log/trace/diagnostics/logDetails/stacktrace/exception (will someone really design like this tho...)

@philsturgeon
Copy link
Contributor Author

philsturgeon commented Jan 31, 2024

@mnaumanali94 I've added #5 and #55.

I do not believe #3 can be done in a way that is both useful and vaguely performant.

As @frankkilcommins suggests I don't think #12 can be done in a way which is meaningful and sticks to the instructions in the OWASP edition.

Yes, we could say "you have to use error format X or Y" and then hopefully they have contract testing, but thats no different to owasp:api8:2023-define-error-responses-401 or owasp:api8:2023-define-error-responses-500 which already ask you to define the content and that will already pick up on your accidentally leaking weird errors if they dont match the schema defined.

This is more likely to be a check for Prism, where it's noticing a mismatch between OpenAPI and the response from the API implementation, but seeing as Spectral can only look at the design (and people arent going to design unexpected broken backtrace errors into their specs) then I just really don't think there's anything to do here.

Copy link

@frankkilcommins frankkilcommins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes now look good to me. @mnaumanali94 will need to do the formal approval

@mnaumanali94 mnaumanali94 self-requested a review February 2, 2024 14:43
Copy link
Collaborator

@mnaumanali94 mnaumanali94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work Phil!

@mnaumanali94
Copy link
Collaborator

@philsturgeon When you get a chance can you please resolve the conflicts and then I think we can go ahead and merge.

@philsturgeon philsturgeon merged commit f7f7e63 into main Feb 5, 2024
1 check passed
@philsturgeon philsturgeon deleted the feat/2023 branch February 5, 2024 14:36
Copy link

github-actions bot commented Mar 5, 2024

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment