When it comes to scanning files like PDFs and images for potential XSS vulnerabilities in Go, the approach typically involves parsing the files to extract embedded content (e.g., scripts, metadata) and checking for malicious patterns. Below are recommended Go libraries and strategies for each file type:
1. PDF Scanning
PDFs can contain JavaScript, forms, or embedded HTML that might lead to XSS if rendered unsafely. Use these libraries to parse and analyze PDF content:
Libraries
-
github.com/ledongthuc/pdf
A lightweight PDF parser for extracting text and basic structure. Use it to scan for suspicious keywords (e.g., <script>, javascript:).
import "github.com/ledongthuc/pdf"
-
github.com/jung-kurt/gofpdf (with extensions)
Primarily for PDF generation, but can be extended to parse existing PDFs and check metadata/annotations.
import "github.com/jung-kurt/gofpdf"
-
github.com/rsc/pdf
A more advanced PDF parser for deeper inspection of PDF internals (e.g., JavaScript actions).
import "github.com/rsc/pdf"
Strategy
- Extract text and embedded elements from the PDF.
- Check for patterns like
<script>, eval(, or event handlers (e.g., onmouseover).
- Use regex or HTML parsers (e.g.,
golang.org/x/net/html) to validate extracted content.
2. Image Scanning
Images (e.g., SVG, PNG, JPEG) can be vectors if they contain embedded scripts (common in SVG) or malicious metadata.
Libraries
-
SVG Parsing
SVG files are XML-based and can include JavaScript. Use these libraries:
-
github.com/srwiley/oksvg
Parse SVG files and traverse elements to detect <script>, onload, or href attributes with javascript:.
import "github.com/srwiley/oksvg"
-
github.com/ajstarks/svgo
Generate/parse SVG and inspect elements programmatically.
import "github.com/ajstarks/svgo"
-
Metadata Extraction
For non-SVG images (PNG, JPEG), check metadata like EXIF:
Strategy
-
Verify the file is truly an image (not a disguised HTML/SVG file) using:
- github.com/h2non/filetype (file type detection).
import "github.com/h2non/filetype"
-
For SVG:
- Parse with
oksvg and check for <script> tags or unsafe attributes.
-
For other formats:
- Validate metadata and ensure filenames/paths are sanitized.
3. General XSS Pattern Detection
Use HTML parsers to analyze extracted content for XSS patterns:
-
golang.org/x/net/html
Parse HTML fragments and check for unsafe elements/attributes.
import "golang.org/x/net/html"
-
github.com/microcosm-cc/bluemonday
A strict HTML sanitizer that can help identify risky content.
import "github.com/microcosm-cc/bluemonday"
Example Workflow
-
File Type Detection
Use filetype to confirm the file is a PDF, SVG, etc.
-
Content Extraction
- For PDFs: Extract text with
ledongthuc/pdf.
- For SVG: Parse with
oksvg and traverse nodes.
- For images: Check metadata with
go-exif.
-
Pattern Matching
Use regex or HTML parsers to detect:
<script>...</script>
- Event attributes (e.g.,
onerror, onload)
javascript: URIs.
Limitations
- PDF JavaScript Detection: Go libraries may not fully parse PDF JavaScript. Consider combining with tools like
pdfid (Python) via exec calls if necessary.
- Complex Files: Embedded objects (e.g., Flash in PDFs) may require deeper analysis.
- False Positives: Regex-based checks can miss context (e.g., scripts in comments).
For production-grade security, pair these libraries with existing tools (e.g., OWASP ZAP, ClamAV) and manual code reviews.