Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add sanitizeJson

Steven Teyffe95dc317f6
diff --git a/apps/web/app/api/cron/import/bitly/fetch-utils.ts b/apps/web/app/api/cron/import/bitly/fetch-utils.ts
index 8d4c90759b..ce90574998 100644
--- a/apps/web/app/api/cron/import/bitly/fetch-utils.ts
+++ b/apps/web/app/api/cron/import/bitly/fetch-utils.ts
@@ -2,6 +2,8 @@
* Utilities for fetching links from Bitly API
*/
+import { sanitizeJson } from "./sanitize-json";
+
interface FetchBitlyLinksResult {
links: any[];
nextSearchAfter: string | null;
@@ -84,13 +86,15 @@ const fetchBitlyLinksStandard = async ({
};
}
+ // Get response as text first
+ const responseText = await response.text();
let data;
try {
- data = await response.json();
+ // Sanitize the JSON and then parse it
+ data = JSON.parse(sanitizeJson(responseText));
} catch (error) {
console.error("JSON parsing error:", error);
- const text = await response.clone().text();
- console.error(`Failed to parse response: ${text}`);
+ console.error(`Failed to parse response: ${responseText}`);
throw new Error("Failed to parse JSON response from Bitly API");
}
@@ -170,13 +174,15 @@ const fetchBitlyLinksBatch = async ({
}
}
+ // Get response as text first
+ const responseText = await response.text();
let data;
try {
- data = await response.json();
+ // Sanitize the JSON and then parse it
+ data = JSON.parse(sanitizeJson(responseText));
} catch (error) {
console.error("JSON parsing error:", error);
- const text = await response.clone().text();
- console.error(`Failed to parse response: ${text}`);
+ console.error(`Failed to parse response: ${responseText}`);
if (i === 0) {
throw new Error("Failed to parse JSON response from Bitly API");
diff --git a/apps/web/app/api/cron/import/bitly/sanitize-json.ts b/apps/web/app/api/cron/import/bitly/sanitize-json.ts
new file mode 100644
index 0000000000..c9dc1ef52b
--- /dev/null
+++ b/apps/web/app/api/cron/import/bitly/sanitize-json.ts
@@ -0,0 +1,21 @@
+export const sanitizeJson = (json: string): string => {
+ // Replace control characters that are not allowed in JSON string literals
+ return json.replace(/[\u0000-\u001F\u007F-\u009F]/g, (char) => {
+ // Convert to proper JSON escape sequence if it's a common one
+ switch (char) {
+ case "\n":
+ return "\\n";
+ case "\r":
+ return "\\r";
+ case "\t":
+ return "\\t";
+ case "\b":
+ return "\\b";
+ case "\f":
+ return "\\f";
+ // Remove or replace other control characters
+ default:
+ return "";
+ }
+ });
+};