@@ -194,6 +194,19 @@ const initImageLazyLoading = () => {
194
194
}
195
195
} ;
196
196
197
+ const escapeHTMLAttribute = ( str ) => {
198
+ const map = {
199
+ "&" : "&" ,
200
+ "<" : "<" ,
201
+ ">" : ">" ,
202
+ '"' : """ ,
203
+ } ;
204
+
205
+ if ( typeof str !== "string" ) return "" ;
206
+
207
+ return str . replace ( / [ & < > " ] / g, ( char ) => map [ char ] ) ;
208
+ } ;
209
+
197
210
const populateLibraryList = ( filterQuery = "" ) => {
198
211
const items = [
199
212
...document . getElementById ( "template" ) . parentNode . children ,
@@ -218,11 +231,15 @@ const populateLibraryList = (filterQuery = "") => {
218
231
}
219
232
const template = document . getElementById ( "template" ) ;
220
233
const searchParams = new URLSearchParams ( location . search ) ;
221
- const referrer = searchParams . get ( "referrer" ) || "https://excalidraw.com" ;
234
+ const referrer = escapeHTMLAttribute (
235
+ searchParams . get ( "referrer" ) || "https://excalidraw.com" ,
236
+ ) ;
222
237
const appName = getAppName ( referrer ) ;
223
- const target = decodeURIComponent ( searchParams . get ( "target" ) || "_blank" ) ;
238
+ const target = decodeURIComponent (
239
+ escapeHTMLAttribute ( searchParams . get ( "target" ) ) || "_blank" ,
240
+ ) ;
224
241
const useHash = searchParams . get ( "useHash" ) ;
225
- const csrfToken = searchParams . get ( "token" ) ;
242
+ const csrfToken = escapeHTMLAttribute ( searchParams . get ( "token" ) ) ;
226
243
for ( let library of libraries ) {
227
244
const div = document . createElement ( "div" ) ;
228
245
div . classList . add ( "library" ) ;
@@ -265,7 +282,9 @@ const populateLibraryList = (filterQuery = "") => {
265
282
inner = inner . replace ( '<p class="updated">Updated: {updated}</p>' , "" ) ;
266
283
}
267
284
inner = inner . replace ( / \{ a p p N a m e \} / g, appName ) ;
268
- const libraryUrl = encodeURIComponent ( `${ location . origin } /${ source } ` ) ;
285
+ const libraryUrl = encodeURIComponent (
286
+ `${ escapeHTMLAttribute ( origin ) } /${ source } ` ,
287
+ ) ;
269
288
inner = inner . replace (
270
289
"{addToLib}" ,
271
290
`${ referrer } ${ useHash ? "#" : "?" } addLibrary=${ libraryUrl } ${
0 commit comments