| 
 | 1 | +Your competitor is finally fed up with the lackluster results from the UC Berkeley graduate and decide to hire a student who has completed CS 253: Web Security. They are able to quickly implement a foolproof `htmlElementEscape()` function which defeats your shenanigans once and for all.  | 
1 | 2 | 
 
  | 
 | 3 | +Please read the code for `htmlElementEscape()` and ensure you understand how it works in detail.  | 
 | 4 | + | 
 | 5 | +```js  | 
 | 6 | +function htmlElementEscape (str) {  | 
 | 7 | + return str  | 
 | 8 | + // Without the '<' character, no HTML tags an be created.  | 
 | 9 | + .replace(/</g, '<')  | 
 | 10 | + | 
 | 11 | + // This is not for security, but because '&' is the HTML escape character  | 
 | 12 | + // and we don't want the user's input to be treated as an escape sequence.  | 
 | 13 | + .replace(/&/g, '&')  | 
 | 14 | +}  | 
 | 15 | +```  | 
 | 16 | + | 
 | 17 | +Now all your competitor needs to do is call this function whenever they put untrusted data directly into the HTML body somewhere. This includes inside normal tags like `div`, `p`, `b`, `td`, etc.  | 
 | 18 | + | 
 | 19 | +So, their updated route handler code probably looks something like this now:  | 
 | 20 | + | 
 | 21 | +```js  | 
 | 22 | +router.get('/search', async (req, res) => {  | 
 | 23 | + let q = req.query.q  | 
 | 24 | + if (q == null) q = ''  | 
 | 25 | + | 
 | 26 | + q = htmlElementEscape(q)  | 
 | 27 | + | 
 | 28 | + const results = await getResults(q)  | 
 | 29 | + res.render('hackoogle-search-page', { q, results })  | 
 | 30 | +})  | 
 | 31 | +```  | 
 | 32 | + | 
 | 33 | +Dang, it seems we're out of luck.  | 
2 | 34 | 
 
  | 
3 | 35 | <iframe src='http://localhost:4090'></iframe>  | 
4 | 36 | 
 
  | 
 | 
0 commit comments