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

Add Minimum Permissions Map #18517

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions actions/ql/lib/codeql/actions/config/Config.qll
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ predicate vulnerableActionsDataModel(
*/
predicate immutableActionsDataModel(string action) { Extensions::immutableActionsDataModel(action) }

/**
* MaD models for minimum permissions for actions
* Fields:
* - action: action name
* - minimum_permissions: list of minimum permissions
*/
predicate minimumPermissionsDataModel(
string action, string minimum_permissions
) {
Extensions::minimumPermissionsDataModel(action, minimum_permissions)
}

/**
* MaD models for untrusted git commands
* Fields:
Expand Down
5 changes: 5 additions & 0 deletions actions/ql/lib/codeql/actions/config/ConfigExtensions.qll
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ extensible predicate vulnerableActionsDataModel(
*/
extensible predicate immutableActionsDataModel(string action);

/**
* Holds for actions that have a minimum permissions definition.
*/
extensible predicate minimumPermissionsDataModel(string action, string minimum_permissions);

/**
* Holds for git commands that may introduce untrusted data when called on an attacker controlled branch.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import actions
class MinimumActionsPermissions extends UsesStep {
string action;
string minimum_permissions;

MinimumActionsPermissions() {
minimumPermissionsDataModel(action, minimum_permissions) and
this.getCallee() = action
}

string getMinimumPermissions() { result = minimum_permissions }

string getAction() { result = action }
}
25 changes: 25 additions & 0 deletions actions/ql/lib/ext/config/minimum_permissions_map.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
:
- addsTo:
pack: github/actions-all
extensible: minimumPermissionsDataModel
data:
- ["actions/cache", "{}"]
- ["actions/setup-node", "contents:read"]
- ["actions/upload-artifact", "{}"]
- ["actions/setup-python", "contents:read"]
- ["actions/download-artifact", "{}"]
- ["actions/github-script", "It depends on what the script does"]
- ["actions/setup-java", "contents:read"]
- ["actions/setup-go", "contents:read"]
- ["actions/setup-dotnet", "contents:read"]
- ["actions/labeler", "contents:read, pull-requests:write"]
- ["actions/attest", "id-token:write, attestations:write"]
- ["actions/add-to-project", "repository-projects:read, repository-projects:write, issues:read, pull-requests:read"]
- ["actions/dependency-review-action", "contents:read"]
- ["actions/attest-sbom", "id-token:write, attestations:write"]
- ["actions/stale", "contents:write, issues:write, pull-requests:write"]
- ["actions/attest-build-provenance", "id-token:write, attestations:write"]
- ["actions/jekyll-build-pages", "contents:read, pages:write, id-token:write"]
- ["actions/publish-action", "contents:write"]
- ["actions/version-package-tools", "contents:read, actions:read"]
- ["actions/reusable-workflows", "contents:read, actions:read"]
45 changes: 34 additions & 11 deletions actions/ql/src/Security/CWE-275/MissingActionsPermissions.ql
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,38 @@
* external/cwe/cwe-275
*/

import actions
import actions
import codeql.actions.security.MinimumActionsPermissions

from Job job
where
not exists(job.getPermissions()) and
not exists(job.getEnclosingWorkflow().getPermissions()) and
// exists a trigger event that is not a workflow_call
exists(Event e |
e = job.getATriggerEvent() and
not e.getName() = "workflow_call"
)
select job, "Actions Job or Workflow does not set permissions"
// Returns the minimum permissions for all of the uses steps
// that are children of the job separated by a comma
// e.g. "contents: read, packages: write". If we cannot determine
// the permission we fallback to "unknown"
string getMinPermissions(Job job) {
if unknownPermissions(job) = true then result = "unknown" else
result = minPermissions(job)
}

string minPermissions(Job job) {
result = concat(job.getAChildNode*().(MinimumActionsPermissions).getMinimumPermissions(), ", ")
}

// Holds if we cannot determine the permissions for the uses step
// using the data extension or there are no uses steps
// that are children of the job
boolean unknownPermissions(Job job) {
minPermissions(job) = "" and result = true or count(job.getAChildNode*().(MinimumActionsPermissions)) = 0 and result = true

Check warning

Code scanning / CodeQL

Counting zero elements Warning

Use not exists(..) instead of checking that there is zero elements in a set.
}

from Job job
where
not exists(job.getPermissions()) and
not exists(job.getEnclosingWorkflow().getPermissions()) and
// exists a trigger event that is not a workflow_call
exists(Event e |
e = job.getATriggerEvent() and
not e.getName() = "workflow_call"
)
select job,
"Actions Job or Workflow does not set permissions. Recommended minimum permissions are ($@)",

Check warning

Code scanning / CodeQL

Alert message style violation Warning

Alert message should end with a full stop.
job, getMinPermissions(job)

Check warning

Code scanning / CodeQL

Alert message style violation Warning

Don't repeat the alert location as a link.
Loading