Skip to content

Commit

Permalink
escape special characters in onlyStoryFiles filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanKolnik committed Mar 7, 2024
1 parent 03d2f43 commit b5da952
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
21 changes: 21 additions & 0 deletions node-src/tasks/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ describe('traceChangedFiles', () => {
expect(ctx.onlyStoryFiles).toStrictEqual(Object.keys(deps));
});

it('escapes special characters on context', async () => {
const deps = { './example-(new).stories.js': ['./example-(new).stories.js'] };
findChangedDependencies.mockResolvedValue([]);
findChangedPackageFiles.mockResolvedValue([]);
getDependentStoryFiles.mockResolvedValue(deps);

const ctx = {
env,
log,
http,
options: {},
sourceDir: '/static/',
fileInfo: { statsPath: '/static/preview-stats.json' },
git: { changedFiles: ['./example.js'] },
turboSnap: {},
} as any;
await traceChangedFiles(ctx, {} as any);

expect(ctx.onlyStoryFiles).toStrictEqual(["./example-\\(\\new\\)\\.stories.js"]);
});

it('does not run package dependency analysis if there are no metadata changes', async () => {
const deps = { 123: ['./example.stories.js'] };
getDependentStoryFiles.mockResolvedValue(deps);
Expand Down
7 changes: 6 additions & 1 deletion node-src/tasks/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ interface PathSpec {
contentLength: number;
}

const escapedSpecialChars = '`$^*+?()[]';
const specialCharsRegex = new RegExp(`([${escapedSpecialChars.split('').join('\\')}])`);

Check warning on line 41 in node-src/tasks/upload.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

node-src/tasks/upload.ts#L41

The `RegExp` constructor was called with a non-literal value.

// Get all paths in rootDir, starting at dirname.
// We don't want the paths to include rootDir -- so if rootDir = storybook-static,
// paths will be like iframe.html rather than storybook-static/iframe.html
Expand Down Expand Up @@ -154,7 +157,9 @@ export const traceChangedFiles = async (ctx: Context, task: Task) => {
changedDependencyNames || []
);
if (onlyStoryFiles) {
ctx.onlyStoryFiles = Object.keys(onlyStoryFiles);
// Escape special characters in the filename so it does not conflict with picomatch
ctx.onlyStoryFiles = Object.keys(onlyStoryFiles).map((key) => key.split(specialCharsRegex).join('\\'));

if (!ctx.options.interactive) {
if (!ctx.options.traceChanged) {
ctx.log.info(
Expand Down

0 comments on commit b5da952

Please sign in to comment.