mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 10:03:46 +01:00
217 lines
6.6 KiB
Go
217 lines
6.6 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"github.com/phishingclub/phishingclub/database/dbengine"
|
|
"github.com/phishingclub/phishingclub/registry"
|
|
)
|
|
|
|
// Admin imports refuse text that is not valid UTF-8 or holds a NUL on
|
|
// postgres. Postgres can not store such text, the database layer would put
|
|
// dbengine.Replacement in place of each bad byte, so a file saved in another
|
|
// encoding (such as Windows-1252 from Excel) would lose its accented letters
|
|
// without a word. The operator gets an error naming the file instead and saves
|
|
// it as UTF-8. sqlite stores the bytes as they are, so nothing is refused
|
|
// there. The cleaning in the database layer stays for every other path.
|
|
|
|
// ImportTextIssue is a file, and for a CSV the line, whose text is not valid
|
|
// UTF-8 or holds a NUL
|
|
type ImportTextIssue struct {
|
|
File string `json:"file"`
|
|
Line int `json:"line"`
|
|
}
|
|
|
|
// refusesImportText is true when imports refuse text that is not clean
|
|
func refusesImportText() bool {
|
|
return dbengine.IsPostgres()
|
|
}
|
|
|
|
// notUTF8Err is the error an import gives for a file, or a line of a CSV
|
|
// when line is above zero, that is not UTF-8. The name is quoted, as it can
|
|
// hold any bytes.
|
|
func notUTF8Err(file string, line int) error {
|
|
if line > 0 {
|
|
return fmt.Errorf("file %q line %d is not UTF-8, save it as UTF-8 and import again", file, line)
|
|
}
|
|
return fmt.Errorf("file %q is not UTF-8, save it as UTF-8 and import again", file)
|
|
}
|
|
|
|
// importTextErr returns notUTF8Err for file when text is not clean and
|
|
// imports refuse it
|
|
func importTextErr(file string, text string) error {
|
|
if !refusesImportText() || dbengine.IsCleanText(text) {
|
|
return nil
|
|
}
|
|
return notUTF8Err(file, 0)
|
|
}
|
|
|
|
// ImportTextRowIssue returns the issue for a row of an uploaded CSV file,
|
|
// starting on line, whose text is not clean, when imports refuse it, else nil.
|
|
// The service refuses the import with importTextIssuesErr after its
|
|
// authorization check.
|
|
func ImportTextRowIssue(file string, line int, fields []string) *ImportTextIssue {
|
|
if !refusesImportText() {
|
|
return nil
|
|
}
|
|
for _, f := range fields {
|
|
if !dbengine.IsCleanText(f) {
|
|
return &ImportTextIssue{File: file, Line: line}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// importValuesErr is importTextErr for every string a decoded file holds,
|
|
// such as a parsed data.yaml. The YAML parser refuses bytes that are not
|
|
// UTF-8, but an escape such as "\0" gives a NUL.
|
|
func importValuesErr(file string, v any) error {
|
|
if !refusesImportText() || textValuesClean(reflect.ValueOf(v)) {
|
|
return nil
|
|
}
|
|
return notUTF8Err(file, 0)
|
|
}
|
|
|
|
// textValuesClean reports whether every string in v, through pointers,
|
|
// structs, slices and maps, is clean text
|
|
func textValuesClean(v reflect.Value) bool {
|
|
switch v.Kind() {
|
|
case reflect.String:
|
|
return dbengine.IsCleanText(v.String())
|
|
case reflect.Pointer, reflect.Interface:
|
|
return v.IsNil() || textValuesClean(v.Elem())
|
|
case reflect.Struct:
|
|
for i := 0; i < v.NumField(); i++ {
|
|
if !textValuesClean(v.Field(i)) {
|
|
return false
|
|
}
|
|
}
|
|
case reflect.Slice, reflect.Array:
|
|
for i := 0; i < v.Len(); i++ {
|
|
if !textValuesClean(v.Index(i)) {
|
|
return false
|
|
}
|
|
}
|
|
case reflect.Map:
|
|
it := v.MapRange()
|
|
for it.Next() {
|
|
if !textValuesClean(it.Key()) || !textValuesClean(it.Value()) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// importTextIssuesErr returns the error for the first issue the client found
|
|
// in the files of an import, when imports refuse such text. The client reads
|
|
// the file itself (a recipient CSV is parsed in the browser), so only it knows
|
|
// the file and line.
|
|
func importTextIssuesErr(issues []ImportTextIssue) error {
|
|
if !refusesImportText() || len(issues) == 0 {
|
|
return nil
|
|
}
|
|
first := issues[0]
|
|
name := first.File
|
|
if r := []rune(name); len(r) > 255 {
|
|
name = string(r[:255])
|
|
}
|
|
return notUTF8Err(name, max(first.Line, 0))
|
|
}
|
|
|
|
// packageTextErr returns an error naming the file when a Bait Box package
|
|
// holds text the install stores in the database that is not clean, when
|
|
// imports refuse it: the values of each data.yaml and the files its pages,
|
|
// emails, scripts, report templates, proxies, remote browsers and domains
|
|
// name. Assets and attachments are stored as files, not as text, and are not
|
|
// checked.
|
|
func packageTextErr(pkg *registry.VerifiedPackage) error {
|
|
if !refusesImportText() || pkg == nil || pkg.Files == nil {
|
|
return nil
|
|
}
|
|
id := ""
|
|
if pkg.Manifest != nil {
|
|
id = pkg.Manifest.ID
|
|
}
|
|
// the callers wrap the error as a validation error
|
|
refuse := func(path string) error {
|
|
return fmt.Errorf("file %q of package %s is not UTF-8, save it as UTF-8 and publish the package again", path, id)
|
|
}
|
|
docs, err := contentDocs(pkg)
|
|
if err != nil {
|
|
// the install reports a data.yaml that does not parse
|
|
return nil
|
|
}
|
|
for _, doc := range docs {
|
|
dataPath := doc.prefix + "data.yaml"
|
|
if !textValuesClean(reflect.ValueOf(doc.dy)) {
|
|
return refuse(dataPath)
|
|
}
|
|
for _, f := range packageTextFiles(doc.dy) {
|
|
if f == "" {
|
|
continue
|
|
}
|
|
if body, ok := pkg.Files[doc.prefix+f]; ok && !dbengine.IsCleanText(string(body)) {
|
|
return refuse(doc.prefix + f)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// packageTextError is the error fetchPackage gives, together with the package,
|
|
// for a package that packageTextErr refuses
|
|
type packageTextError struct{ error }
|
|
|
|
func (e packageTextError) Unwrap() error { return e.error }
|
|
|
|
// textChecked returns the package, with a packageTextError when packageTextErr
|
|
// refuses it
|
|
func textChecked(pkg *registry.VerifiedPackage) (*registry.VerifiedPackage, error) {
|
|
if err := packageTextErr(pkg); err != nil {
|
|
return pkg, packageTextError{err}
|
|
}
|
|
return pkg, nil
|
|
}
|
|
|
|
// manifestReadable returns the package for a caller that reads only its
|
|
// manifest or its variables and writes none of its content: a package refused
|
|
// only for its text is returned without the error. Any other error is kept.
|
|
func manifestReadable(p *registry.VerifiedPackage, err error) (*registry.VerifiedPackage, error) {
|
|
var te packageTextError
|
|
if p != nil && errors.As(err, &te) {
|
|
return p, nil
|
|
}
|
|
return p, err
|
|
}
|
|
|
|
// packageTextFiles are the files a data.yaml names whose content the install
|
|
// stores as text
|
|
func packageTextFiles(dy *installDataYAML) []string {
|
|
var out []string
|
|
for _, p := range dy.Pages {
|
|
out = append(out, p.File)
|
|
}
|
|
for _, e := range dy.Emails {
|
|
out = append(out, e.File)
|
|
}
|
|
for _, sc := range dy.Scripts {
|
|
out = append(out, sc.File)
|
|
}
|
|
for _, rt := range dy.ReportTemplates {
|
|
out = append(out, rt.File)
|
|
}
|
|
for _, p := range dy.Proxies {
|
|
out = append(out, p.File)
|
|
}
|
|
for _, rb := range dy.RemoteBrowsers {
|
|
out = append(out, rb.File)
|
|
}
|
|
for _, d := range dy.Domains {
|
|
out = append(out, d.Content, d.NotFound)
|
|
}
|
|
return out
|
|
}
|