-
Notifications
You must be signed in to change notification settings - Fork 452
Add random suffix when writing diagnostics to avoid filename collisions #3852
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
Changes from 3 commits
cdb655d
e73c940
c109008
245f682
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,10 +167,21 @@ function writeDiagnostic( | |
| // Create the directory if it doesn't exist yet. | ||
| mkdirSync(diagnosticsPath, { recursive: true }); | ||
|
|
||
| // Include a random suffix to avoid filename collisions between diagnostics | ||
| // produced within the same millisecond. This doesn't need to be | ||
| // cryptographically secure, so `Math.random` is fine. | ||
| const uniqueSuffix = Math.floor(Math.random() * 0x100000000) | ||
| .toString(16) | ||
| .padStart(8, "0"); | ||
| // We should only need to remove colons, but to be defensive, only allow a restricted set of | ||
| // characters. | ||
| const sanitizedTimestamp = diagnostic.timestamp.replace( | ||
| /[^a-zA-Z0-9.-]/g, | ||
| "", | ||
| ); | ||
| const jsonPath = path.resolve( | ||
| diagnosticsPath, | ||
| // Remove colons from the timestamp as these are not allowed in Windows filenames. | ||
| `codeql-action-${diagnostic.timestamp.replaceAll(":", "")}.json`, | ||
| `codeql-action-${sanitizedTimestamp}-${uniqueSuffix}.json`, | ||
| ); | ||
|
|
||
| writeFileSync(jsonPath, JSON.stringify(diagnostic)); | ||
|
Comment on lines
186
to
191
|
||
|
|
||
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.
I wonder if, given the relatively low volume of diagnostics we expect the Action to produce, it would be a better approach to just have a global counter that's incremented with each diagnostic that's written, and append that? That way, we'd avoid the very unlikely, but possible case where the random numbers clash (as Copilot notes).
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.
Agreed, my first approach was to avoid global state but everything is running on the same thread so there isn't a risk of race conditions.