mirror of
https://github.com/aquasecurity/trivy.git
synced 2026-02-08 09:43:11 +08:00
Signed-off-by: Arunprasad Rajkumar <arajkuma@redhat.com> Signed-off-by: guoguangwu <guoguangwu@magic-shield.com> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: knqyf263 <knqyf263@gmail.com> Co-authored-by: Masahiro <mur4m4s4.331@gmail.com> Co-authored-by: Tomoya Amachi <tomoya.amachi@gmail.com> Co-authored-by: Masahiro <lomycisw@gmail.com> Co-authored-by: Liz Rice <liz@lizrice.com> Co-authored-by: Johannes <johannes@jitesoft.com> Co-authored-by: aprp <doelaudi@gmail.com> Co-authored-by: rahul2393 <rahulyadavsep92@gmail.com> Co-authored-by: Arunprasad Rajkumar <ar.arunprasad@gmail.com> Co-authored-by: Emrecan BATI <emrecanbati@gmail.com> Co-authored-by: sherif84 <12298259+sherif84@users.noreply.github.com> Co-authored-by: Sherif Fathalla <sfathall@akamai.com> Co-authored-by: sherif <sherif.mailbox@gmail.com> Co-authored-by: Sam Lane <samuel.lane@hotmail.com> Co-authored-by: Ankush K <akhobragade@gmail.com> Co-authored-by: Ankush K <akhobragade42@gmail.com> Co-authored-by: Tauseef <tauseefmlk@gmail.com> Co-authored-by: Daniel <danfaizer@gmail.com> Co-authored-by: Matthieu MOREL <mmorel-35@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: afdesk <work@afdesk.com> Co-authored-by: AndreyLevchenko <levchenko.andrey@gmail.com> Co-authored-by: Kobus van Schoor <10784365+kobus-v-schoor@users.noreply.github.com> Co-authored-by: Jan-Otto Kröpke <github@jkroepke.de> Co-authored-by: jerbob92 <jerbob92@users.noreply.github.com> Co-authored-by: DmitriyLewen <91113035+DmitriyLewen@users.noreply.github.com> Co-authored-by: Shira Cohen <97398476+ShiraCohen33@users.noreply.github.com> Co-authored-by: astevenson-microsoft <78623826+astevenson-microsoft@users.noreply.github.com> Co-authored-by: Kyriakos Georgiou <kgeorgiou@users.noreply.github.com> Co-authored-by: mycodeself <mycodeself@users.noreply.github.com> Co-authored-by: DavidSalame <75929252+davidsalame1@users.noreply.github.com> Co-authored-by: Tom Fay <tom@teamfay.co.uk> Co-authored-by: Tom Fay <tomfay@microsoft.com> Co-authored-by: François Poirotte <fpoirotte@users.noreply.github.com> Co-authored-by: Guy Ben-Aharon <baguy3@gmail.com> Co-authored-by: Catminusminus <37803616+Catminusminus@users.noreply.github.com> Co-authored-by: Lior Vaisman Argon <97836016+VaismanLior@users.noreply.github.com> Co-authored-by: Matthieu Maitre <mmaitre@microsoft.com> Co-authored-by: Andrea Scarpino <andrea@scarpino.dev> Co-authored-by: MorAlon1 <101275199+MorAlon1@users.noreply.github.com> Co-authored-by: liorj-orca <96177663+liorj-orca@users.noreply.github.com> Co-authored-by: Nikita Pivkin <100182843+nikpivkin@users.noreply.github.com> Co-authored-by: guangwu <guoguangwu@magic-shield.com> Co-authored-by: Nikita Pivkin <nikita.pivkin@smartforce.io> Co-authored-by: DmitriyLewen <dmitriy.lewen@smartforce.io> Co-authored-by: yuriShafet <5830215+yuriShafet@users.noreply.github.com> Co-authored-by: Octogonapus <firey45@gmail.com>
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package pip
|
|
|
|
import (
|
|
"bufio"
|
|
"strings"
|
|
"unicode"
|
|
|
|
"golang.org/x/text/encoding"
|
|
u "golang.org/x/text/encoding/unicode"
|
|
"golang.org/x/text/transform"
|
|
"golang.org/x/xerrors"
|
|
|
|
dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
|
|
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
|
|
)
|
|
|
|
const (
|
|
commentMarker string = "#"
|
|
endColon string = ";"
|
|
hashMarker string = "--"
|
|
startExtras string = "["
|
|
endExtras string = "]"
|
|
)
|
|
|
|
type Parser struct{}
|
|
|
|
func NewParser() types.Parser {
|
|
return &Parser{}
|
|
}
|
|
|
|
func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
|
|
// `requirements.txt` can use byte order marks (BOM)
|
|
// e.g. on Windows `requirements.txt` can use UTF-16LE with BOM
|
|
// We need to override them to avoid the file being read incorrectly
|
|
var transformer = u.BOMOverride(encoding.Nop.NewDecoder())
|
|
decodedReader := transform.NewReader(r, transformer)
|
|
|
|
scanner := bufio.NewScanner(decodedReader)
|
|
var libs []types.Library
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
line = strings.ReplaceAll(line, " ", "")
|
|
line = strings.ReplaceAll(line, `\`, "")
|
|
line = removeExtras(line)
|
|
line = rStripByKey(line, commentMarker)
|
|
line = rStripByKey(line, endColon)
|
|
line = rStripByKey(line, hashMarker)
|
|
s := strings.Split(line, "==")
|
|
if len(s) != 2 {
|
|
continue
|
|
}
|
|
libs = append(libs, types.Library{
|
|
Name: s[0],
|
|
Version: s[1],
|
|
})
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, nil, xerrors.Errorf("scan error: %w", err)
|
|
}
|
|
return libs, nil, nil
|
|
}
|
|
|
|
func rStripByKey(line, key string) string {
|
|
if pos := strings.Index(line, key); pos >= 0 {
|
|
line = strings.TrimRightFunc((line)[:pos], unicode.IsSpace)
|
|
}
|
|
return line
|
|
}
|
|
|
|
func removeExtras(line string) string {
|
|
startIndex := strings.Index(line, startExtras)
|
|
endIndex := strings.Index(line, endExtras) + 1
|
|
if startIndex != -1 && endIndex != -1 {
|
|
line = line[:startIndex] + line[endIndex:]
|
|
}
|
|
return line
|
|
}
|