LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-03-2018, 05:39 AM   #1
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Current best practices for FastCGI with Python under Nginx?


I'd like to set up some web-accessible Python 3 scripts using the FastCGI API and UNIX domain sockets. By sockets I mean the files themselves not network connections because I'd prefer to deal with the file system permissions for access control and privilege separation.

Should Nginx be launching and keeping the script alive? Should that happen independently of the web server first?

What are the current best practices for FastCGI with Python under Nginx?
 
Old 11-13-2018, 02:53 PM   #2
/dev/random
Member
 
Registered: Aug 2012
Location: Ontario, Canada
Distribution: Slackware 14.2, LFS-current, NetBSD 6.1.3, OpenIndiana
Posts: 319

Rep: Reputation: 112Reputation: 112
FastCGI should be treated with care, you must take care to escape anything you can when dealing with input of any kind, you must also never write programs that assume any variable assignment, always and I mean always ensure that variables are valid and have traps for every piece of code that references them.

1) Validate user input
2) Ensure any parsing of user input is valid before parsing
3) Any function that references a variable of any kind should check if the contents of that variable is valid, if it isn't make the function default to a safe default
4) Try to avoid any os.system, subprocess or using any external application, use pythons libraries to achieve the same thing.

All it takes is one slip up and that box can be exploited or if there real smart rooted.
 
Old 11-13-2018, 03:13 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Adding to what /dev/random said, all of which is important...in addition to validating input before processing, use the form elements to limit what can be entered, where practical. For example, if asking for a US state name, present a drop-down list with only valid states rather than ask the visitor to type in a name. If the number of options is small, the radio button is effective, too, as only one selection can be made. Sex/gender is a good example

Always use the POST method, to prevent hacking of the URL, which is possible when using GET. The GET method exposes the name/value pairs being passed to the script; the POST method doesn't do that.
 
Old 11-13-2018, 11:30 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359

Original Poster
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by scasey View Post
The GET method exposes the name/value pairs being passed to the script; the POST method doesn't do that.
On the other hand GET does allow the URL to get bookmarked. And it is still trivial to form a custom POST request.

Those are all good practices I've been aware of since 1997 or so, in the context of regular CGI independent of language. My main question is about FastCGI itself and 1) if any particular python modules are recommended to deal with it and 2) how the script is launched. One way would be to launch the python script as a service and let it wait. Another way might be to have the web server itself launch the script and hopefully let it stay running so as to avoid both duplicates and initialization delays.

Any suggestions in regards to dealing with the FastCGI API itself? Flask? Something else?
 
Old 11-14-2018, 12:00 AM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by Turbocapitalist View Post
On the other hand GET does allow the URL to get bookmarked. And it is still trivial to form a custom POST request.
I don't want to hijack your thread; so feel free to ignore this...
I don't know much (anything?) specific about python or FastCGI.
I get (pun intended) what you're saying about bookmarking, but I've never been comfortable with having the input data "exposed" like that. It can be tweaked in the address bar...'tho one can, of course, code to catch any invalid stuff. Yes, it can be a pain to re-enter stuff when using POST.

I read "trivial to form a custom POST request" as easy...is that what you meant? I certainly don't find it difficult.

My web-based applications are coded in perl. They usually both present the form and process it, but occasionally the form pages are static and only the server-side script is dynamic.

I'd think whether or not to have the script running all the time would be a function of how fast it runs. I have no problems starting a script to read the database and create 20 reports at one time. Takes about a minute. As long as the user understands that, there's no issue. Additionally, once the script has fired off, waiting for it is not necessary. The links to the reports will be there when the page is reloaded/revisited.

I'm also not sure how a web form would connect to a running process, but maybe you already know how to do that. (See my 2nd sentence, above)

Sorry I can't help with specifics. Will be watching to learn from this thread.
 
Old 11-14-2018, 12:21 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359

Original Poster
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by scasey View Post
My web-based applications are coded in perl. They usually both present the form and process it, but occasionally the form pages are static and only the server-side script is dynamic.
That's how I normally do it: perl with all processing on the server side and no javascript, at least not any js interfering with the functionality. No third-party requests at all for extrnal objects.

As for the python, I won't say more so as to avoid derailing the thread, but am interested in whether the python script should wait on its own or get its initial launch from the web server. The python module flup seems to be useful for FastCGI, is there an optimal way to use it? Or a better module?
 
  


Reply

Tags
fastcgi, nginx, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Nginx adn FastCGI on Ubuntu 14 + No input file specific lamletoi Linux - Server 2 09-21-2014 09:01 PM
How to secure nginx +php using fastcgi in shared enviorment Kraiser10 Linux - Security 3 08-31-2014 06:41 AM
[SOLVED] fastcgi cannot find php files after I created nginx chroot environment Laertiades Linux - Server 1 01-30-2013 01:03 PM
[SOLVED] Nginx not passing the PHP scripts to FastCGI server listening on 127.0.0.1:9000 dreamcoder Linux - Server 8 08-02-2012 08:46 AM
LXer: Drupal 6 Hosting With nginx And PHP-FastCGI On Ubuntu 9.10 LXer Syndicated Linux News 0 04-08-2010 02:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 01:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration