tedd / Tedd.ShortUrl

Simple URL shortening in ASP.Net 6.0 using MSSQL backend
https://go.tedd.no
GNU Lesser General Public License v2.1
5 stars 3 forks source link

Far Fewer Key Combinations Because Urls.Key Column is Case Insensitive #36

Open suntereo opened 8 months ago

suntereo commented 8 months ago

First and foremost, I would like to extend my sincere thanks and appreciation for developing the Tedd.ShortUrl package! Here's an important issue I found.

Description of the Problem

The Tedd.ShortUrl package currently defines the Urls.Key column in the database with a case-insensitive collation by default. This design choice leads to an unintended reduction in the number of unique key combinations that can be generated, contrary to the assumptions set by the configuration.

Expected vs. Actual Behavior

The appsettings.json configuration file specifies a character set that includes both lowercase and uppercase letters: "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ". This suggests an intention for the system to treat keys with different cases as unique identifiers.

Impact Analysis

Here is a comparison of the number of possible combinations when using a 5-character key under both case-sensitive and case-insensitive configurations:

This represents an approximate staggering reduction of 93% in the potential unique key space.

Real-World Implications

This reduction in key space has already manifested in practical terms, as evidenced by an issue reported here where duplicate keys were generated much sooner than statistically anticipated. This significantly increases the likelihood of key collisions, undermining the utility of the Tedd.ShortUrl service.

Workaround

To resolve this issue and align the database behavior with the intended configuration, the following SQL script changes can be applied manually after the table is created:

-- Drop the existing index if it exists
DROP INDEX IF EXISTS IX_Urls_Key ON dbo.Urls;

-- Alter the column to a case-sensitive collation
ALTER TABLE dbo.Urls
ALTER COLUMN [Key] nvarchar(10) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL;

-- Recreate the unique index with the new collation
CREATE UNIQUE NONCLUSTERED INDEX [IX_Urls_Key] ON [dbo].[Urls]
(
    [Key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW LOCKS = ON, ALLOW_PAGE LOCKS = ON) ON [PRIMARY]
GO

These modifications will ensure the creation of the Key column with the necessary case sensitivity and will allow the application's character set to be fully leveraged, producing the expected number of unique short URL keys.

Long-term Solution

A better long-term solution would be to update the Entity Framework migrations to create the Key column with a case-sensitive collation by default.

Conclusion

Addressing this issue is crucial to maintaining the integrity and reliability of the Tedd.ShortUrl service. The current limitation poses a significant risk of key collisions and reduces the effectiveness of the system. Prompt implementation of the proposed solution is highly recommended.