]> git.ardis.lu - cloudflare-workers/commitdiff
Add error handling instead of using default values
authorArdis Lu <ardisglu@gmail.com>
Tue, 12 Jul 2022 04:59:59 +0000 (21:59 -0700)
committerArdis Lu <ardisglu@gmail.com>
Tue, 12 Jul 2022 04:59:59 +0000 (21:59 -0700)
hash/index.js

index de4696da34e606d3c1dd556496d54469364f6a60..78b241f5bb5c7267792a3712fcf536fa8ceb5030 100644 (file)
@@ -1,17 +1,28 @@
 export default {
   async fetch(request) {
     const url = new URL(request.url);
-    let algorithm = 'SHA-256';
-    let query = 'Example string';
+    let algorithm;
+    let query;
+
+    // Parse URLSearchParams
     for (const [key, value] of url.searchParams) {
-      if (key.toLowerCase() === 'algorithm' && ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'].includes(value.toUpperCase())) {
-        algorithm = value;
+      const normalizedKey = key.toLowerCase();
+      if ((normalizedKey === 'a' || normalizedKey === 'algorithm') && /^SHA-?(1|256|384|512)$/i.test(value)) {
+        algorithm = value.includes('-') ? value : `SHA-${value.slice(3)}`; // Insert a '-' character if it's not present; crypto.subtle.digest() requires it.
       }
-      else if ((key.toLowerCase() === 'q' || key.toLowerCase() === 'query') && value) { // Intentionally excluding falsy values and not just null because empty strings are no good
+      else if ((normalizedKey === 'q' || normalizedKey === 'query') && value) { // Intentionally fail for falsy values because a null or empty string is also invalid
         query = value;
       }
     }
 
+    // Handling bad params
+    if (algorithm === undefined) {
+      return new Response('Invalid algorithm provided. Provide one of the following values for the parameter "a" or "algorithm": SHA-1, SHA-256, SHA-384, or SHA-512.', { status: 400 });
+    }
+    else if (query === undefined) {
+      return new Response('Invalid query provided. Provide a value for the parameter "q" or "query".', { status: 400 });
+    }
+
     // Convert query string to byte array and hash it, then convert hashed byte array to hex string
     const inputArray = new TextEncoder().encode(query);
     const digest = await crypto.subtle.digest(algorithm, inputArray);
This page took 0.041175 seconds and 4 git commands to generate.