ngx-bootstrap icon indicating copy to clipboard operation
ngx-bootstrap copied to clipboard

Typeahead highlight displays <strong> tag when empty spaces added at the end of query and does not escape html tags

Open pouellet opened this issue 6 years ago • 10 comments

Bug description

This only happens when retrieving data asynchronously. We are using typeahead to trigger a quick search and display the three most relevant search results. We trim spaces around our search query, so a search with 'Ala ' (two trailing spaces) would return Alabama and Alaska. When this happens, the typeahead highlighting function mixes up the highlighting indexes and end up displaying stuff like trong>Alabama.

image

Similarly, if the data returned contains html, the html is not escaped. So if the backends return <script>Bermuda</script> and <i>Bern</i> when typing Be , then we end up with something like this:

image

Luckily, Angular detects the <script> tag as malicious and drops it, but it still result in messed up display and a few warnings in the console.

image

As a workaround, we are now wrapping the highlighting function with the following code:

    highlight(match: TypeaheadMatch, query: Array<String>): string {
        // Remove empty spaces from query. Entering 'Raster  ' in the search query results
        // in an array of ["Raster", " "] which messes up the highlighting.
        query = query.filter((value) => value.trim().length > 0);
        
        // Escape html from typeahead matches to avoid html injection and console warnings
        // when malicious tags get stripped out. It also messes up the highlighting.
        match = new TypeaheadMatch(match.item, escape(match.value), match.isHeader());
        return this.typeahead._container.hightlight(match, query);
    }

Plunker/StackBlitz that reproduces the issue

Extracted from the async demo, with a small change in the Regex to allow Ala to return Alabama. Also shows the issues with the html escape by typing Be. The second dropdown uses the workaround mentioned above to fix the issues.

https://stackblitz.com/edit/ngx-bootstrap-typeahead-highlight-bug

Versions of ngx-bootstrap, Angular, and Bootstrap

ngx-bootstrap: 2.0.5 Angular: 4.x, 5.x

pouellet avatar May 16 '18 22:05 pouellet

Ngx-bootstrap doesn't include this particular case. If you get an object with html tag, you should convert it by yourself. If a returned object doesn't consist string with html tag, everything will work fine

svetoldo4444ka avatar Sep 21 '18 11:09 svetoldo4444ka

Ngx-bootstrap doesn't include this particular case. If you get an object with html tag, you should convert it by yourself.

Fair enough, thanks for the update.

If a returned object doesn't consist string with html tag, everything will work fine

The first example in my bug report (typing Ala followed by two white spaces) does not return any HTML tags, it's only returning a string (e.g. 'Alabama'). You can have a look at the StackBlitz repro.

pouellet avatar Sep 21 '18 17:09 pouellet

What is the progress?

mykola-novytskyi avatar Dec 28 '21 13:12 mykola-novytskyi

We have an angular web application which uses typeahead. One of the dropdown menus is a list of antenna-radome types that have fixed length of 20 characters and consist of two parts: antenna type and radome type. As radome type is always the last 4 chars, there are multiple spaces between antenna and radome types, which causes the same problem as pouellet described above. Examples of the dropdown options:

AOA7490582.2    NONE
AOAD/M_B        DOME
AOAD/M_B        DUTD
AOAD/M_B        EMRA
AOAD/M_B        NONE
AOAD/M_TA_NGS   DOME

We currently use other third-party typeahead package which is not ideal. We are keen to use ngx-bootstrap/typeahead once this issue was fixed.

frankfu03 avatar Feb 15 '22 00:02 frankfu03

any update on this ??

UdayAppam avatar Feb 16 '22 11:02 UdayAppam

https://github.com/valor-software/ngx-bootstrap/pull/6434

Here I have tried the fix. If any suggestions am open to discuss

UdayAppam avatar Feb 17 '22 08:02 UdayAppam

@valorkin there is a PR raised to fix this issue by @UdayAppam. Any plans to take this up?

vibhus avatar May 20 '22 10:05 vibhus

@valorkin my company would also like to see this fix used. Can offer a reward.

matthewfedak avatar Oct 31 '22 12:10 matthewfedak

@valorkin my company would also like to see this fix used. Can offer a reward.

I can fix that issue, how much reward I get

ReaPer343 avatar Jun 09 '23 12:06 ReaPer343

@ReaPer343 Sorry, just seen this message. I already fixed it and have since changed companies. Thanks for the offer.

matthewfedak avatar Jul 18 '23 10:07 matthewfedak