Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

escape special characters in onlyStoryFiles filenames #942

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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.
JonathanKolnik marked this conversation as resolved.
Show resolved Hide resolved

// 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 @@
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