Exemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)

        if form.is_valid():
            url = request.POST['url']

            # different paths for both anonymous and registered users
            if (str(request.user) == 'AnonymousUser'):
                path = 'media/AnonymousUser/url/'
                user = None
            else:
                path = 'media/user/' + str(request.user) + '/url/'
                user = request.user
            scan_start_time = datetime.now()

            # logic to check how many files are already present for the scan
            dir_list = list()
            dir_list = os.listdir(path)
            file_name = ''

            if len(dir_list) == 0:
                file_name = '1'
            else:
                dir_list.sort()
                file_name = str(1 + int(dir_list[-1]))

            scan_directory = file_name
            scan_id = create_scan_id(user, url, scan_directory, scan_start_time)
            scan_code_async.delay(url, scan_id, path, file_name)
            return HttpResponseRedirect('/resultscan/' + str(scan_id))
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES)

        if form.is_valid():
            if (str(request.user) == 'AnonymousUser'):
                path = 'media/AnonymousUser/'
                user = None

            else:
                path = 'media/user/' + str(request.user) + '/'
                user = request.user

            subprocess.call(['mkdir', '-p', path])
            f = request.FILES['upload_from_local']
            fs = FileSystemStorage(path)
            filename = fs.save(f.name, f)

            path = path + str(filename)
            scan_directory = filename
            url = fs.url(filename)
            scan_start_time = timezone.now()
            scan_id = create_scan_id(user, url, scan_directory,
                                     scan_start_time)
            apply_scan_async.delay(path, scan_id)

            return HttpResponseRedirect('/resultscan/' + str(scan_id))
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)

        if form.is_valid():
            url = request.POST['url']
            logger = logging.getLogger(__name__)

            if request.user.is_authenticated():
                path = '/'.join(['media', 'user', str(request.user), 'url'])
                user = request.user
            else:
                path = '/'.join(['media', 'AnonymousUser', 'url'])
                user = None

            scan_start_time = timezone.now()
            git_url_parser = GitURL(url)

            if git_url_parser.host == 'github.com':
                file_name = git_url_parser.repo
                scan_directory = file_name
                scan_id = create_scan_id(user, url, scan_directory,
                                         scan_start_time)
                current_scan = Scan.objects.get(pk=scan_id)
                path = '/'.join(
                    [path, '{}'.format(current_scan.pk), file_name])

                os.makedirs(path)

                handle_special_urls.delay(url, scan_id, path,
                                          git_url_parser.host)
                logger.info('git repo detected')
            else:
                scan_directory = None
                scan_id = create_scan_id(user, url, scan_directory,
                                         scan_start_time)
                current_scan = Scan.objects.get(pk=scan_id)
                path = '/'.join([path, '{}'.format(current_scan.pk)])

                os.makedirs(path)

                file_name = '{}'.format(current_scan.pk)
                scan_code_async.delay(url, scan_id, path, file_name)

            return HttpResponseRedirect('/resultscan/' +
                                        '{}'.format(current_scan.pk))
Exemplo n.º 4
0
 def test_create_scan_id_anonymous_user(self):
     user = None
     url = 'https://github.com'
     scan_directory = 'media/url'
     scan_start_time = timezone.now()
     scan_id = create_scan_id(user=user,
                              url=url,
                              scan_directory=scan_directory,
                              scan_start_time=scan_start_time)
     self.assertEqual(url, Scan.objects.get(pk=scan_id).url)
Exemplo n.º 5
0
 def test_create_scan_id_registered_user(self):
     user = User.objects.create_user(username='******',
                                     password='******')
     url = 'https://github.com'
     scan_directory = 'media/user'
     scan_start_time = timezone.now()
     scan_id = create_scan_id(user=user,
                              url=url,
                              scan_directory=scan_directory,
                              scan_start_time=scan_start_time)
     self.assertEqual(user.username,
                      Scan.objects.get(pk=scan_id).user.username)