Skip to content

Commit

Permalink
Merge pull request #6705 from Checkmarx/kics-1035-no-relative-paths
Browse files Browse the repository at this point in the history
feat(query): ansible playbooks Insecure Relative Path Resolution
  • Loading branch information
asofsilva authored Sep 20, 2023
2 parents e1c8c8f + 501c650 commit 5e34f96
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "8d22ae91-6ac1-459f-95be-d37bd373f244",
"queryName": "Insecure Relative Path Resolution",
"severity": "LOW",
"category": "Best Practices",
"descriptionText": "Using relative paths can lead to unexpected behavior as the path is resolved relative to the current working directory, which can change.",
"descriptionUrl": "https://ansible.readthedocs.io/projects/lint/rules/no-relative-paths/",
"platform": "Ansible",
"descriptionID": "84ea91c8",
"cloudProvider": "common"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Cx

import data.generic.ansible as ansLib

module_to_folder = {
"copy": "files",
"win_copy": "files",
"template": "templates",
"win_template": "win_templates",
"ansible.builtin.template": "templates",
"ansible.builtin.copy": "files",
}

CxPolicy[result] {
task := ansLib.tasks[id][t]
folder := module_to_folder[m]
copyOrTemplate := task[m]
ansLib.checkState(copyOrTemplate)

relative_path := sprintf("../%s", [folder])
contains(copyOrTemplate.src, relative_path)

result := {
"documentId": id,
"resourceType": m,
"resourceName": task.name,
"searchKey": sprintf("name={{%s}}.{{%s}}.src", [task.name, m]),
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("%s.src should not be a relative path", [m]),
"keyActualValue": sprintf("%s.src is a relative path", [m]),
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
- name: Negative Example
hosts: localhost
tasks:
- name: One
ansible.builtin.copy:
content:
dest: /etc/mine.conf
mode: "0644"
- name: Two
ansible.builtin.copy:
src: /home/example/files/foo.conf
dest: /etc/foo.conf
mode: "0644"

---
- name: Negative Example 2
hosts: localhost
tasks:
- name: One
ansible.builtin.template:
src: ../example/foo.j2
dest: /etc/file.conf
mode: "0644"
- name: Two
ansible.builtin.copy:
src: ../example/foo.conf
dest: /etc/foo.conf
mode: "0644"
- name: Three
win_template:
src: ../example/foo2.j2
dest: /etc/file.conf
mode: "0644"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: Positive Example
hosts: localhost
tasks:
- name: One
ansible.builtin.template:
src: ../templates/foo.j2
dest: /etc/file.conf
mode: "0644"
- name: Two
ansible.builtin.copy:
src: ../files/foo.conf
dest: /etc/foo.conf
mode: "0644"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"queryName": "Insecure Relative Path Resolution",
"severity": "LOW",
"line": 7,
"fileName": "positive1.yaml"
},
{
"queryName": "Insecure Relative Path Resolution",
"severity": "LOW",
"line": 12,
"fileName": "positive1.yaml"
}
]

0 comments on commit 5e34f96

Please sign in to comment.