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

Fix insecure temporary file creation #429

Conversation

SandstromErik
Copy link
Contributor

Fix insecure temporary file creation when setting up arguments for code gen process

Copy link
Member

@Daniel-Svensson Daniel-Svensson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this mostly solves the sonarqube issue, but it can be made better

"Issues"

  • There is a race condition between File.Exists and when the file is created
  • minor: 9 attempts 1..9, is it intentional or was it intended to be 10 ?

Improvements

Avoid race condition

  1. Exlicitly create the file with
    - FileMode.CreateNew (this checks that the file did not exist before so File.Exists becomes redundant)
    - FileAccess.Write
    - FileShare.Read (this allows the file to be "locked", preventing it from beeing modified or deleted while in use)
  • use try/catch to handle IOException in case file exists (and retry with a new name)
  1. And make sure that it remains open untill after the process is completed.

Other things

  • add "openria-codegen-{DateTime.Now:MMdd-hhmmss-fff}" or similar as part of filename
    • The random part of filename will maybe only be needed if first attempt fails
  • The code to create the temp file could return something which is IDisposable
    • but it might very well be more complex than needed
    • that means an using can be used instead of try/catch/finally
      to close and delete the file
    • If using a new class make it sealed and add a method such as "KeepFileOnDispose" for disabling deletion of file
  • Consider moving logic for creating temporary file to a helper method ? (this can make code flow easier

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

75.0% 75.0% Coverage
0.0% 0.0% Duplication

Copy link
Member

@Daniel-Svensson Daniel-Svensson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution is fine.

I would have made it a bit different and kept the file open until after the process has completed.

I would have made a "FileStream CreteTemporaryFile() method similar to below psuedo code"
and called it from GenerateClientProxiesOutOfProcess. Keeping that code separate (even as a local method) means it is quite simple to add up to X retries

name = Path.Combine (tempfolder ,"openria....")
extentions = "rsp" // tmp , txt or similar

   try
      return new FileStream(fname + ext, CreateNew, Write, FileShare.Read);
  catch IOException
       return new FileStream(fname + random + ext, CreateNew, Write, FileShare.Read);

update
As for your error handling question:

  • it would be nice to log an error with details about that we "could not create temporary file with codegen input", but the risk is very low (full file system etc) so maybe the generic exception message is god enough.

To log the exception one could do
something similar to try/catch in GenerateClientProxiesOutOfProcess

Or have create temp file log extra details

for I in 0..9
   Try return new FileStream
   Catch update random part

Report error and return failure using null, false or throw 

@Daniel-Svensson Daniel-Svensson merged commit 286c839 into OpenRIAServices:main Aug 1, 2023
@SandstromErik SandstromErik deleted the feature/fixInsecurityWithGetTempFileName branch October 2, 2023 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants