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

Fix incorrect vulnerable manifest path check #186

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/index.js

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

44 changes: 44 additions & 0 deletions src/dependabot/verified_commits.test.ts
Expand Up @@ -134,39 +134,83 @@ const response = {
}
}

const responseWithManifestFileAtRoot = {
data: {
repository: {
vulnerabilityAlerts: {
nodes: [
{
vulnerableManifestFilename: 'package.json',
vulnerableManifestPath: 'package.json',
vulnerableRequirements: '= 4.0.1',
state: 'DISMISSED',
securityVulnerability: { package: { name: 'coffee-script' } },
securityAdvisory: { cvss: { score: 4.5 }, ghsaId: 'FOO' }
}
]
}
}
}
}

test('it returns the alert state if it matches all 3', async () => {
nock('https://api.github.com').post('/graphql', query)
.reply(200, response)

expect(await getAlert('coffee-script', '4.0.1', '/wwwroot', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: 'DISMISSED', cvss: 4.5, ghsaId: 'FOO' })

nock('https://api.github.com').post('/graphql', query)
.reply(200, responseWithManifestFileAtRoot)

expect(await getAlert('coffee-script', '4.0.1', '/', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: 'DISMISSED', cvss: 4.5, ghsaId: 'FOO' })
})

test('it returns the alert state if it matches 2 and the version is blank', async () => {
nock('https://api.github.com').post('/graphql', query)
.reply(200, response)

expect(await getAlert('coffee-script', '', '/wwwroot', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: 'DISMISSED', cvss: 4.5, ghsaId: 'FOO' })

nock('https://api.github.com').post('/graphql', query)
.reply(200, responseWithManifestFileAtRoot)

expect(await getAlert('coffee-script', '', '/', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: 'DISMISSED', cvss: 4.5, ghsaId: 'FOO' })
})

test('it returns default if it does not match the version', async () => {
nock('https://api.github.com').post('/graphql', query)
.reply(200, response)

expect(await getAlert('coffee-script', '4.0.2', '/wwwroot', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: '', cvss: 0, ghsaId: '' })

nock('https://api.github.com').post('/graphql', query)
.reply(200, responseWithManifestFileAtRoot)

expect(await getAlert('coffee-script', '4.0.2', '/', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: '', cvss: 0, ghsaId: '' })
})

test('it returns default if it does not match the directory', async () => {
nock('https://api.github.com').post('/graphql', query)
.reply(200, response)

expect(await getAlert('coffee-script', '4.0.1', '/', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: '', cvss: 0, ghsaId: '' })

nock('https://api.github.com').post('/graphql', query)
.reply(200, responseWithManifestFileAtRoot)

expect(await getAlert('coffee-script', '4.0.1', '/wwwroot', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: '', cvss: 0, ghsaId: '' })
})

test('it returns default if it does not match the name', async () => {
nock('https://api.github.com').post('/graphql', query)
.reply(200, response)

expect(await getAlert('coffee', '4.0.1', '/wwwroot', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: '', cvss: 0, ghsaId: '' })

nock('https://api.github.com').post('/graphql', query)
.reply(200, responseWithManifestFileAtRoot)

expect(await getAlert('coffee', '4.0.1', '/', mockGitHubClient, mockGitHubPullContext())).toEqual({ alertState: '', cvss: 0, ghsaId: '' })
})

test('trimSlashes should only trim slashes from both ends', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/dependabot/verified_commits.ts
Expand Up @@ -78,7 +78,7 @@ export async function getAlert (name: string, version: string, directory: string

const nodes = alerts?.repository?.vulnerabilityAlerts?.nodes
const found = nodes.find(a => (version === '' || a.vulnerableRequirements === `= ${version}`) &&
trimSlashes(a.vulnerableManifestPath) === `${trimSlashes(directory)}/${a.vulnerableManifestFilename}` &&
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mwaddell without the propsed change, when package.json is at root, you get the following:

  • a.vulnerableManifestPath equals /package.json which makes trimSlashes(a.vulnerableManifestPath) resolve to package.json
  • `${trimSlashes(directory)}/${a.vulnerableManifestFilename}` is equivalent to `${trimSlashes('/')}/package.json` which yields /package.json

And so basically the slash trimmed paths never match, ending up making getAlert never return a matching security alert

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand now - thank you for the clarification

trimSlashes(a.vulnerableManifestPath) === trimSlashes(`${directory}/${a.vulnerableManifestFilename}`) &&
a.securityVulnerability.package.name === name)

return {
Expand Down