Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
slapos
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Xavier Thompson
slapos
Commits
4c304fcf
Commit
4c304fcf
authored
Nov 08, 2024
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplehttpserver: Disable write access by default
parent
83200a29
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
37 deletions
+95
-37
slapos/recipe/simplehttpserver/__init__.py
slapos/recipe/simplehttpserver/__init__.py
+8
-4
slapos/recipe/simplehttpserver/simplehttpserver.py
slapos/recipe/simplehttpserver/simplehttpserver.py
+17
-10
slapos/test/recipe/test_simplehttpserver.py
slapos/test/recipe/test_simplehttpserver.py
+70
-23
No files found.
slapos/recipe/simplehttpserver/__init__.py
View file @
4c304fcf
...
...
@@ -29,12 +29,15 @@ import string, random
import
os
from
six.moves
import
range
from
zc.buildout.buildout
import
bool_option
class
Recipe
(
GenericBaseRecipe
):
def
__init__
(
self
,
buildout
,
name
,
options
):
base_path
=
options
[
'base-path'
]
if
options
.
get
(
'use-hash-url'
,
'True'
)
in
[
'true'
,
'True'
]
:
if
bool_option
(
options
,
'use-hash-url'
,
'false'
)
:
pool
=
string
.
ascii_letters
+
string
.
digits
hash_string
=
''
.
join
(
random
.
choice
(
pool
)
for
i
in
range
(
64
))
path
=
os
.
path
.
join
(
base_path
,
hash_string
)
...
...
@@ -66,7 +69,8 @@ class Recipe(GenericBaseRecipe):
'log-file'
:
self
.
options
[
'log-file'
],
'cert-file'
:
self
.
options
.
get
(
'cert-file'
,
''
),
'key-file'
:
self
.
options
.
get
(
'key-file'
,
''
),
'root-dir'
:
self
.
options
[
'root-dir'
]
'root-dir'
:
self
.
options
[
'root-dir'
],
'allow-write'
:
bool_option
(
self
.
options
,
'allow-write'
,
'false'
)
}
return
self
.
createPythonScript
(
...
...
slapos/recipe/simplehttpserver/simplehttpserver.py
View file @
4c304fcf
...
...
@@ -15,23 +15,29 @@ class ServerHandler(SimpleHTTPRequestHandler):
document_path
=
''
restrict_root_folder
=
True
restrict_write
=
True
def
respond
(
self
,
code
=
200
,
type
=
'text/html'
):
self
.
send_response
(
code
)
self
.
send_header
(
"Content-type"
,
type
)
self
.
end_headers
()
def
restricted
Root
Access
(
self
):
def
restrictedAccess
(
self
):
if
self
.
restrict_root_folder
and
self
.
path
and
self
.
path
==
'/'
:
# no access to root path
self
.
respond
(
403
)
self
.
wfile
.
write
(
b"Forbidden"
)
return
True
if
self
.
restrict_write
and
self
.
command
not
in
(
'GET'
,
'HEAD'
):
# no write access
self
.
respond
(
403
)
self
.
wfile
.
write
(
b"Forbidden"
)
return
True
return
False
def
do_GET
(
self
):
logging
.
info
(
'%s - GET: %s
\
n
%s'
%
(
self
.
client_address
[
0
],
self
.
path
,
self
.
headers
))
if
self
.
restricted
Root
Access
():
if
self
.
restrictedAccess
():
return
SimpleHTTPRequestHandler
.
do_GET
(
self
)
...
...
@@ -46,7 +52,7 @@ class ServerHandler(SimpleHTTPRequestHandler):
request can be encoded as application/x-www-form-urlencoded or multipart/form-data
"""
logging
.
info
(
'%s - POST: %s
\
n
%s'
%
(
self
.
client_address
[
0
],
self
.
path
,
self
.
headers
))
if
self
.
restricted
Root
Access
():
if
self
.
restrictedAccess
():
return
form
=
cgi
.
FieldStorage
(
...
...
@@ -83,7 +89,7 @@ class ServerHandler(SimpleHTTPRequestHandler):
logging
.
error
(
'Failed to create file in %s. The error is
\
n
%s'
%
(
file_path
,
str
(
exception
)))
logging
.
info
(
'Writing rec
ie
ved content to file %s'
%
file_path
)
logging
.
info
(
'Writing rec
ei
ved content to file %s'
%
file_path
)
try
:
with
open
(
file_path
,
method
)
as
myfile
:
myfile
.
write
(
content
)
...
...
@@ -110,6 +116,7 @@ def run(args):
Handler
=
ServerHandler
Handler
.
document_path
=
args
[
'root-dir'
]
Handler
.
restrict_root_folder
=
(
args
[
'root-dir'
]
!=
args
[
'cwd'
])
Handler
.
restrict_write
=
not
args
[
'allow-write'
]
if
valid_ipv6
(
host
):
server
=
HTTPServerV6
...
...
slapos/test/recipe/test_simplehttpserver.py
View file @
4c304fcf
...
...
@@ -21,35 +21,26 @@ class SimpleHTTPServerTest(unittest.TestCase):
self
.
install_dir
=
tempfile
.
mkdtemp
()
self
.
addCleanup
(
shutil
.
rmtree
,
self
.
install_dir
)
self
.
wrapper
=
os
.
path
.
join
(
self
.
install_dir
,
'server'
)
self
.
process
=
None
def
setUpRecipe
(
self
,
opt
=
()):
host
,
port
=
os
.
environ
[
'SLAPOS_TEST_IPV4'
],
9999
self
.
server_url
=
'http://{host}:{port}'
.
format
(
host
=
host
,
port
=
port
)
self
.
recipe
=
makeRecipe
(
simplehttpserver
.
Recipe
,
options
=
{
options
=
{
'base-path'
:
self
.
base_path
,
'host'
:
host
,
'port'
:
port
,
'log-file'
:
os
.
path
.
join
(
self
.
install_dir
,
'simplehttpserver.log'
),
'wrapper'
:
self
.
wrapper
,
},
}
options
.
update
(
opt
)
self
.
recipe
=
makeRecipe
(
simplehttpserver
.
Recipe
,
options
=
options
,
name
=
'simplehttpserver'
,
)
self
.
server_url
=
'http://{host}:{port}'
.
format
(
host
=
host
,
port
=
port
)
def
tearDown
(
self
):
if
self
.
process
:
self
.
process
.
terminate
()
self
.
process
.
wait
()
def
test_options
(
self
):
self
.
assertNotEqual
(
self
.
recipe
.
options
[
'path'
],
''
)
self
.
assertEqual
(
self
.
recipe
.
options
[
'root-dir'
],
os
.
path
.
join
(
self
.
base_path
,
self
.
recipe
.
options
[
'path'
],
))
def
test_install
(
self
):
def
startServer
(
self
):
self
.
assertEqual
(
self
.
recipe
.
install
(),
self
.
wrapper
)
self
.
process
=
subprocess
.
Popen
(
self
.
wrapper
,
...
...
@@ -70,6 +61,36 @@ class SimpleHTTPServerTest(unittest.TestCase):
self
.
fail
(
'server did not start.
\
n
out: %s error: %s'
%
self
.
process
.
communicate
())
self
.
assertIn
(
'Directory listing for /'
,
resp
.
text
)
return
server_base_url
def
tearDown
(
self
):
if
self
.
process
:
self
.
process
.
terminate
()
self
.
process
.
wait
()
self
.
process
.
communicate
()
# close pipes
self
.
process
=
None
def
test_options_use_hash
(
self
):
self
.
setUpRecipe
({
'use-hash-url'
:
'true'
})
self
.
assertNotEqual
(
self
.
recipe
.
options
[
'path'
],
''
)
self
.
assertEqual
(
self
.
recipe
.
options
[
'root-dir'
],
os
.
path
.
join
(
self
.
base_path
,
self
.
recipe
.
options
[
'path'
],
))
def
test_options_no_hash
(
self
):
self
.
setUpRecipe
()
self
.
assertEqual
(
self
.
recipe
.
options
[
'path'
],
''
)
self
.
assertEqual
(
self
.
recipe
.
options
[
'root-dir'
],
self
.
base_path
)
def
test_write
(
self
):
self
.
setUpRecipe
({
'allow-write'
:
'true'
})
server_base_url
=
self
.
startServer
()
# post with multipart/form-data encoding
resp
=
requests
.
post
(
...
...
@@ -119,3 +140,29 @@ class SimpleHTTPServerTest(unittest.TestCase):
},
)
self
.
assertEqual
(
resp
.
status_code
,
requests
.
codes
.
forbidden
)
def
test_readonly
(
self
):
self
.
setUpRecipe
()
indexpath
=
os
.
path
.
join
(
self
.
base_path
,
'index.txt'
)
indexcontent
=
"This file is served statically and readonly"
with
open
(
indexpath
,
'w'
)
as
f
:
f
.
write
(
indexcontent
)
server_base_url
=
self
.
startServer
()
indexurl
=
os
.
path
.
join
(
server_base_url
,
'index.txt'
)
resp
=
requests
.
get
(
indexurl
)
self
.
assertEqual
(
resp
.
status_code
,
requests
.
codes
.
ok
)
self
.
assertEqual
(
resp
.
text
,
indexcontent
)
resp
=
requests
.
post
(
server_base_url
,
files
=
{
'path'
:
'index.txt'
,
'content'
:
'Not readonly after all'
,
},
)
self
.
assertEqual
(
resp
.
status_code
,
requests
.
codes
.
forbidden
)
with
open
(
indexpath
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
indexcontent
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment