SVG
image/svg+xml
Magic Bytes
Offset: 0
3C 73 76 67
Scalable Vector Graphics (SVG) is an XML-based vector image format developed and maintained by the World Wide Web Consortium (W3C) for describing two-dimensional graphics. It is primarily utilized for web design, iconography, and responsive illustrations because it maintains clarity at any resolution or scale. Since SVG is text-based, it can include embedded scripts and styles; therefore, sanitization is recommended to mitigate potential cross-site scripting (XSS) vulnerabilities when rendering untrusted content.
Validation Code
How to validate .svg files in Python
Python
def is_svg(file_path: str) -> bool:
"""Check if file is a valid SVG by magic bytes."""
signature = bytes([0x3C, 0x73, 0x76, 0x67])
with open(file_path, "rb") as f:
return f.read(4) == signature
How to validate .svg files in Node.js
Node.js
function isSVG(buffer: Buffer): boolean {
const signature = Buffer.from([0x3C, 0x73, 0x76, 0x67]);
return buffer.subarray(0, 4).equals(signature);
}
Go
func IsSVG(data []byte) bool {
signature := []byte{0x3C, 0x73, 0x76, 0x67}
if len(data) < 4 {
return false
}
return bytes.Equal(data[:4], signature)
}
API Endpoint
GET
/api/v1/svg
curl https://filesignature.org/api/v1/svg