Skip to content

C#: Add query for missing function level access control #13094

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

Merged

Conversation

joefarebrother
Copy link
Contributor

@joefarebrother joefarebrother commented May 9, 2023

Adds query for missing authorization checks.
Finds WebForms methods (System.Web.UI.Page) and MVC actions whose names indicate that they should have authorization checs (e.g. Delete or Admin), but such checks could not be found; including through code, attributes, or Web.config xml files.

@github-actions github-actions bot added the C# label May 9, 2023
@joefarebrother joefarebrother force-pushed the csharp-missing-access-control branch from 5aed1c9 to 12bb418 Compare June 14, 2023 15:12
@joefarebrother joefarebrother changed the title [Draft] [C#] Add query for missing function level access control [C#] Add query for missing function level access control Jun 14, 2023
@joefarebrother joefarebrother marked this pull request as ready for review June 14, 2023 15:17
@joefarebrother joefarebrother requested a review from a team as a code owner June 14, 2023 15:17
@joefarebrother
Copy link
Contributor Author

Documentation is still in progress, but the technical work is ready for reveiw.

@michaelnebel michaelnebel self-requested a review June 19, 2023 08:09
Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

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

This already looks really good!
I have added some minor comments/recommendations and suggestions.
There primary concern is probably about the use of .calls*(..).
Have you tried running the query on some large projects?

predicate hasAuthViaCode(ActionMethod m) {
m.needsAuth() and
exists(Callable caller, AuthExpr auth |
m.getAnAuthorizingCallable().calls*(caller) and
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this cause performance issues (the reflexive and transitive closure of this could be huge)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The DCA run looks ok; besides a couple failures that look unrelated to this query

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright - that sounds good!

Copy link
Contributor

Choose a reason for hiding this comment

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

@hvitved: Do you have any concerns about using the reflexive, transitive closure of calls?

Copy link
Contributor

Choose a reason for hiding this comment

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

If DCA is happy, then I'm happy :-)

@michaelnebel
Copy link
Contributor

This already looks really good! I have added some minor comments/recommendations and suggestions. There primary concern is probably about the use of .calls*(..). Have you tried running the query on some large projects?

It is probably a good idea to run this query against the DCA nightly suite.

@sidshank sidshank changed the title [C#] Add query for missing function level access control C#: Add query for missing function level access control Jun 19, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 20, 2023

QHelp previews:

csharp/ql/src/Security Features/CWE-285/MissingAccessControl.qhelp

Missing function level access control

Sensitive actions, such as editing or deleting content, or accessing admin pages, should have authorization checks to ensure that they cannot be used by malicious actors.

Recommendation

Ensure that proper authorization checks are made for sensitive actions. For WebForms applications, the authorization tag in Web.config XML files can be used to implement access control. The System.Web.UI.Page.User property can also be used to verify a user's role. For MVC applications, the Authorize attribute can be used to require authorization on specific action methods.

Example

In the following WebForms example, the case marked BAD has no authorization checks whereas the case marked GOOD uses User.IsInRole to check for the user's role.

class ProfilePage : System.Web.UI.Page {
    // BAD: No authorization is used
    protected void btn1_Edit_Click(object sender, EventArgs e) {
        ...
    }

    // GOOD: `User.IsInRole` checks the current user's role.
    protected void btn2_Delete_Click(object sender, EventArgs e) {
        if (!User.IsInRole("admin")) {
            return;
        }
        ...
    }
} 

The following Web.config file uses the authorization tag to deny access to anonymous users, in a location tag to have that configuration apply to a specific path.

<?xml version="1.0"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location path="User/Profile">
    <system.web>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>
</configuration>

In the following MVC example, the case marked BAD has no authorization checks whereas the case marked GOOD uses the Authorize attribute.

public class ProfileController : Controller {

    // BAD: No authorization is used.
    public ActionResult Edit(int id) {
        ...
    }

    // GOOD: The `Authorize` attribute is used.
    [Authorize]
    public ActionResult Delete(int id) {
        ...
    }
}

References

@joefarebrother joefarebrother added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label Jun 29, 2023
michaelnebel
michaelnebel previously approved these changes Jul 4, 2023
Copy link
Contributor

@michaelnebel michaelnebel 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 Joe!!! :-)
Looks good to me!

@joefarebrother
Copy link
Contributor Author

Great; just need the docs review then

Copy link
Contributor

@tamasvajk tamasvajk left a comment

Choose a reason for hiding this comment

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

Looks good, I found two small issues.

@mchammer01
Copy link
Contributor

I'll review this for Docs!

Copy link
Contributor

@mchammer01 mchammer01 left a comment

Choose a reason for hiding this comment

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

@joefarebrother 👋🏻 - I wasn't able to see a preview of the ql file, with the examples in-situ. Have things changed, do you know how to get to the preview?
I reviewed this from an editorial point of view, and left a few comments and suggestions for your consideration. Feel free to ignore the ones you don't agree with 😃
(The punctuation is needed in the list in the References section. )

@joefarebrother
Copy link
Contributor Author

Thanks @mchammer01 - I've applied those suggestions.
The preview is available in this comment: #13094 (comment)
Possibly it wasn't working previously due to the > typo.

@mchammer01
Copy link
Contributor

@joefarebrother - thanks for pointing me to the preview ✨
I hadn't seen it 🙈
LGTM, just a tiny nit. The // BAD: No authorization is used comment in your first code example is missing a full stop. Feel free to ignore if you think it's OTT 😅

Copy link
Contributor

@mchammer01 mchammer01 left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

@joefarebrother joefarebrother merged commit c10a668 into github:main Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C# documentation ready-for-doc-review This PR requires and is ready for review from the GitHub docs team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants