-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Fix artifact staging filenames on Windows #39363
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -510,8 +510,10 @@ private String createFilename(String environment, RunnerApi.ArtifactInformation | |
| // all path separators. | ||
| List<String> components = Splitter.onPattern("[^A-Za-z-_.]]").splitToList(path); | ||
| String base = components.get(components.size() - 1); | ||
| String sanitizedEnvironment = environment.replaceAll("[<>:\"/\\\\|?*]", "_"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The splitter pattern has a stray That branch is taken when Would it make sense to fix the pattern to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: consider an allowlist such as
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand non-ascii unicode file names are acceptible?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you're right. Windows accepts Unicode filenames, so the ASCII-only allowlist would unnecessarily replace valid non-ASCII characters and is probably too restrictive for this fix. The narrower denylist makes more sense here. If covering control characters is worthwhile, they could instead be added explicitly, for example [\x00-\x1F<>:"/\|?*], while preserving Unicode. Thanks for pointing that out.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for tracing this. Good catch on the splitter pattern and I agree that an ASCII-only allowlist would be unnecessarily restrictive for valid Unicode filenames. I'll update the fix to preserve Unicode while sanitizing Windows-invalid characters, and I'll also take another look at the splitter issue so that both the environment-derived and artifact-derived parts of the generated filename are safe. I'll add/update the regression tests accordingly. |
||
| return clip( | ||
| String.format("%s-%s-%s", idGenerator.getId(), clip(environment, 25), base), 100); | ||
| String.format("%s-%s-%s", idGenerator.getId(), clip(sanitizedEnvironment, 25), base), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: a trailing |
||
| 100); | ||
| } | ||
|
|
||
| private String clip(String s, int maxLength) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,6 +166,27 @@ public void testStageArtifacts() throws InterruptedException, ExecutionException | |
| checkArtifacts(contentsList, staged.get("env2")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStageArtifactsWithInvalidFilenameCharacters() | ||
| throws InterruptedException, ExecutionException { | ||
| String environment = "0:ref_Environment_default"; | ||
| List<String> contentsList = ImmutableList.of("artifact-content"); | ||
|
|
||
| stagingService.registerJob( | ||
| "stagingToken", | ||
| ImmutableMap.of( | ||
| environment, | ||
| Lists.transform(contentsList, FakeArtifactRetrievalService::resolvedArtifact))); | ||
|
|
||
| ArtifactStagingService.offer(new FakeArtifactRetrievalService(), stagingStub, "stagingToken"); | ||
|
|
||
| Map<String, List<RunnerApi.ArtifactInformation>> staged = | ||
| stagingService.getStagedArtifacts("stagingToken"); | ||
|
|
||
| assertEquals(1, staged.size()); | ||
| checkArtifacts(contentsList, staged.get(environment)); | ||
|
Comment on lines
+186
to
+187
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
| } | ||
|
|
||
| private void checkArtifacts( | ||
| List<String> expectedContents, List<RunnerApi.ArtifactInformation> staged) { | ||
| assertEquals( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid compiling the regular expression pattern on every invocation of
createFilename, it is highly recommended to precompile the pattern as aprivate static final Patternconstant at the class level.For example: