Can OWIN Fileserver middleware allow sub folders - owin

In a new OWIN project I'm using
appBuilder.UseFileServer(new FileServerOptions()
{
FileSystem = new PhysicalFileSystem(#".\public"),
});
Which serves up files in the public folder in my project just fine. But if I make a css folder and put a file in there it doesn't work (404) - I don't want directory browsing, but I do want files from sub folders served up but I can't see an option for that. Am I missing something to do this?

That should just work. The URL needs to include the sub folder name. e.g. /css/my.css

Related

Next.JS security of directory structure and JSON secrets

I have a security question regarding the access of Next.JS directories, and their access requirements. I have a root folder that has my pages, public, src, styles, models folders. In the src folder I have a settings.json file that is a empty JavaScript object. The idea is that settings would be added to this file and accessed by api routes, to check settings that could be modified on this settings.json file... What I am wondering is if the client can actually somehow just read/access the src directory and get the settings.json file. I want to put secret key's here that way I can easily change secret keys without having to restart my server. So I could just update the secret key live, and have it applied to the settings.json file. Then the update would be live immediately and I don't have to change the environment variables and restart the server.
Is it safe to keep and use a json file in the src directory to store confidential data? If not, is there a way to keep and use a json file for this purpose?
Thanks for the help and info.
As juliomalves pointed out client code won't be able to access a directory or file that you have on the server with the exception of the public directory.
Next gives you the ability to serve static assets from [root]/public as documented here
Note: Only assets that are in the public directory at build time will be served by Next.js.
If this directory is ever renamed, these assets are no longer available from a client.
Note: Don't name the public directory anything else. The name cannot be changed and is the only directory used to serve static assets.
"I put a settings.json file right next to that .env file and required it in an api route, could the client somehow download that settings.json file without me purposely sending them the contents/file itself?"
The only way information can be served from an api route is by expressly creating a route to call res[ponse].send() (or res.json()) with data imported from that file. Api routes are not ever bundled on the client side and only ever exist on the server as noted here.
Any file inside the folder pages/api is mapped to /api/* and will be treated as an API endpoint instead of a page. They are server-side only bundles and won't increase your client-side bundle size.
"What I am wondering is if the client can actually somehow just read/access the src directory and get the settings.json file."
As noted above only assets in the /public directory are accessible as files by path. Directories are never accessible in Next as static assets. This is even pointed out in the source code.
send(req, path)
.on('directory', () => {
// We don't allow directories to be read.
const err: any = new Error('No directory access')
err.code = 'ENOENT'
reject(err)
})

How to change the directory/file path of a file being served by Firebase?

I am currently hosting a static website through Firebase, but the one file being served (index.html) exists in my Mac computer's Public folder. Since I would like to serve other files, I want to move the index to a project folder that will contain future linked files (e.g. /public/my-project/index.html).
How would I go about updating the file path/directory?
In your project directory (the directory in which you run firebase deploy or firebase deploy --only hosting), open or create firebase.json and make sure it points to the correct public folder.
{
"hosting": {
"public": "public"
}
}
This is a relative path. If your project and your firebase.json are located in /projects/my-project, this will deploy everything in /projects/my-project/public.
All of this is set up automatically if you initialize your Firebase project with firebase init.
Since your project may contain lots of files that aren't supposed to be deployed (readme, node_modules, build scripts etc.), I'd recommend having a public folder inside the project folder, like described above. (You can, however, ignore files with the ignore option, but you'd like to keep this list of ignored files manageable.)
If you really don't want a public folder inside your project, you may try pointing to the current folder from your firebase.json:
{
"hosting": {
"public": "."
}
}
More information about hosting configuration: https://firebase.google.com/docs/hosting/full-config
I accidentally stumbled across the answer while tweaking my settings in Terminal, it was a lot simpler than I expected. For future reference to anyone who sees this,
Open Terminal, and enter firebase init (assuming you have node.js and firebase already installed).
Use your arrows keys to navigate down to the ○ Hosting option, press your spacebar to select, and Return/Enter key to proceed.
The question "What do you want to use as your public directory?" will prompt. Type your desired file path (i.e. public/my-project/public) and enter.
The question "Configure as a single-page app (rewrite all urls to /index.html)?" This is your choice and depends on your platform.
If a file named index.html already exists in that location, it will ask if you want to overwrite it and create a new one. Most likely, you will want to keep the existing index you have -- if this is the case, type n.
Make sure to also follow #nicoqh's valuable advice and differentiate between your public and private folders so that not all of your files end up getting deployed by Firebase.

Securing files in Google Cloud app engine (NodeJS)

I have created a small web application with NodeJS Express. Basically a webserver that has a 'webserver.properties' file. With a very basic app.yaml file.
After deploying it to Google Cloud by use of 'gcloud app deploy' I get the everything up and running.
However...when I open the following URL in the browser: https://webserverurl.com/webserver.properties , the webserver.properties file can be approached and is in turn downloaded immediately.
How can I prevent this from happening and make sure that such properties files are inaccessible from outside?
The problem is that when you use this line:
app.use('/', express.static(__dirname + '/'));
you are giving access to your root directory. See this for a definition of __dirname. If you want to give access to a specific folder you can do this:
Lets say your root directory is src and you fave a dir with static files called src/myfiles. In order to give acces to files in myfiles you can use this line:
app.use('/mypathname', express.static('myfiles'));
where:
'/mypathname' is the part pertaining your URL. In your case it would be https://webserverurl.com/mypathname/any-file-name.jpg
express.static('myfiles') is the name of your local dir.
See this guide.
Hope this helps

How do you change the express static directories?

I am working on a development platform, I have code similar to the following:
app.use('/public', express.static( config.directory.public ));
The issue is that there are many (100s) of projects each with its own directory structure. The project will be selected via the URL:
http://localhost/dev/accounts
Where accounts is a project with its own directory tree and static public directory.
I do not want to run a separate copy of node for each project. Once a project has been selected via the URL then express needs to be reconfigured to serve files for that request.
However, that approach is probably not feasible because we may be working on many projects at the same time. So every request for static files would have to be processed according to the project URL. It seems to negate the benefit of static directories.
I think what I am after is a way to put variables into the directory path
http://localhost/dev/accounts
Would set a variable called prj = "accounts" and then somehow set express so that the root directory is "c:\projects\" + prj + "\public".
If I simply issue a new app.use(..) statement for every request I imagine bad things will happen.
Maybe I am better off just manually reading the file contents for each static request and sending the contents back.
Is there another way to approach this problem?
I'm not sure if I understood your question correctly, but express serves static files in file directories automatically for you. If you have a bunch of projects in some 'path/to/public' folder, you just need to do something like
app.use('/', express.static( __dirname + '/public' ));
That way, you just need to type some url like
http://localhost/project1
or
http://localhost/project2

getting the static directory with nodejs using express?

i have set the static web directory for use in my nodejs app:
app.use express.static(process.cwd(), 'www')
This brings up all the static files like css, images etc, when im in the root it works
http://localhost:8124/
however if i go to somewhere like /tags, it deosnt bring up the static files:
http://localhost:8124/tags/
i get 404 error on the console because its trying to access the www folder
with
/tags/www/ ....
im not sure how to solve this problem, thanks
It looks like you're referring to static assets, without leading /, this results in appending relative asset address to the current URL. Instead refer to your static assets with leading /
eg. /www/style.css rather than www/style.css

Resources