Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 0fe8911

Browse files
committed
feat(server): lint for trailing slashes in sync URL and extra slashes in customRequestHandler
1 parent 2c87721 commit 0fe8911

3 files changed

Lines changed: 103 additions & 10 deletions

File tree

apps/server/src/routes/custom.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cls from "../services/cls.js";
55
import sql from "../services/sql.js";
66
import becca from "../becca/becca.js";
77
import type { Request, Response, Router } from "express";
8-
import { safeExtractMessageAndStackFromError } from "../services/utils.js";
8+
import { safeExtractMessageAndStackFromError, normalizeCustomHandlerPattern } from "../services/utils.js";
99

1010
function handleRequest(req: Request, res: Response) {
1111

@@ -38,15 +38,23 @@ function handleRequest(req: Request, res: Response) {
3838
continue;
3939
}
4040

41-
const regex = new RegExp(`^${attr.value}$`);
42-
let match;
41+
// Get normalized patterns to handle both trailing slash cases
42+
const patterns = normalizeCustomHandlerPattern(attr.value);
43+
let match = null;
4344

44-
try {
45-
match = path.match(regex);
46-
} catch (e: unknown) {
47-
const [errMessage, errStack] = safeExtractMessageAndStackFromError(e);
48-
log.error(`Testing path for label '${attr.attributeId}', regex '${attr.value}' failed with error: ${errMessage}, stack: ${errStack}`);
49-
continue;
45+
// Try each pattern until we find a match
46+
for (const pattern of patterns) {
47+
try {
48+
const regex = new RegExp(`^${pattern}$`);
49+
match = path.match(regex);
50+
if (match) {
51+
break; // Found a match, exit pattern loop
52+
}
53+
} catch (e: unknown) {
54+
const [errMessage, errStack] = safeExtractMessageAndStackFromError(e);
55+
log.error(`Testing path for label '${attr.attributeId}', regex '${pattern}' failed with error: ${errMessage}, stack: ${errStack}`);
56+
continue;
57+
}
5058
}
5159

5260
if (!match) {

apps/server/src/services/sync_options.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import optionService from "./options.js";
44
import config from "./config.js";
5+
import { normalizeUrl } from "./utils.js";
56

67
/*
78
* Primary configuration for sync is in the options (document), but we allow to override
@@ -17,7 +18,10 @@ function get(name: keyof typeof config.Sync) {
1718
export default {
1819
// env variable is the easiest way to guarantee we won't overwrite prod data during development
1920
// after copying prod document/data directory
20-
getSyncServerHost: () => get("syncServerHost"),
21+
getSyncServerHost: () => {
22+
const host = get("syncServerHost");
23+
return host ? normalizeUrl(host) : host;
24+
},
2125
isSyncSetup: () => {
2226
const syncServerHost = get("syncServerHost");
2327

apps/server/src/services/utils.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,85 @@ export function safeExtractMessageAndStackFromError(err: unknown): [errMessage:
375375
return (err instanceof Error) ? [err.message, err.stack] as const : ["Unknown Error", undefined] as const;
376376
}
377377

378+
/**
379+
* Normalizes URL by removing trailing slashes and fixing double slashes.
380+
* Preserves the protocol (http://, https://) but removes trailing slashes from the rest.
381+
*
382+
* @param url The URL to normalize
383+
* @returns The normalized URL without trailing slashes
384+
*/
385+
export function normalizeUrl(url: string): string {
386+
if (!url || typeof url !== 'string') {
387+
return url;
388+
}
389+
390+
// Trim whitespace
391+
url = url.trim();
392+
393+
if (!url) {
394+
return url;
395+
}
396+
397+
// Remove trailing slash, but preserve protocol
398+
if (url.endsWith('/') && !url.match(/^https?:\/\/$/)) {
399+
url = url.slice(0, -1);
400+
}
401+
402+
// Fix double slashes (except in protocol)
403+
url = url.replace(/([^:]\/)\/+/g, '$1');
404+
405+
return url;
406+
}
407+
408+
/**
409+
* Normalizes a path pattern for custom request handlers.
410+
* Ensures both trailing slash and non-trailing slash versions are handled.
411+
*
412+
* @param pattern The original pattern from customRequestHandler attribute
413+
* @returns An array of patterns to match both with and without trailing slash
414+
*/
415+
export function normalizeCustomHandlerPattern(pattern: string): string[] {
416+
if (!pattern || typeof pattern !== 'string') {
417+
return [pattern];
418+
}
419+
420+
pattern = pattern.trim();
421+
422+
if (!pattern) {
423+
return [pattern];
424+
}
425+
426+
// If pattern already ends with optional trailing slash, return as-is
427+
if (pattern.endsWith('/?$') || pattern.endsWith('/?)')) {
428+
return [pattern];
429+
}
430+
431+
// If pattern ends with $, handle it specially
432+
if (pattern.endsWith('$')) {
433+
const basePattern = pattern.slice(0, -1);
434+
435+
// If already ends with slash, create both versions
436+
if (basePattern.endsWith('/')) {
437+
const withoutSlash = basePattern.slice(0, -1) + '$';
438+
const withSlash = pattern;
439+
return [withoutSlash, withSlash];
440+
} else {
441+
// Add optional trailing slash
442+
const withSlash = basePattern + '/?$';
443+
return [withSlash];
444+
}
445+
}
446+
447+
// For patterns without $, add both versions
448+
if (pattern.endsWith('/')) {
449+
const withoutSlash = pattern.slice(0, -1);
450+
return [withoutSlash, pattern];
451+
} else {
452+
const withSlash = pattern + '/';
453+
return [pattern, withSlash];
454+
}
455+
}
456+
378457

379458
export default {
380459
compareVersions,
@@ -400,6 +479,8 @@ export default {
400479
md5,
401480
newEntityId,
402481
normalize,
482+
normalizeCustomHandlerPattern,
483+
normalizeUrl,
403484
quoteRegex,
404485
randomSecureToken,
405486
randomString,

0 commit comments

Comments
 (0)