Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
bfa5bc7
Add serializers based on models #8
kartiksibal Aug 14, 2017
89f2bb1
Add DRF to requirements.txt
kartiksibal Aug 14, 2017
c75ae67
Remove un-used imports #8
kartiksibal Aug 14, 2017
4d1c6c1
Merge branch 'json_api' of https://github.com/nexB/vulnerablecode int…
kartiksibal Aug 14, 2017
585327a
Change data output format #8
kartiksibal Aug 20, 2017
37d16b2
Change API data format to as discussed #8
kartiksibal Aug 21, 2017
96d6bfd
Add test cases and change data output format #8
kartiksibal Aug 22, 2017
0705c24
Updated serialization logic
kartiksibal Aug 23, 2017
f08d614
Add many to many field on Package #8
kartiksibal Aug 23, 2017
692d72d
Remove un-used imports #8
kartiksibal Aug 23, 2017
fcadb17
Add test cases #8
kartiksibal Aug 24, 2017
d41215d
New migrations #8
kartiksibal Aug 24, 2017
9015a68
Update models #8
kartiksibal Aug 24, 2017
bdcb807
Minor formatting changes #8
kartiksibal Aug 24, 2017
42c7be2
Add test cases #8
kartiksibal Aug 25, 2017
d1821c4
Update unique together #8
kartiksibal Aug 25, 2017
8158a84
Add missing migration file along some code cleanup #8
tdruez Sep 7, 2017
0317f8a
Move tests to vulncode_app/tests #8
kartiksibal Sep 12, 2017
d533162
Re-order serializers according to models #8
kartiksibal Sep 12, 2017
5469f99
change API url to api/ from data/ #8
kartiksibal Sep 12, 2017
0bf6354
Update travis according to new dir structure
kartiksibal Sep 12, 2017
9b277d6
Update tests according to changed api url #8
kartiksibal Sep 12, 2017
5d5f089
Merge branch 'json_api' of https://github.com/nexB/vulnerablecode int…
kartiksibal Sep 12, 2017
16ea31a
Update readme for API instructions #8
kartiksibal Sep 12, 2017
86c359a
Refactor the codebase structure
tdruez Sep 20, 2017
b016877
Fix failing tests following codebase structure changes
tdruez Sep 21, 2017
23fc544
Add back Gitter webhooks removed by mistake
tdruez Sep 21, 2017
a860bef
Update URL regex #8
kartiksibal Sep 22, 2017
c42a775
Included platform in package serializer #8
kartiksibal Sep 22, 2017
2d6d5eb
Update tests according to changes #8
kartiksibal Sep 22, 2017
2c5f78d
Add a `vulnerabilities` m2m field on the Package model
tdruez Sep 22, 2017
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
14 changes: 12 additions & 2 deletions app/vulncode_app/data_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from vulncode_app.models import Vulnerability
from vulncode_app.models import VulnerabilityReference
from vulncode_app.models import ImpactedPackage

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Imports should be sorted

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorts import

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

from vulncode_app.models import Package


