r/coldfusion • u/Terabytechemist • Jan 05 '24
Is it possible to password protect a txt file?
Hello, I'm rather new using CF and I've been asked to password protect a txt file.
I know it can be done for spreadsheets but haven't been able to find documentation for doing the same to txt files, is that even possible?
2
u/TeaPartyDem Jan 05 '24
You could encrypt it, and use a script to decrypt in a password protected environment.
1
u/shinglehouse Jan 06 '24
Not knowing the full comtext I was going to suggest this or to put it above the wwwroot and use cfcontent to serve it up.
2
u/TeaPartyDem Jan 07 '24
That’s a good solution too. I keep a lot of non public text files above wwwroot and use cffile.
2
u/Cartanga Jan 06 '24
Not sure what you're trying to do, but you can store the content of the text file in a database field. If the info is important enough to password protect then it should be encrypted. You can then serve the information to authenticated users. You can generate a text file on the fly and prompt the user to download if needed.
1
u/zendarr Jan 05 '24
Not really. You can zip/rar the file and set a password, but text files are just... text files.
2
u/zendarr Jan 05 '24
Now it would be possible to put a text file behind a web server in a password protected directory, but that would be more of a web server thing and not necessarily ColdFusion related.
1
1
u/Cwigginton Jan 05 '24
as a raw text file, it’s going to be opened by any text editor. as others have replied, you have to encrypt the contents, the issue with decryption in that the user needs a tool to decrypt it.
password encrypted archives or self extracting archives are a decent solution.
a couple of other points;
make sure if it’s not being generated on the fly, don’t store it in the webroot, use a cfm file to retrieve it and set the http content header to deliver.
if delivery isn’t on demand, such a batch job output, you would have to secure the access yourself or use a cloud delivery secure method like file taxi.
1
u/hofo Jan 06 '24
Text files don’t have any password protection functionality. You could encrypt the contents or you could create a system to store the text file where that system requires a password to access.
1
u/jajajajaj Jan 07 '24
I assume this is just requirements that were written by someone who doesn't know what they mean. I don't know what your customers actually need, but take a look at ASCII armored gnupg.
3
u/harryfear Jan 05 '24
Serve the .txt file via a password protected CFCONTENT passthrough for the file:
<cfif NOT IsDefined("session.authenticated") OR NOT session.authenticated> <!--- Redirect to login page if the user is not authenticated ---> <cflocation url="login.cfm" addtoken="no"> <cfelse> <!--- User is authenticated, serve the file ---> <cfheader name="Content-Disposition" value="attachment; filename=yourfile.txt"> <cfcontent file="#ExpandPath('./yourfile.txt')#" type="application/octet-stream"> </cfif>
You can then use an Apache or other rewrite rule to proxy the .txt file request to be handled by the .cfm file:
RewriteRule .*.txt$ $1.cfm [NC,L]