示例#1
0
def bug_save_editor_data():
    try:
        vulnerability_details = VulnerabilityDetails()
        vulnerability_details.validate()
    except InvalidIdentifierException as e:
        return create_json_response(str(e), 400)
    vuln_view = vulnerability_details.vulnerability_view

    if request.method == "POST":
        if not vuln_view:
            return create_json_response("Please create an entry first", 404)

        if not vuln_view.master_commit:
            current_app.logger.error(
                f"Vuln (id: {vuln_view.id}) has no linked Git commits!")
            return create_json_response("Entry has no linked Git link!", 404)

        master_commit = vulnerability_details.getMasterCommit()

        # print("DATA: {request.json}"
        old_files = master_commit.repository_files
        current_app.logger.debug("%d old files", len(old_files))
        # Flush any old custom content of this vulnerability first.
        new_files = []
        for file in request.get_json():
            for of in old_files:
                if of.file_path == file["path"] or of.file_hash == file["hash"]:
                    current_app.logger.debug(
                        "Found old file: %s",
                        (file["path"], file["hash"], file["name"]))
                    file_obj = of
                    break
            else:
                current_app.logger.debug(
                    "Creating new file: %s",
                    (file["path"], file["hash"], file["name"]))
                file_obj = RepositoryFiles(
                    file_name=file["name"],
                    file_path=file["path"],
                    file_patch="DEPRECATED",
                    file_hash=file["hash"],
                )
            # Create comment objects.
            new_comments = []
            for comment in file["comments"]:
                comment_obj = RepositoryFileComments(
                    row_from=comment["row_from"],
                    row_to=comment["row_to"],
                    text=comment["text"],
                    sort_pos=comment["sort_pos"],
                    creator=g.user,
                )
                new_comments.append(comment_obj)
            update_file_comments(file_obj, new_comments)
            # Create marker objects.
            new_markers = []
            for marker in file["markers"]:
                marker_obj = RepositoryFileMarkers(
                    row_from=marker["row_from"],
                    row_to=marker["row_to"],
                    column_from=marker["column_from"],
                    column_to=marker["column_to"],
                    marker_class=marker["class"],
                    creator=g.user,
                )
                new_markers.append(marker_obj)
            update_file_markers(file_obj, new_markers)
            new_files.append(file_obj)

        current_app.logger.debug("Setting %d files", len(new_files))
        master_commit.repository_files = new_files

        # Update / Insert entries into the database.
        db.session.commit()
        return create_json_response("Update successful.")
    return create_json_response("Accepting only POST requests.", 400)
示例#2
0
文件: api.py 项目: v1cker/vulncode-db
def bug_save_editor_data():
  try:
    vulnerability_details = VulnerabilityDetails()
    vulnerability_details.validate()
  except InvalidIdentifierException as e:
    return createJsonResponse(str(e), 400)
  vuln_view = vulnerability_details.vulnerability_view

  if request.method == 'POST':
    if not vuln_view:
      return createJsonResponse('Please create an entry first', 404)

    if not vuln_view.master_commit:
      current_app.logger.error(
          'Vuln (id: {:d}) has no linked Git commits!'.format(vuln_view.id))
      return createJsonResponse('Entry has no linked Git link!', 404)

    master_commit = vulnerability_details.getMasterCommit()

    #print("DATA: {:s}".format(str(request.json)))
    old_files = master_commit.repository_files
    current_app.logger.debug('%d old files', len(old_files))
    # Flush any old custom content of this vulnerability first.
    new_files = []
    for file in request.get_json():
      for of in old_files:
        if of.file_path == file['path'] or of.file_hash == file['hash']:
          current_app.logger.debug('Found old file: %s',
                                   (file['path'], file['hash'], file['name']))
          file_obj = of
          break
      else:
        current_app.logger.debug('Creating new file: %s',
                                 (file['path'], file['hash'], file['name']))
        file_obj = RepositoryFiles(
            file_name=file['name'],
            file_path=file['path'],
            file_patch='DEPRECATED',
            file_hash=file['hash'],
        )
      # Create comment objects.
      new_comments = []
      for comment in file['comments']:
        comment_obj = RepositoryFileComments(
            row_from=comment['row_from'],
            row_to=comment['row_to'],
            text=comment['text'],
            sort_pos=comment['sort_pos'],
            creator=g.user,
        )
        new_comments.append(comment_obj)
      update_file_comments(file_obj, new_comments)
      # Create marker objects.
      new_markers = []
      for marker in file['markers']:
        marker_obj = RepositoryFileMarkers(
            row_from=marker['row_from'],
            row_to=marker['row_to'],
            column_from=marker['column_from'],
            column_to=marker['column_to'],
            marker_class=marker['class'],
            creator=g.user,
        )
        new_markers.append(marker_obj)
      update_file_markers(file_obj, new_markers)
      new_files.append(file_obj)

    current_app.logger.debug('Setting %d files', len(new_files))
    master_commit.repository_files = new_files

    # Update / Insert entries into the database.
    db.session.commit()
    return createJsonResponse('Update successful.')
  return createJsonResponse('Accepting only POST requests.', 400)