Home > Enterprise >  Replacing hyphen by equal sign in Nano IDs in URLs, aka, extra equal signs in URLs - are they safe?
Replacing hyphen by equal sign in Nano IDs in URLs, aka, extra equal signs in URLs - are they safe?

Time:09-04

I am using Nano ID to generate unique strings to safely differentiate URLs automatically generated from product parameters, so where duplicates might occur. The order of parameters is strict (think "../manufacturer-model-type-whateverz-nanoid.html"), they are separated by hyphens. Nano ID uses hyphens in its alphabet/dictionary, which would break the 'hyphen logic', so I want to replace the Nano ID hyphens by equal signs. I think it is safe, as long as it does not interfere with ?key=values. So it ok or is there something else that would make me think twice? After all, equal signs could also be in the NanoID alphabet, but for some reason they aren't.

CodePudding user response:

According to RFC1738:

Scheme names consist of a sequence of characters. The lower case letters "a"--"z", digits, and the characters plus (" "), period ("."), and hyphen ("-") are allowed. For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names (e.g., allow "HTTP" as well as "http").

It furthermore states that all other characters are considered unsafe and must be encoded. So don't use an equals sign instead of the hyphen.

Instead, use a custom alphabet for Nanoid that contains only letters and digits as described in nanoid Readme

import { customAlphabet } from 'nanoid'
const nanoid = customAlphabet('1234567890abcdef', 10)    
model.id = nanoid() //=> "4f90d13a42"
  • Related