Skip to content

Commit 433125d

Browse files
committed
sanitize strings
1 parent c7512c7 commit 433125d

File tree

5 files changed

+80
-10
lines changed

5 files changed

+80
-10
lines changed

packages/synthetics-sdk-broken-links/src/link_utils.ts

+38
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,44 @@ export function shuffleAndTruncate(
272272
return linksToFollow.slice(0, link_limit! - 1);
273273
}
274274

275+
/**
276+
* Sanitizes an object name string for safe use, ensuring compliance with
277+
* naming restrictions.
278+
*
279+
* @param {string} inputString - The original object name string.
280+
* @returns {string} The sanitized object name.
281+
*
282+
* **Sanitization Rules:**
283+
* * Removes control characters ([\u007F-\u009F]).
284+
* * Removes disallowed characters (#, [, ], *, ?, ", <, >, |, /).
285+
* * Replaces the forbidden prefix ".well-known/acme-challenge/" with an underscore.
286+
* * Replaces standalone occurrences of "." or ".." with an underscore.
287+
*/
288+
export function sanitizeObjectName(
289+
inputString: string | null | undefined
290+
): string {
291+
if (!inputString) return '';
292+
293+
// Regular expressions for:
294+
/*eslint no-useless-escape: "off"*/
295+
const invalidCharactersRegex = /[\r\n\u007F-\u009F#\[\]*?:"<>|/]/g; ///[\r\n\u007F-\u009F#\[\]*?:"<>|]/g; // Control characters, special characters, path separator
296+
const wellKnownPrefixRegex = /^\.well-known\/acme-challenge\//;
297+
298+
// Core sanitization:
299+
let sanitizedString = inputString
300+
.replace(wellKnownPrefixRegex, '_') // Replace forbidden prefix
301+
.replace(invalidCharactersRegex, '_') // replace invalid characters
302+
.trim() // Clean up any leading/trailing spaces
303+
.replace(/\s+/g, '_'); // Replace one or more spaces with underscores
304+
305+
// Handle "." and ".." specifically
306+
if (sanitizedString === '.' || sanitizedString === '..') {
307+
sanitizedString = '_'; // Replace with a safe character
308+
}
309+
310+
return sanitizedString;
311+
}
312+
275313
export function getTimeLimitPromise(
276314
startTime: string,
277315
totalTimeoutMillis: number,

packages/synthetics-sdk-broken-links/src/storage_func.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
getExecutionRegion,
2525
resolveProjectId,
2626
} from '@google-cloud/synthetics-sdk-api';
27+
import { sanitizeObjectName } from './link_utils';
2728

2829
// External Dependencies
2930
import { Storage, Bucket } from '@google-cloud/storage';
@@ -55,14 +56,14 @@ export async function getOrCreateStorageBucket(
5556
let bucketName = '';
5657

5758
try {
58-
const projectId = await resolveProjectId();
59-
const region = await getExecutionRegion();
60-
61-
// if storageClient was not properly initialized OR the user chose to
62-
// use/create the default bucket but we were not able to resolve projectId
63-
// or cloudRegion
64-
if (!storageClient || (!storageLocation && (!projectId || !region)))
65-
return null;
59+
if (!storageClient) return null;
60+
61+
const projectId = sanitizeObjectName(await resolveProjectId());
62+
const region = sanitizeObjectName(await getExecutionRegion());
63+
64+
// if the user chose to use/create the default bucket but we were not able
65+
// to resolve projectId or cloudRegion
66+
if (!storageLocation && (!projectId || !region)) return null;
6667

6768
bucketName = storageLocation
6869
? storageLocation.split('/')[0]

packages/synthetics-sdk-broken-links/test/integration/integration.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
require('../../test/example_html_files/integration_server.js');
3333
const { getTestServer } = require('@google-cloud/functions-framework/testing');
3434

35-
describe.only('CloudFunctionV2 Running Broken Link Synthetics', async () => {
35+
describe('CloudFunctionV2 Running Broken Link Synthetics', async () => {
3636
const status_class_2xx = {
3737
status_class: ResponseStatusCode_StatusClass.STATUS_CLASS_2XX,
3838
};

packages/synthetics-sdk-broken-links/test/unit/broken_links.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
BrokenLinkCheckerOptions,
3636
} from '../../src/broken_links';
3737

38-
describe.only('runBrokenLinks', async () => {
38+
describe('runBrokenLinks', async () => {
3939
const status_class_2xx: ResponseStatusCode = {
4040
status_class: ResponseStatusCode_StatusClass.STATUS_CLASS_2XX,
4141
};

packages/synthetics-sdk-broken-links/test/unit/link_utils.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
createSyntheticResult,
3131
getGenericSyntheticResult,
3232
LinkIntermediate,
33+
sanitizeObjectName,
3334
shuffleAndTruncate,
3435
} from '../../src/link_utils';
3536
import { setDefaultOptions } from '../../src/options_func';
@@ -210,4 +211,34 @@ describe('GCM Synthetics Broken Links Utilies', async () => {
210211
expect(startTime).to.be.lessThan(endTime);
211212
expect(milliDifference).to.be.greaterThan(0);
212213
});
214+
215+
describe('sanitizeObjectName', () => {
216+
it('should remove invalid characters', () => {
217+
const input = "test/\@#$%^&*()/_+\-=[]{};':\"\|,.<>/?\r\n\t";
218+
const expectedOutput = "test_@_$%^&_()__+-=__{};'__\_,.______";
219+
expect(sanitizeObjectName(input)).to.equal(expectedOutput);
220+
});
221+
222+
it('should replace the forbidden prefix', () => {
223+
const input = ".well-known/acme-challenge/test";
224+
const expectedOutput = "_test";
225+
expect(sanitizeObjectName(input)).to.equal(expectedOutput);
226+
});
227+
228+
it('should handle standalone "." and ".."', () => {
229+
expect(sanitizeObjectName(".")).to.equal("_");
230+
expect(sanitizeObjectName("..")).to.equal("_");
231+
});
232+
233+
it('should handle null and undefined', () => {
234+
expect(sanitizeObjectName(null)).to.equal("");
235+
expect(sanitizeObjectName(undefined)).to.equal("");
236+
})
237+
238+
it('should trim leading and trailing whitespace', () => {
239+
const input = " test name ";
240+
const expectedOutput = "test_name";
241+
expect(sanitizeObjectName(input)).to.equal(expectedOutput);
242+
});
243+
});
213244
});

0 commit comments

Comments
 (0)