guncebektas / meteor-friendly-slugs

Meteor package to generate URL friendly slugs from a field with auto-incrementation to ensure unique URLs.
65 stars 15 forks source link

Slugs aren't guaranteed to be unique #3

Closed kylestew closed 9 years ago

kylestew commented 9 years ago

It's kind of an edge case, but there is a way to cause multiple slugs to be the same even when distinct is set to true.

  1. Create object with field to be slugged as test, slug will be generated as test
  2. Repeat step 1, slug will be generated as test-1
  3. Create object with field value test-1 and slug value will also be test-1 making two slugs the same in the database

It seems like we can fool the slug system if we throw a un-slugged string that matches an existing incremented slug. I'm using slugs as unique URLs, so there is the case where it will incorrectly match a URL.

Any ideas on how this could be fixed? This is the only shortcoming I see with this library, otherwise its great and I hope to continue to use it.

todda00 commented 9 years ago

Ah yes, didn't think about that case.

The most efficient way to resolve this with no extra queries would be to look for -X at the end of a potential slug where X is any number of digits. Would reduce multiple sets as well, so the following example would occur:

  1. create test, slugs as test
  2. create another test, slugs as test-1
  3. create test-1, slugs as test-2
  4. create test-1-2-3-4-5 slugs as test-3
  5. create test-1-abc, would still slug as test-1-abc

Probably less than ideal for edge cases like 3 and 4, but would guarantee distinct slugs. This is the most efficient way to handle these cases. Do you think this would work for your situation?

kylestew commented 9 years ago

Yes, this seems like it would work great. Its an edge case, so the goal is to make sure there isn't a chance for the slug to be ambiguous and cause links to not be unique. If users are doing crazy '-123' stuff with their names, they may get a weird slug link. Thats ok.

Do you see this being implemented in the library?

todda00 commented 9 years ago

Done - released 0.3.0