internal/codescan/libraries.go

545ba0bcdea3b534903bd78c918c0c025a97eda7
gitbay/internal/codescan/libraries.go

115 lines · 5387 bytes

5 symbols in this file
  1package codescan
  2
  3import "strings"
  4
  5// library is a family of rules that is about one library: opengrep-rules
  6// keeps those under <language>/<library>/, which a rule id carries as
  7// language.library. Its findings apply only to a repository whose manifests
  8// name one of packages, written ecosystem:name; a go: name ending in / is a
  9// module path prefix, and root: is an entry at the top of the tree.
 10// Directories that are about a language, a runtime or the standard library
 11// (lang, browser, audit, correctness, template, node-crypto, …) are not
 12// here, so their rules apply everywhere. Neither are languages whose
 13// manifests gitbay does not read.
 14type library struct {
 15	rules    string
 16	packages []string
 17}
 18
 19var libraries = []library{
 20	{"go.aws-lambda", []string{"go:github.com/aws/aws-lambda-go"}},
 21	{"go.gorilla", []string{"go:github.com/gorilla/"}},
 22	{"go.gorm", []string{"go:gorm.io/gorm", "go:github.com/jinzhu/gorm"}},
 23	{"go.grpc", []string{"go:google.golang.org/grpc"}},
 24	{"go.jwt-go", []string{"go:github.com/dgrijalva/jwt-go", "go:github.com/golang-jwt/jwt/", "go:github.com/form3tech-oss/jwt-go"}},
 25	{"go.otto", []string{"go:github.com/robertkrimen/otto"}},
 26
 27	{"javascript.ajv", []string{"npm:ajv"}},
 28	{"javascript.angular", []string{"npm:angular", "npm:@angular/core"}},
 29	{"javascript.apollo", []string{"npm:apollo-server", "npm:apollo-server-express", "npm:@apollo/server"}},
 30	{"javascript.argon2", []string{"npm:argon2"}},
 31	{"javascript.bluebird", []string{"npm:bluebird"}},
 32	{"javascript.chrome-remote-interface", []string{"npm:chrome-remote-interface"}},
 33	{"javascript.express", []string{"npm:express"}},
 34	{"javascript.fbjs", []string{"npm:fbjs"}},
 35	{"javascript.grpc", []string{"npm:grpc", "npm:@grpc/grpc-js"}},
 36	{"javascript.jose", []string{"npm:jose", "npm:node-jose"}},
 37	{"javascript.jquery", []string{"npm:jquery"}},
 38	{"javascript.jsonwebtoken", []string{"npm:jsonwebtoken"}},
 39	{"javascript.jwt-simple", []string{"npm:jwt-simple"}},
 40	{"javascript.monaco-editor", []string{"npm:monaco-editor"}},
 41	{"javascript.node-expat", []string{"npm:node-expat"}},
 42	{"javascript.passport-jwt", []string{"npm:passport-jwt"}},
 43	{"javascript.phantom", []string{"npm:phantom"}},
 44	{"javascript.playwright", []string{"npm:playwright", "npm:@playwright/test"}},
 45	{"javascript.puppeteer", []string{"npm:puppeteer", "npm:puppeteer-core"}},
 46	{"javascript.react", []string{"npm:react"}},
 47	{"javascript.sandbox", []string{"npm:sandbox"}},
 48	{"javascript.sax", []string{"npm:sax"}},
 49	{"javascript.sequelize", []string{"npm:sequelize"}},
 50	{"javascript.serialize-javascript", []string{"npm:serialize-javascript"}},
 51	{"javascript.shelljs", []string{"npm:shelljs"}},
 52	{"javascript.thenify", []string{"npm:thenify"}},
 53	{"javascript.vm2", []string{"npm:vm2"}},
 54	{"javascript.vue", []string{"npm:vue"}},
 55	{"javascript.wkhtmltoimage", []string{"npm:wkhtmltoimage"}},
 56	{"javascript.wkhtmltopdf", []string{"npm:wkhtmltopdf"}},
 57	{"javascript.xml2json", []string{"npm:xml2json"}},
 58
 59	{"typescript.angular", []string{"npm:@angular/core"}},
 60	{"typescript.aws-cdk", []string{"npm:aws-cdk-lib", "npm:@aws-cdk/core"}},
 61	{"typescript.nestjs", []string{"npm:@nestjs/core"}},
 62	{"typescript.react", []string{"npm:react"}},
 63	{"typescript.react.portability.i18next", []string{"npm:i18next", "npm:react-i18next"}},
 64
 65	{"python.airflow", []string{"pypi:apache-airflow"}},
 66	{"python.attr", []string{"pypi:attrs"}},
 67	{"python.bokeh", []string{"pypi:bokeh"}},
 68	{"python.boto3", []string{"pypi:boto3"}},
 69	{"python.click", []string{"pypi:click"}},
 70	{"python.cryptography", []string{"pypi:cryptography"}},
 71	{"python.distributed", []string{"pypi:distributed", "pypi:dask"}},
 72	{"python.django", []string{"pypi:django"}},
 73	{"python.docker", []string{"pypi:docker"}},
 74	{"python.fastapi", []string{"pypi:fastapi"}},
 75	{"python.flask", []string{"pypi:flask"}},
 76	{"python.jinja2", []string{"pypi:jinja2", "pypi:flask"}},
 77	{"python.jwt", []string{"pypi:pyjwt"}},
 78	{"python.pycryptodome", []string{"pypi:pycryptodome", "pypi:pycryptodomex"}},
 79	{"python.pymongo", []string{"pypi:pymongo"}},
 80	{"python.pyramid", []string{"pypi:pyramid"}},
 81	{"python.requests", []string{"pypi:requests"}},
 82	{"python.sh", []string{"pypi:sh"}},
 83	{"python.sqlalchemy", []string{"pypi:sqlalchemy", "pypi:flask-sqlalchemy"}},
 84	{"python.twilio", []string{"pypi:twilio"}},
 85
 86	// Lints for writing Semgrep rules, which match any YAML with a list
 87	// of rules that have ids.
 88	{"yaml.semgrep", []string{"root:.semgrep", "root:.semgrep.yml", "root:.semgrep.yaml", "pypi:semgrep"}},
 89}
 90
 91// libraryFor returns the most specific library a rule id belongs to. The
 92// id may carry a prefix from where the rules were mounted
 93// (opt.opengrep-rules.python.django…).
 94func libraryFor(rule string) (library, bool) {
 95	var best library
 96	found := false
 97	for _, l := range libraries {
 98		if ruleIn(rule, l.rules) && len(l.rules) > len(best.rules) {
 99			best, found = l, true
100		}
101	}
102	return best, found
103}
104
105// ruleIn reports whether a rule id is family or within it: family is a
106// whole id, a run of its dot-separated segments, or a suffix.
107func ruleIn(rule, family string) bool {
108	return rule == family || strings.HasPrefix(rule, family+".") ||
109		strings.HasSuffix(rule, "."+family) || strings.Contains(rule, "."+family+".")
110}
111
112// name is how the issue and status refer to a library.
113func (l library) name() string {
114	return l.rules[strings.LastIndex(l.rules, ".")+1:]
115}