I've been looking into using a library such as SVGO to be able to clean user submitted SVG code on the front end. SVGO is a node.js based library which typically works on the back end, so I've been trying to wrap my head around how to go about sending SVG code from the front end to the back end and then have the cleaned code regurgitated on the front end.
It's when I was trying to figure this out that I checked their web app example, which—upon inspection, runs code inside linked scripts that I would typically see on the back end on the front end. In particular, many of their functions have the signature (full script):
1: [function(require, module, exports) {
"use strict";
var loadScripts = require("./load-scripts"),
...
module.exports = exportedFunction;
}]
Pretty confusing, as this is typically JS that I've associated with the backend, particularly the require
, module.exports
syntaxes to name a few.
Question
Thanks for any insight. I'm trying to make sure I build my project in the way that makes most sense/up to web standards.
Answers:
They are using module loaders like browserify or requirejs to allow the use of commonjs modules in the browser. It doesn't mean all the library would work on the client side, using node io modules would not work for example.
It can be useful to make some cleanup on the browser side before sending (saving a few kBytes). Browser has the advantage to be free, you don't pay for hosting the code that run on the client side, some optionnal sanitization can be run on the browser. But: see 3.
Even if it is easy to make client-side sanitization, you should always check and sanitize user's inputs on the backend, especially .svg
files as they can contain <script>
tags that might allow XSS .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With