|
| 1 | +package sanitize |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + |
| 9 | + "github.com/pangeacyber/pangea-go/pangea-sdk/v3/internal/request" |
| 10 | + "github.com/pangeacyber/pangea-go/pangea-sdk/v3/pangea" |
| 11 | +) |
| 12 | + |
| 13 | +func (e *sanitize) Sanitize(ctx context.Context, input *SanitizeRequest, file io.ReadSeeker) (*pangea.PangeaResponse[SanitizeResult], error) { |
| 14 | + if input == nil { |
| 15 | + return nil, errors.New("nil input") |
| 16 | + } |
| 17 | + |
| 18 | + if input.TransferMethod == pangea.TMpostURL { |
| 19 | + params, err := pangea.GetUploadFileParams(file) |
| 20 | + if err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + input.SHA256 = params.SHA256 |
| 24 | + input.CRC32C = params.CRC32C |
| 25 | + input.Size = pangea.Int(params.Size) |
| 26 | + } |
| 27 | + |
| 28 | + name := "file" |
| 29 | + if input.TransferMethod == pangea.TMmultipart { |
| 30 | + name = "upload" |
| 31 | + } |
| 32 | + |
| 33 | + fd := pangea.FileData{ |
| 34 | + File: file, |
| 35 | + Name: name, |
| 36 | + } |
| 37 | + |
| 38 | + return request.DoPostWithFile(ctx, e.Client, "v1beta/sanitize", input, &SanitizeResult{}, fd) |
| 39 | +} |
| 40 | + |
| 41 | +func (e *sanitize) RequestUploadURL(ctx context.Context, input *SanitizeRequest) (*pangea.PangeaResponse[SanitizeResult], error) { |
| 42 | + if input.TransferMethod == pangea.TMmultipart || input.TransferMethod == pangea.TMdestURL || input.TransferMethod == pangea.TMsourceURL { |
| 43 | + return nil, fmt.Errorf("transfer method [%s] is not supported in RequestUploadURL. Use Sanitize() method instead.", input.TransferMethod) |
| 44 | + } |
| 45 | + |
| 46 | + if input.TransferMethod == pangea.TMpostURL && (input.SHA256 == "" || input.CRC32C == "" || input.Size == nil) { |
| 47 | + return nil, errors.New("Need to set SHA256, CRC32C and Size in order to use TMpostURL") |
| 48 | + } |
| 49 | + |
| 50 | + return request.GetUploadURL(ctx, e.Client, "v1beta/sanitize", input, &SanitizeResult{}) |
| 51 | +} |
| 52 | + |
| 53 | +// SanitizeFile represents the SanitizeFile API request model. |
| 54 | +type SanitizeFile struct { |
| 55 | + ScanProvider string `json:"scan_provider,omitempty"` |
| 56 | +} |
| 57 | + |
| 58 | +// SanitizeContent represents the SanitizeContent API request model. |
| 59 | +type SanitizeContent struct { |
| 60 | + URLIntel *bool `json:"url_intel,omitempty"` |
| 61 | + URLIntelProvider string `json:"url_intel_provider,omitempty"` |
| 62 | + DomainIntel *bool `json:"domain_intel,omitempty"` |
| 63 | + DomainIntelProvider string `json:"domain_intel_provider,omitempty"` |
| 64 | + Defang *bool `json:"defang,omitempty"` |
| 65 | + DefangThreshold *int `json:"defang_threshold,omitempty"` |
| 66 | + Redact *bool `json:"redact,omitempty"` |
| 67 | + RemoveAttachments *bool `json:"remove_attachments,omitempty"` |
| 68 | + RemoveInteractive *bool `json:"remove_interactive,omitempty"` |
| 69 | +} |
| 70 | + |
| 71 | +// SanitizeShareOutput represents the SanitizeShareOutput API request model. |
| 72 | +type SanitizeShareOutput struct { |
| 73 | + Enabled *bool `json:"enabled,omitempty"` |
| 74 | + OutputFolder string `json:"output_folder,omitempty"` |
| 75 | +} |
| 76 | + |
| 77 | +// SanitizeRequest represents the SanitizeRequest API request model. |
| 78 | +type SanitizeRequest struct { |
| 79 | + pangea.BaseRequest |
| 80 | + pangea.TransferRequest |
| 81 | + |
| 82 | + SourceURL string `json:"source_url,omitempty"` |
| 83 | + ShareID string `json:"share_id,omitempty"` |
| 84 | + File *SanitizeFile `json:"file,omitempty"` |
| 85 | + Content *SanitizeContent `json:"content,omitempty"` |
| 86 | + ShareOutput *SanitizeShareOutput `json:"share_output,omitempty"` |
| 87 | + Size *int `json:"size,omitempty"` |
| 88 | + CRC32C string `json:"crc32c,omitempty"` |
| 89 | + SHA256 string `json:"sha256,omitempty"` |
| 90 | + UploadedFileName string `json:"uploaded_file_name,omitempty"` |
| 91 | +} |
| 92 | + |
| 93 | +// DefangData represents the DefangData PangeaResponseResult. |
| 94 | +type DefangData struct { |
| 95 | + ExternalURLsCount int `json:"external_urls_count"` |
| 96 | + ExternalDomainsCount int `json:"external_domains_count"` |
| 97 | + DefangedCount int `json:"defanged_count"` |
| 98 | + URLIntelSummary string `json:"url_intel_summary"` |
| 99 | + DomainIntelSummary string `json:"domain_intel_summary"` |
| 100 | +} |
| 101 | + |
| 102 | +// RedactData represents the RedactData PangeaResponseResult. |
| 103 | +type RedactData struct { |
| 104 | + RedactionCount int `json:"redaction_count"` |
| 105 | + SummaryCounts map[string]int `json:"summary_counts"` |
| 106 | +} |
| 107 | + |
| 108 | +// CDR represents the CDR PangeaResponseResult. |
| 109 | +type CDR struct { |
| 110 | + FileAttachmentsRemoved int `json:"file_attachments_removed"` |
| 111 | + InteractiveContentsRemoved int `json:"interactive_contents_removed"` |
| 112 | +} |
| 113 | + |
| 114 | +// SanitizeData represents the SanitizeData PangeaResponseResult. |
| 115 | +type SanitizeData struct { |
| 116 | + Defang *DefangData `json:"defang,omitempty"` |
| 117 | + Redact *RedactData `json:"redact,omitempty"` |
| 118 | + MaliciousFile bool `json:"malicious_file"` |
| 119 | + CDR *CDR `json:"cdr,omitempty"` |
| 120 | +} |
| 121 | + |
| 122 | +// SanitizeResult represents the SanitizeResult PangeaResponseResult. |
| 123 | +type SanitizeResult struct { |
| 124 | + DestURL *string `json:"dest_url,omitempty"` |
| 125 | + DestShareID *string `json:"dest_share_id,omitempty"` |
| 126 | + Data SanitizeData `json:"data"` |
| 127 | + Parameters map[string]interface{} `json:"parameters,omitempty"` |
| 128 | +} |
0 commit comments