Skip to content

Commit

Permalink
Merge pull request #1065 from qtomlinson/qt/fix-pypi
Browse files Browse the repository at this point in the history
Restrict PyPI coordinates name in PypiCoordinatesMapper
  • Loading branch information
qtomlinson authored Mar 6, 2024
2 parents d4f30a1 + 2fcd2f7 commit eabeb40
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/pypiCoordinatesMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ class PypiCoordinatesMapper {
}

_shouldResolve(coordinates) {
return coordinates.name.includes('.') ||
coordinates.name.includes('_') ||
coordinates.name.includes('-')
if (typeof coordinates.name !== 'string' || coordinates.name.includes('/')) return false
return coordinates.name.includes('.') || coordinates.name.includes('_') || coordinates.name.includes('-')
}

async _resolve(coordinates) {
const url = `${this.baseUrl}/pypi/${coordinates.name}/json`
try {
const answer = await this._handleRequest(url)
return answer?.info?.name && { name: answer.info.name }
return answer?.info?.name && { name: answer.info.name }
} catch (error) {
if (error.statusCode === 404) return null
throw error
Expand Down
24 changes: 24 additions & 0 deletions test/lib/pypiCoordinatesMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ describe('PypiCoordinatesMapper', () => {
const mapped = await coordinatesMapper.map(mockPypiCoordinates('backports'))
expect(mapped).to.be.null
})

it('should return null when pypi name to be mapped is invalid', async () => {
sinon.stub(coordinatesMapper, '_handleRequest').rejects('Should not be called')
const spec = {
type: 'pypi',
provider: 'pypi',
name: 'back.ports/test',
revision: '1.0.0'
}
const coordinates = EntityCoordinates.fromObject(spec)
const mapped = await coordinatesMapper.map(coordinates)
expect(mapped).to.be.null
})

it('should return null given no name', async () => {
sinon.stub(coordinatesMapper, '_handleRequest').rejects('Should not be called')
const spec = {
type: 'pypi',
provider: 'pypi'
}
const coordinates = EntityCoordinates.fromObject(spec)
const mapped = await coordinatesMapper.map(coordinates)
expect(mapped).to.be.null
})
})


0 comments on commit eabeb40

Please sign in to comment.