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

Self-detecting Snyk Scan Type #46

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
5 changes: 1 addition & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ inputs:
min-severity:
description: 'minimum severity level that Jira tickets should be created for (acceptable values are "low", "medium", "high", and "critical")'
required: false
default: 'low'
# Scan
zap-risk-code:
description: 'riskcode type to report'
Expand All @@ -45,10 +46,6 @@ inputs:
description: 'can type to perform "snyk" or "zap"'
required: false
default: 'snyk'
snyk-test-type:
description: 'type of snyk test being performed; can be "open-source", "iac", "container", or "code"'
required: false
default: 'open-source'
major-version-only:
description: 'ticket in jira will be created only for the major version of vulnerability package; can be "true" or "false"'
required: false
Expand Down
60 changes: 44 additions & 16 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

60 changes: 44 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ try {
}
})();
} else if (scanType === 'snyk') {
let snykTestType = ''
const isMajorVersion = (v1, v2) => {
if (!v1 || !v2 || typeof v1 !== 'string' || typeof v2 !== 'string' ||
!/\d+\.\d+\.\d+/.test(v1) || !/\d+\.\d+\.\d+/.test(v2)) {
Expand All @@ -211,10 +212,10 @@ try {

// severity level enum
const Severities = {
low: 0,
medium: 1,
high: 2,
critical: 3
low: 1,
medium: 2,
high: 3,
critical: 4
};

let vulnerabilities = [];
Expand All @@ -223,11 +224,26 @@ try {
console.error("invalid input for min-severity; must be set to 'low', 'medium', 'high', or 'critical'");
process.exit(2);
}

if (inputData) {
if (core.getInput('snyk-test-type') === 'open-source') {
const data = JSON.parse(inputData);
if (Array.isArray(data)) {
if (data.some(d => d.infrastructureAsCodeIssues)) {
snykTestType = 'iac';
} else if (data.some(d => d.vulnerabilities)) {
snykTestType = 'open-source';
}
} else if (data && data.vulnerabilities) {
snykTestType = 'container';
}

if (!snykTestType) {
console.error('Error: Unable to determine Snyk scan type. Invalid or unknown data structure.');
process.exit(1);
}

if (snykTestType === 'open-source') {
try {
const data = JSON.parse(inputData);

if (Array.isArray(data)) {
for (const project of data) {
if (project && project.vulnerabilities && Array.isArray(project.vulnerabilities)) {
Expand All @@ -237,18 +253,21 @@ try {
vulnerabilities.push(v);
}
});
} else {
console.error(`Error: Invalid project structure in open-source data. Project data: ${JSON.stringify(project)}`);
}
}
} else {
console.error('Error: Open-source scan expected an array, but received invalid data.');
}
} catch (error) {
console.error('Error parsing Snyk output:', error);
process.exit(2);
// vulnerabilities = parseNonJsonData(inputData);
}
}
else if (core.getInput('snyk-test-type') === 'container') {
else if (snykTestType === 'container') {
try {
const data = JSON.parse(inputData);
if (data && data.vulnerabilities && Array.isArray(data.vulnerabilities)) {
data.vulnerabilities.forEach(v => {
if (minSeverity && Severities[v.severity] >= Severities[minSeverity]) {
Expand All @@ -265,19 +284,22 @@ try {
vulnerabilities.push(v);
}
})
} else {
console.error(`Error: Invalid application structure in container scan. Application data: ${JSON.stringify(app)}`);
}
});
}
} else {
console.error('Error: Container scan expected "vulnerabilities" to be an array, but received invalid data.');
}
} catch (error) {
console.error('Error parsing Snyk output:', error);
process.exit(2);
// vulnerabilities = parseNonJsonData(inputData);
}
}
else if (core.getInput('snyk-test-type') === 'iac') {
else if (snykTestType === 'iac') {
try {
const data = JSON.parse(inputData);
if (data && Array.isArray(data)) {
data.forEach(d => {
if (Array.isArray(d.infrastructureAsCodeIssues) && d.infrastructureAsCodeIssues.length > 0) {
Expand All @@ -288,23 +310,29 @@ try {
vulnerabilities.push(iacIssue);
}
});
} else {
console.error(`Error: Invalid IAC data structure. InfrastructureAsCodeIssues not found in data: ${JSON.stringify(d)}`);
}
});
} else {
console.error('Error: IAC scan expected an array, but received invalid data.');
}
} catch (error) {
console.error('Error parsing Snyk output:', error);
process.exit(2);
}
} else {
console.error(`Error: Unknown Snyk scan type "${snykTestType}".`);
}

if (vulnerabilities.length === 0) {
console.error('No Vulnerabilities Detetcted or Invalid JSON data format.');
console.error('No Vulnerabilities Detected');
}

return vulnerabilities;

} else {
console.error('No Vulnerabilities Detetcted or Invalid JSON data format.');
console.error('No input-data/vulnerabilities Detected');
// vulnerabilities = parseNonJsonData(inputData);
}
}
Expand All @@ -321,7 +349,7 @@ try {
return descriptionStr;
}

async function createSnykJiraTicket(vulnerability, comment='') {
async function createSnykJiraTicket(vulnerability, comment='', snykTestType = 'container') {
try {

const title = vulnerability.title.replaceAll("\"", "\\\"");
Expand All @@ -345,7 +373,7 @@ try {
"key": `${core.getInput('jira-project-key')}`
},
"summary": `${core.getInput('jira-title-prefix')} ${vulnerability.title}`,
"description": `${comment}${ core.getInput('snyk-test-type') === 'iac' ? iacDescriptionStr(vulnerability) : vulnerability.description}`,
"description": `${comment}${ snykTestType === 'iac' ? iacDescriptionStr(vulnerability) : vulnerability.description}`,
"issuetype": {
"name": `${core.getInput('jira-issue-type')}`
},
Expand Down Expand Up @@ -413,7 +441,7 @@ try {
console.log(
`Creating Jira ticket for vulnerability: ${vulnerability.title}`
);
const resp = await createSnykJiraTicket(vulnerability, comment);
const resp = await createSnykJiraTicket(vulnerability, comment, snykTestType);
console.log(resp)
} else {
console.log('skipping because not major update')
Expand Down
Loading