]> git.ardis.lu - alu.bz/commitdiff
Fix broken shortUrl validation regex
authorArdis Lu <ardisglu@gmail.com>
Fri, 6 Dec 2024 05:14:34 +0000 (21:14 -0800)
committerArdis Lu <ardisglu@gmail.com>
Fri, 6 Dec 2024 05:14:34 +0000 (21:14 -0800)
Hyphen needs to be escaped to "\-", and alphanumeric can be simplified to "\w".

functions/api.js
index.html

index 4e907698741f2b4caf6634db1f19a1511c6e101c..6ff41f8fad78ff9e2a2372ceb53e5ab4397d47ec 100644 (file)
@@ -51,7 +51,7 @@ export async function onRequestPost({ request, env }) {
     } while (await env.links.get(shortUrl) !== null); // Confirm it's not already taken
   }
   // Invalid short URLs
-  else if (!/^[0-9a-zA-Z-_]*$/g.test(shortUrl) || shortUrl === 'api') {
+  else if (!/^[\w\-]+$/g.test(shortUrl) || shortUrl === 'api') {
     return new Response('Invalid short URL.', { status: 400, headers: { 'Content-Type': 'text/plain; charset=UTF-8' } });
   }
   // User-provided duplicates
index 10c1cb638b6b1cdbe4c1719a7e929a4789aa2f9f..42695ca0b9e68578800771b1d7cae95277991cd7 100644 (file)
@@ -52,7 +52,7 @@
         <label for="short-url">Custom short path</label>
         <aside id="short-url-aside">Connect wallet to enable this feature.</aside>
         <!-- Maximum 80 characters, valid characters are alphanumeric, hyphen (-), and underscore (_) -->
-        <input type="text" id="short-url" name="short-url" placeholder="example" maxlength="80" pattern="^[0-9a-zA-Z-_]*$" disabled>
+        <input type="text" id="short-url" name="short-url" placeholder="example" maxlength="80" pattern="[\w\-]+" disabled>
       </div>
 
       <button type="submit" id="generate" class="text-button">Create short link</button>
This page took 0.042933 seconds and 4 git commands to generate.