2019-05-08 19:20:30 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
2019-05-27 09:27:55 -04:00
|
|
|
import unittest
|
2019-05-08 19:20:30 -04:00
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
from http_server import spawn
|
|
|
|
from util import DenoTestCase, root_path, test_main, tty_capture
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
PERMISSIONS_PROMPT_TEST_TS = "tools/complex_permissions_test.ts"
|
|
|
|
|
|
|
|
PROMPT_PATTERN = b'⚠️'
|
|
|
|
PERMISSION_DENIED_PATTERN = b'PermissionDenied: permission denied'
|
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
@unittest.skipIf(os.name == 'nt', "Unable to test tty on Windows")
|
|
|
|
class BaseComplexPermissionTest(DenoTestCase):
|
2019-05-27 09:27:55 -04:00
|
|
|
def _run_deno(self, flags, args):
|
2019-05-08 19:20:30 -04:00
|
|
|
"Returns (return_code, stdout, stderr)."
|
2019-05-27 09:27:55 -04:00
|
|
|
cmd = ([self.deno_exe, "run", "--no-prompt"] + flags +
|
|
|
|
[PERMISSIONS_PROMPT_TEST_TS] + args)
|
|
|
|
return tty_capture(cmd, b'')
|
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
class TestReadPermissions(BaseComplexPermissionTest):
|
|
|
|
test_type = "read"
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_inside_project_dir(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-" + self.test_type + "=" + root_path],
|
|
|
|
[self.test_type, "package.json", "tests/subdir/config.json"])
|
2019-05-08 19:20:30 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_outside_test_dir(self):
|
|
|
|
code, _stdout, stderr = self._run_deno([
|
|
|
|
"--allow-" + self.test_type + "=" + os.path.join(
|
|
|
|
root_path, "tests")
|
|
|
|
], [self.test_type, "package.json"])
|
2019-05-08 19:20:30 -04:00
|
|
|
assert code == 1
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_inside_test_dir(self):
|
|
|
|
code, _stdout, stderr = self._run_deno([
|
|
|
|
"--allow-" + self.test_type + "=" + os.path.join(
|
|
|
|
root_path, "tests")
|
|
|
|
], [self.test_type, "tests/subdir/config.json"])
|
2019-05-08 19:20:30 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_outside_test_and_js_dir(self):
|
|
|
|
code, _stdout, stderr = self._run_deno([
|
|
|
|
"--allow-" + self.test_type + "=" + os.path.join(
|
2019-05-08 19:20:30 -04:00
|
|
|
root_path, "tests") + "," + os.path.join(root_path, "js")
|
2019-05-27 09:27:55 -04:00
|
|
|
], [self.test_type, "package.json"])
|
2019-05-08 19:20:30 -04:00
|
|
|
assert code == 1
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_inside_test_and_js_dir(self):
|
|
|
|
code, _stdout, stderr = self._run_deno([
|
|
|
|
"--allow-" + self.test_type + "=" + os.path.join(
|
2019-05-08 19:20:30 -04:00
|
|
|
root_path, "tests") + "," + os.path.join(root_path, "js")
|
2019-05-27 09:27:55 -04:00
|
|
|
], [self.test_type, "js/dir_test.ts", "tests/subdir/config.json"])
|
2019-05-08 19:20:30 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_relative(self):
|
2019-05-23 16:47:55 -04:00
|
|
|
# Save and restore curdir
|
|
|
|
saved_curdir = os.getcwd()
|
|
|
|
os.chdir(root_path)
|
2019-05-27 09:27:55 -04:00
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-" + self.test_type + "=" + "./tests"],
|
|
|
|
[self.test_type, "tests/subdir/config.json"])
|
2019-05-23 16:47:55 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
os.chdir(saved_curdir)
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_no_prefix(self):
|
2019-05-23 16:47:55 -04:00
|
|
|
# Save and restore curdir
|
|
|
|
saved_curdir = os.getcwd()
|
|
|
|
os.chdir(root_path)
|
2019-05-27 09:27:55 -04:00
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-" + self.test_type + "=" + "tests"],
|
|
|
|
[self.test_type, "tests/subdir/config.json"])
|
2019-05-23 16:47:55 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
os.chdir(saved_curdir)
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
class TestWritePermissions(TestReadPermissions):
|
|
|
|
test_type = "write"
|
|
|
|
|
|
|
|
|
|
|
|
class TestNetFetchPermissions(BaseComplexPermissionTest):
|
|
|
|
test_type = "net_fetch"
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_localhost_4545(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=localhost:4545"],
|
|
|
|
[self.test_type, "http://localhost:4545"])
|
2019-05-23 16:47:55 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_deno_land(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=deno.land"],
|
|
|
|
[self.test_type, "http://localhost:4545"])
|
|
|
|
assert code == 1
|
2019-05-09 12:20:34 -04:00
|
|
|
assert not PROMPT_PATTERN in stderr
|
2019-05-27 09:27:55 -04:00
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
|
|
|
def test_allow_localhost_4545_fail(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=localhost:4545"],
|
|
|
|
[self.test_type, "http://localhost:4546"])
|
|
|
|
assert code == 1
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
2019-05-09 12:20:34 -04:00
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_localhost(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(["--allow-net=localhost"], [
|
|
|
|
self.test_type, "http://localhost:4545", "http://localhost:4546",
|
|
|
|
"http://localhost:4547"
|
|
|
|
])
|
2019-05-23 16:47:55 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
class TestNetDialPermissions(BaseComplexPermissionTest):
|
|
|
|
test_type = "net_dial"
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_localhost_ip_4555(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=127.0.0.1:4545"], [self.test_type, "127.0.0.1:4545"])
|
2019-05-23 16:47:55 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_deno_land(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=deno.land"], [self.test_type, "127.0.0.1:4545"])
|
2019-05-22 22:47:15 -04:00
|
|
|
assert code == 1
|
2019-05-17 14:50:53 -04:00
|
|
|
assert not PROMPT_PATTERN in stderr
|
2019-05-22 22:47:15 -04:00
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
2019-05-17 14:50:53 -04:00
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_localhost_ip_4545_fail(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=127.0.0.1:4545"], [self.test_type, "127.0.0.1:4546"])
|
2019-05-17 14:50:53 -04:00
|
|
|
assert code == 1
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_localhost_ip(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(["--allow-net=127.0.0.1"], [
|
|
|
|
self.test_type, "127.0.0.1:4545", "127.0.0.1:4546",
|
|
|
|
"127.0.0.1:4547"
|
|
|
|
])
|
|
|
|
assert code == 0
|
2019-05-17 14:50:53 -04:00
|
|
|
assert not PROMPT_PATTERN in stderr
|
2019-05-27 09:27:55 -04:00
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
class TestNetListenPermissions(BaseComplexPermissionTest):
|
|
|
|
test_type = "net_listen"
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_localhost_4555(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=localhost:4555"], [self.test_type, "localhost:4555"])
|
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
2019-05-17 14:50:53 -04:00
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_deno_land(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=deno.land"], [self.test_type, "localhost:4545"])
|
2019-05-23 16:47:55 -04:00
|
|
|
assert code == 1
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_localhost_4555_fail(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(
|
|
|
|
["--allow-net=localhost:4555"], [self.test_type, "localhost:4556"])
|
|
|
|
assert code == 1
|
2019-05-17 14:50:53 -04:00
|
|
|
assert not PROMPT_PATTERN in stderr
|
2019-05-27 09:27:55 -04:00
|
|
|
assert PERMISSION_DENIED_PATTERN in stderr
|
2019-05-17 14:50:53 -04:00
|
|
|
|
2019-05-27 09:27:55 -04:00
|
|
|
def test_allow_localhost(self):
|
|
|
|
code, _stdout, stderr = self._run_deno(["--allow-net=localhost"], [
|
|
|
|
self.test_type, "localhost:4555", "localhost:4556",
|
|
|
|
"localhost:4557"
|
|
|
|
])
|
2019-05-17 14:50:53 -04:00
|
|
|
assert code == 0
|
|
|
|
assert not PROMPT_PATTERN in stderr
|
|
|
|
assert not PERMISSION_DENIED_PATTERN in stderr
|
|
|
|
|
2019-05-08 19:20:30 -04:00
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
def complex_permissions_tests():
|
|
|
|
return BaseComplexPermissionTest.__subclasses__()
|
2019-05-08 19:20:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-05-30 16:40:40 -04:00
|
|
|
with spawn():
|
|
|
|
test_main()
|