code

package
v0.0.0-...-dfd24c6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2023 License: Apache-2.0 Imports: 44 Imported by: 0

Documentation

Overview

* © 2023 Khulnasoft Limited All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.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.

Index

Constants

View Source
const (
	CreateBundleOperation           = "createBundle"
	ExtendBundleWithSourceOperation = "extendBundleWithSource"
	RunAnalysisOperation            = "runAnalysis"
	RunAutofixOperation             = "runAutofix"
	GetFiltersOperation             = "getFilters"
	FakeFileExtension               = ".java"
	// Helper constants to synchronize fake results and tests
	FakeAutofixSuggestionNewText = "FAKE_AUTOFIX_NEW_TEXT"
)

Variables

View Source
var (
	FakeVulnmapCodeApiServiceMutex = &sync.Mutex{}

	FakeCommand = vulnmap.CommandData{
		Title:     "Code Flow blah blah fake",
		CommandId: vulnmap.NavigateToRangeCommand,
		Arguments: []any{"path", fakeRange},
	}
	FakeFixCommand = vulnmap.CommandData{
		Title:     "Code Flow blah blah fake",
		CommandId: vulnmap.CodeFixCommand,
		Arguments: []any{"id", "path", fakeRange},
	}

	FakeIssue = vulnmap.Issue{
		ID:               "VULNMAP-123",
		Range:            fakeRange,
		Severity:         vulnmap.High,
		Product:          product.ProductCode,
		IssueType:        vulnmap.CodeQualityIssue,
		Message:          "This is a dummy error (severity error)",
		CodelensCommands: []vulnmap.CommandData{FakeCommand, FakeFixCommand},
		CodeActions:      []vulnmap.CodeAction{FakeCodeAction},
		AdditionalData: vulnmap.CodeIssueData{
			IsAutofixable: true,
		},
	}

	FakeCodeAction = vulnmap.CodeAction{
		Title:   "FakeAction",
		Command: &FakeCommand,
	}

	FakeFilters        = []string{".cjs", ".ejs", ".es", ".es6", ".htm", ".html", ".js", ".jsx", ".mjs", ".ts", ".tsx", ".vue", ".java", ".erb", ".haml", ".rb", ".rhtml", ".slim", ".kt", ".swift", ".cls", ".config", ".pom", ".wxs", ".xml", ".xsd", ".aspx", ".cs", ".py", ".go", ".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".hxx", ".php", ".phtml"}
	FakeAutofixFilters = []string{FakeFileExtension} // Main test scenario -- allowlist the fake file for autofix
)

Functions

func DecodePath

func DecodePath(encodedRelativePath string) (string, error)

func EncodePath

func EncodePath(relativePath string) string

func TempWorkdirWithVulnerabilities

func TempWorkdirWithVulnerabilities(t *testing.T) (filePath string, path string)

func ToAbsolutePath

func ToAbsolutePath(baseDir string, relativePath string) string

func ToRelativeUnixPath

func ToRelativeUnixPath(baseDir string, absoluteFilePath string) (string, error)

Types

type AnalysisOptions

type AnalysisOptions struct {
	// contains filtered or unexported fields
}

type AnalysisRequest

type AnalysisRequest struct {
	Key             AnalysisRequestKey `json:"key"`
	Severity        int                `json:"severity,omitempty"`
	Prioritized     bool               `json:"prioritized,omitempty"`
	Legacy          bool               `json:"legacy"`
	AnalysisContext codeRequestContext `json:"analysisContext"`
}

type AnalysisRequestKey

type AnalysisRequestKey struct {
	Type         string   `json:"type"`
	Hash         string   `json:"hash"`
	LimitToFiles []string `json:"limitToFiles,omitempty"`
	Shard        string   `json:"shard"`
}

type AnalysisStatus

type AnalysisStatus struct {
	// contains filtered or unexported fields
}

type AutofixFeedback

type AutofixFeedback struct {
	FixId           string             `json:"fixId"`
	Feedback        string             `json:"feedback"`
	AnalysisContext codeRequestContext `json:"analysisContext"`
}

type AutofixOptions

type AutofixOptions struct {
	// contains filtered or unexported fields
}

type AutofixRequest

type AutofixRequest struct {
	Key             AutofixRequestKey  `json:"key"`
	AnalysisContext codeRequestContext `json:"analysisContext"`
}

type AutofixRequestKey

type AutofixRequestKey struct {
	Type     string `json:"type"`
	Hash     string `json:"hash"`
	Shard    string `json:"shard"`
	FilePath string `json:"filePath"`
	RuleId   string `json:"ruleId"`
	// 1-based to comply with Sarif and Code API, see
	// https://docs.oasis-open.org/sarif/sarif/v2.1.0/sarif-v2.1.0.html#_Ref493492556
	LineNum int `json:"lineNum"`
}

type AutofixResponse

type AutofixResponse struct {
	Status             string                     `json:"status"`
	AutofixSuggestions []autofixResponseSingleFix `json:"fixes"`
}

AutofixResponse is the json-based structure to which we can translate the results of the HTTP request to Autofix upstream.

type AutofixStatus

type AutofixStatus struct {
	// contains filtered or unexported fields
}

type AutofixSuggestion

type AutofixSuggestion struct {
	FixId       string
	AutofixEdit vulnmap.WorkspaceEdit
}

AutofixSuggestion models a fix returned by autofix service

type Bundle

type Bundle struct {
	VulnmapCode   VulnmapCodeClient
	BundleHash    string
	UploadBatches []*UploadBatch
	Files         map[string]BundleFile
	// contains filtered or unexported fields
}

func (*Bundle) FetchDiagnosticsData

func (b *Bundle) FetchDiagnosticsData(
	ctx context.Context,
) ([]vulnmap.Issue, error)

func (*Bundle) Upload

func (b *Bundle) Upload(ctx context.Context, uploadBatch *UploadBatch) error

type BundleFile

type BundleFile struct {
	Hash    string `json:"hash"`
	Content string `json:"content"`
}

type BundleUploader

type BundleUploader struct {
	VulnmapCode VulnmapCodeClient
	// contains filtered or unexported fields
}

func NewBundler

func NewBundler(VulnmapCode VulnmapCodeClient, instrumentor performance.Instrumentor) *BundleUploader

func (*BundleUploader) Upload

func (b *BundleUploader) Upload(ctx context.Context, bundle Bundle, files map[string]BundleFile) (Bundle, error)

type FakeVulnmapCodeClient

type FakeVulnmapCodeClient struct {
	Calls               map[string][][]any
	HasCreatedNewBundle bool
	HasExtendedBundle   bool
	ExtendBundleFiles   map[string]BundleFile
	TotalBundleCount    int
	ExtendedBundleCount int
	AnalysisDuration    time.Duration
	FailOnCreateBundle  bool
	ConfigFiles         []string

	NoFixSuggestions bool
	// contains filtered or unexported fields
}

func (*FakeVulnmapCodeClient) Clear

func (f *FakeVulnmapCodeClient) Clear()

func (*FakeVulnmapCodeClient) CreateBundle

func (f *FakeVulnmapCodeClient) CreateBundle(_ context.Context,
	files map[string]string,
) (bundleHash string, missingFiles []string, err error)

func (*FakeVulnmapCodeClient) ExtendBundle

func (f *FakeVulnmapCodeClient) ExtendBundle(
	_ context.Context,
	bundleHash string,
	files map[string]BundleFile,
	removedFiles []string,
) (string, []string, error)

func (*FakeVulnmapCodeClient) GetAllCalls

func (f *FakeVulnmapCodeClient) GetAllCalls(op string) [][]any

func (*FakeVulnmapCodeClient) GetCallParams

func (f *FakeVulnmapCodeClient) GetCallParams(callNo int, op string) []any

func (*FakeVulnmapCodeClient) GetFilters

func (f *FakeVulnmapCodeClient) GetFilters(_ context.Context) (
	filters FiltersResponse,
	err error,
)

func (*FakeVulnmapCodeClient) RunAnalysis

func (*FakeVulnmapCodeClient) RunAutofix

func (f *FakeVulnmapCodeClient) RunAutofix(
	_ context.Context,
	options AutofixOptions,
	baseDir string,
) ([]AutofixSuggestion, AutofixStatus, error)

func (*FakeVulnmapCodeClient) SubmitAutofixFeedback

func (f *FakeVulnmapCodeClient) SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error

type FiltersResponse

type FiltersResponse struct {
	ConfigFiles []string `json:"configFiles" pact:"min=1"`
	Extensions  []string `json:"extensions" pact:"min=1"`
}

type SarifResponse

type SarifResponse struct {
	Type     string  `json:"type"`
	Progress float64 `json:"progress"`
	Status   string  `json:"status"`
	Timing   struct {
		FetchingCode int `json:"fetchingCode"`
		Queue        int `json:"queue"`
		Analysis     int `json:"analysis"`
	} `json:"timing"`
	Coverage []struct {
		Files       int    `json:"files"`
		IsSupported bool   `json:"isSupported"`
		Lang        string `json:"lang"`
	} `json:"coverage"`
	Sarif struct {
		Schema  string `json:"$schema"`
		Version string `json:"version"`
		Runs    []run  `json:"runs"`
	} `json:"sarif"`
}

type ScanMetrics

type ScanMetrics struct {
	// contains filtered or unexported fields
}

type ScanStatus

type ScanStatus struct {
	// contains filtered or unexported fields
}

func NewScanStatus

func NewScanStatus() *ScanStatus

type Scanner

type Scanner struct {
	BundleUploader   *BundleUploader
	VulnmapApiClient vulnmap_api.VulnmapApiClient
	// contains filtered or unexported fields
}

func New

func New(bundleUploader *BundleUploader,
	apiClient vulnmap_api.VulnmapApiClient,
	reporter error_reporting.ErrorReporter,
	analytics ux2.Analytics,
	learnService learn.Service,
	notifier notification.Notifier,
) *Scanner

func (*Scanner) IsEnabled

func (sc *Scanner) IsEnabled() bool

func (*Scanner) Product

func (sc *Scanner) Product() product.Product

func (*Scanner) Scan

func (sc *Scanner) Scan(ctx context.Context, path string, folderPath string) (issues []vulnmap.Issue, err error)

func (*Scanner) SupportedCommands

func (sc *Scanner) SupportedCommands() []vulnmap.CommandName

func (*Scanner) UploadAndAnalyze

func (sc *Scanner) UploadAndAnalyze(ctx context.Context,
	files <-chan string,
	path string,
	scanMetrics *ScanMetrics,
	changedFiles map[string]bool,
) (issues []vulnmap.Issue, err error)

type UploadBatch

type UploadBatch struct {
	// contains filtered or unexported fields
}

func NewUploadBatch

func NewUploadBatch() *UploadBatch

type UploadStatus

type UploadStatus struct {
	UploadedFiles int
	TotalFiles    int
}

type VulnmapAnalysisFailedError

type VulnmapAnalysisFailedError struct {
	Msg string
}

func (VulnmapAnalysisFailedError) Error

type VulnmapAutofixFailedError

type VulnmapAutofixFailedError struct {
	Msg string
}

Should implement `error` interface

func (VulnmapAutofixFailedError) Error

type VulnmapCodeClient

type VulnmapCodeClient interface {
	GetFilters(ctx context.Context) (
		filters FiltersResponse,
		err error)

	CreateBundle(
		ctx context.Context,
		files map[string]string,
	) (newBundleHash string, missingFiles []string, err error)

	ExtendBundle(
		ctx context.Context,
		bundleHash string,
		files map[string]BundleFile,
		removedFiles []string,
	) (newBundleHash string, missingFiles []string, err error)

	RunAnalysis(
		ctx context.Context,
		options AnalysisOptions,
		baseDir string,
	) (
		[]vulnmap.Issue,
		AnalysisStatus,
		error,
	)

	RunAutofix(
		ctx context.Context,
		options AutofixOptions,
		baseDir string,
	) ([]AutofixSuggestion,
		AutofixStatus,
		error,
	)

	SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error
}

type VulnmapCodeHTTPClient

type VulnmapCodeHTTPClient struct {
	// contains filtered or unexported fields
}

func NewHTTPRepository

func NewHTTPRepository(
	instrumentor performance2.Instrumentor,
	errorReporter error_reporting.ErrorReporter,
	client func() *http.Client,
) *VulnmapCodeHTTPClient

func (*VulnmapCodeHTTPClient) CreateBundle

func (s *VulnmapCodeHTTPClient) CreateBundle(
	ctx context.Context,
	filesToFilehashes map[string]string,
) (string, []string, error)

func (*VulnmapCodeHTTPClient) ExtendBundle

func (s *VulnmapCodeHTTPClient) ExtendBundle(
	ctx context.Context,
	bundleHash string,
	files map[string]BundleFile,
	removedFiles []string,
) (string, []string, error)

func (*VulnmapCodeHTTPClient) GetFilters

func (s *VulnmapCodeHTTPClient) GetFilters(ctx context.Context) (
	filters FiltersResponse,
	err error,
)

func (*VulnmapCodeHTTPClient) RunAnalysis

func (s *VulnmapCodeHTTPClient) RunAnalysis(
	ctx context.Context,
	options AnalysisOptions,
	baseDir string,
) ([]vulnmap.Issue, AnalysisStatus, error)

func (*VulnmapCodeHTTPClient) RunAutofix

func (s *VulnmapCodeHTTPClient) RunAutofix(
	ctx context.Context,
	options AutofixOptions,
	baseDir string,
) ([]AutofixSuggestion,
	AutofixStatus,
	error,
)

func (*VulnmapCodeHTTPClient) SubmitAutofixFeedback

func (s *VulnmapCodeHTTPClient) SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL