codidact / qpixel

Q&A-based community knowledge-sharing software
https://codidact.com
GNU Affero General Public License v3.0
393 stars 68 forks source link

Tag deletion can mangle tags_cache of associated posts if it is prefix of another tag #1178

Closed Taeir closed 1 year ago

Taeir commented 1 year ago

If one tag is a prefix to another existing tag, and both tags are present on a post, then deleting the tag will mangle the tags_cache.

For example:

Post A has Tags "hi" and "hi-there", this is saved as

---
- hi
- hi-there

Now take the following actions:

This won't actually be a problem until the post is edited, at which point it will put in the tags box the tag "there" (because this YAML isn't invalid). That is the best-case scenario, since this replacement didn't produce malformed YAML. If we had "hit" instead of "hi-there", it would possibly produce invalid yaml which cannot be decoded and crash with error 500.

I don't think any issues occur until you actually go and edit the post, since I expect the list to use the actual tags association rather than the tags_cache.

trichoplax commented 1 year ago

I'm not familiar with how the cache works. Is there the possibility of storing the tag names in quotes to avoid incomplete matches? Alternatively, could we match on whole words (which might still fail if we or any other instance ever decide to permit spaces in tags). If the tags are always stored one per line, could we match only on whole lines?

Taeir commented 1 year ago

The storage is yaml format, automatically managed by rails. I may be able to switch the replacement function to a regex replace and use word boundaries, but not sure if mysql supports those.