views:

77

answers:

3

I am finishing creating a file upload utility for our site, and if the upload is an invalid format (per our specs not worth going over here) I would want to delete the folder the zip file was unzipped to, and all it's contents.

So far I have used a method of creating a dynamic batch file like this:

    <!--- check if folder exists before starting to delete --->
<cfif directoryexists("#file_path_course#")>

    <!--- this can be passed in a varaible or whatever ---> 
    <cfset tDirectory = "#file_path_course#"> 

    <!--- This is what we will put in the bat file ---> 
    <cfset tString ="RMDIR /S /Q " & tDirectory> 

    <!--- generate a .BAT file for later execution ---> 
    <cffile action="WRITE" file="#file_path_course#\delete.bat" output="#tString#">

    <!--- Now execute the file to delete everything (Folder and all sub-folders and files)---> 
    <cfexecute name="#file_path_course#\delete.bat" timeout="60"></cfexecute> 

    <!--- check if bat file exists --->
    <cfif fileexists("#file_path_course#\delete.bat")>

        <!--- now delete the bat file ---> 
        <cffile action="DELETE" file="#file_path_course#\delete.bat"> 

    </cfif>

    <!--- delete course folder --->
    <cfdirectory action="delete" directory="#file_path_course#" recurse="yes">

    <cfset course_files_deleted = "Yes">

</cfif>

But I am admittedly concerned about the allowed usage of the cfexecute tag.

There is another option, which uses the cfdirectory recurse delete option, which will do all I ask, but I want to be very sure it's not going to delete the folders/files outside the folder I point it to.

There is a 3rd way, which involves a cfdirectory and looping around it, but I also like the idea of using less lines of code to do a simple operation.

Which option do you trust the most?

I am running IIS7, Coldfusion 8.

Thank You very much.

+6  A: 

Why not just use cfdirectory? You said you were worried that it would delete stuff "outside" the folder you specified. It won't. Simple as that. If it did, then the tag would be broken. :)

CF Jedi Master
Just wasn't 100% comfortable with my understanding of how far it would recurse. That's why i asked :)
crosenblum
It does exactly what i want and no more...Fantastic...I just wasn't 100% sure I could set it and forget it, like Ronco used to say :)
crosenblum
Just watch out with that directory path. If a user has any ability to modify set it then you will have to sanitize it to make sure there isn't any sort of ../../windows/system32 in there =]
Tyler Clendenin
+1  A: 

Instead of writing a batch file and then executing it, I let Cold Fusion do all the work.

<cfset targetDirectory = "C:\Websites\site\thisFolder" />
<cfif directoryExists(targetDirectory)>
<cfdirectory action="list" directory="#targetDirectory#" listInfo="" name="theseFiles" recurse="true" type="file" />
    <cfif theseFiles.recordcount gt 0>
    <cfloop query="theseFiles">
        <cffile action="delete" file="#targetDirectory#/#theseFiles.name#" />
    </cfloop>
    </cfif>
<cfdirectory action="delete" directory="#uploadDirectory#/#allFolders.name#" />
</cfif>
RKolosky
You don't need to check the RecordCount - if it's zero the cfloop will just get skipped past anyway.
Peter Boughton
Thank you for your time.
crosenblum
+2  A: 

what i would do is upload the file to a temp directory outside of the webroot. you can use gettempdirectory() to accomplish this which uses your system's temp directory (c:\windows\temp for windows)

then you can unzip the file into a subdirectory off of the temp directory and perform some security checks against the unzipped files and make sure everything is ok, all the while not opening up your site to any attacks. if everything pans out, you can then move the files to their final resting place. if not, just use cfdirectory (as cfjedimaster pointed out) to remove the subdirectory and all the files.

rip747
Smart thinking....well thought out, thank you.
crosenblum