Expand All @@ -38,11 +39,16 @@ def debian_dump(extract_data):
vulnerability=vulnerability,
reference_id=data.get('vulnerability_id', ''),
)
Package.objects.create(
package = Package.objects.create(
name=data.get('package_name', ''),
version=data.get('fixed_version', ''),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Based on our chat, does it ever make sense to create package without name and version or both empty values?
Also what about the platform field? Same for Ubuntu

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Refer: #29

)

impacted_package = ImpactedPackage.objects.create(
vulnerability=vulnerability,
package=package
)


def ubuntu_dump(html):
"""
Expand All @@ -56,6 +62,10 @@ def ubuntu_dump(html):
vulnerability=vulnerability,
reference_id=data.get('cve_id'),
)
Package.objects.create(
package = Package.objects.create(
name=data.get('package_name'),
)
ImpactedPackage.objects.create(
vulnerability=vulnerability,
package=package
)
65 changes: 65 additions & 0 deletions app/vulncode_app/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/vulnerablecode/
# The VulnerableCode software is licensed under the Apache License version 2.0.
# Data generated with VulnerableCode require an acknowledgment.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
# derivative work, you must accompany this data with the following acknowledgment:
#
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/vulnerablecode/ for support and download.

from rest_framework import serializers

from vulncode_app.models import Vulnerability

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Import should be sorted

from vulncode_app.models import VulnerabilityReference
from vulncode_app.models import Package
from vulncode_app.models import PackageReference
from vulncode_app.models import ImpactedPackage


class PackageReferenceSerializer(serializers.ModelSerializer):
class Meta:
model = PackageReference
fields = ('name', 'version')


class VulnerabilityReferenceSerializer(serializers.ModelSerializer):
class Meta:
model = VulnerabilityReference
fields = ('reference_id',)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You want to add the other reference fields too there.



class VulnerabilitySerializer(serializers.ModelSerializer):
reference = VulnerabilityReferenceSerializer(source='vulnerabilityreference_set', many=True)

class Meta:
model = Vulnerability
fields = ('summary', 'reference')


class PackageSerializer(serializers.ModelSerializer):
class Meta:
model = Package
fields = ('name', 'version')


class ImpactedPackageSerializer(serializers.ModelSerializer):
package = PackageSerializer()
vulnerability = VulnerabilitySerializer()

class Meta:
model = ImpactedPackage
fields = ('package', 'vulnerability')
83 changes: 83 additions & 0 deletions app/vulncode_app/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/vulnerablecode/
# The VulnerableCode software is licensed under the Apache License version 2.0.
# Data generated with VulnerableCode require an acknowledgment.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
# derivative work, you must accompany this data with the following acknowledgment:
#
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/vulnerablecode/ for support and download.

import json

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sort your imports

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

no empty lines between a block of imports from the same kind (here stdlib)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

from drf_multiple_model.views import MultipleModelAPIView
from django.test import TestCase
from rest_framework.response import Response

from vulncode_app.data_dump import debian_dump
from vulncode_app.data_dump import ubuntu_dump

from scraper import debian
from scraper import ubuntu


class TestSerializers(TestCase):
def test_debian_response(self):
with open('tests/test_data/debian.json') as f:
test_data = json.loads(f.read())

extract_data = debian.extract_vulnerabilities(test_data)
debian_dump(extract_data)
response = self.client.get('/vulncode_app/data/mimetex', format='json')

expected = {
"name": "mimetex",
"vulnerabilities": [
{
"summary": "Multiple stack-based buffer overflows in mimetex.cgi in mimeTeX",
"reference_id": "CVE-2009-2458",
"version": "1.50-1.1"
},
{
"summary": "Multiple unspecified vulnerabilities in mimeTeX.",
"reference_id": "CVE-2009-2459",
"version": "1.50-1.1"
}
]
}

self.assertEqual(expected, response.data)

def test_ubuntu_response(self):
with open('tests/test_data/ubuntu_main.html') as f:
test_data = f.read()

extract_data = ubuntu.extract_cves(test_data)
ubuntu_dump(extract_data)
response = self.client.get('/vulncode_app/data/automake', format='json')

expected = {
"name": "automake",
"vulnerabilities": [
{
"summary": "",
"reference_id": "CVE-2012-3386",
"version": ""
}
]
}

self.assertEqual(expected, response.data)
18 changes: 14 additions & 4 deletions app/vulncode_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@
# Visit https://github.com/nexB/vulnerablecode/ for support and download.

from django.conf.urls import url

from . import views
from rest_framework.urlpatterns import format_suffix_patterns

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Empty line needed after this

from vulncode_app import views


urlpatterns = [
url(r'(?P<name>[a-z]+)/(?P<version>[0-9]+)', views.package_version, name='package_version'),
url(r'^(?P<name>[a-z]+)', views.package, name='package'),
url(r'^cve-search/(?P<name>[a-z]+)/(?P<version>[0-9]+)',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you call this cve-search?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@pombredanne because it is getting its's data from CVE-SEARCH API, that we worked out in the beginning.

views.package_version,
name='package_version'),

url(r'^cve-search/(?P<name>[a-z]+)',
views.package,
name='package'),

url(r'^data/(?P<package_name>[a-z]+)',
views.VulnerabilityData.as_view()),
]

urlpatterns = format_suffix_patterns(urlpatterns)
32 changes: 32 additions & 0 deletions app/vulncode_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,42 @@
import json

from django.http import HttpResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from vulncode_app.serializers import ImpactedPackageSerializer
from vulncode_app.models import ImpactedPackage
from vulncode_app.models import Package
from vulncode_app import api_data


class VulnerabilityData(APIView):
def get(self, request, package_name):
vulnerability = []
extract = []
data = []

pk = Package.objects.filter(name=package_name)

for i, v in enumerate(pk):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is the wrong approach and should be handled by the proper serializers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@kartiksibal rather than for i, v in enumerate(pk): just iterate on the packages:

packages = Package.objects.filter(name=package_name)
for package in packages:
  ....

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you are still enumerating pk. Instead loop on packages:
for package in Package.objects.filter(name=package_name): ....

vulnerability.append(ImpactedPackage.objects.filter(pk=v.id))
extract += ImpactedPackageSerializer(vulnerability[i], many=True).data

data.append({
'summary': extract[i]['vulnerability']['summary'],
'reference_id': extract[i]['vulnerability']['reference'][0]['reference_id'],
'version': extract[i]['package']['version']
})

response = {
'name': package_name,
'vulnerabilities': data
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Empty line not needed here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

return Response(response)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only one line between class methods

@kartiksibal kartiksibal Aug 24, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not a class method.


def package(request, name):
"""
Queries the cve-search api with just
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
beautifulsoup4==4.6.0
lxml==3.8.0
django==1.11.4
djangorestframework==3.6

# Tests
pytest==3.1.3