Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Even with the "--restrict" option, some file names do not cause an error when imported. #1585

Closed
irreg opened this issue Nov 13, 2023 · 13 comments
Labels
invalid no title, unclear description, or no support for this issue

Comments

@irreg
Copy link

irreg commented Nov 13, 2023

Depending on the file name, neither import, function call, nor direct invocation of the file may result in an error.

Environment

  • Python 3.8.9
  • Pyarmor 8.4.3
  • Windows 10/11 x64
$ pyarmor gen -O dist --assert-call foo.py
INFO     Python 3.8.9
INFO     Pyarmor 8.4.3 (trial), 000000, non-profits
INFO     Platform windows.x86_64

Reproduction procedures

a.py

import t
t.f()

t.py

t.py is one of the error-free filenames. There are many other error-free filenames.
If the filename is s.py, then an error will occur on import. However, changing the file name after encryption does not change the result. The name must not be t.py before encryption.

def f():
    print("test")
  1. Execute: pyarmor gen --restrict t.py
  2. Copy a.py into the dist folder
  3. Execute: python a.py in dist folder
  4. Execute directly: python t.py in dist folder

Expect an error in steps 3 and 4, but no error in fact

Trace Log (pyarmor.trace.log)

trace.co             t:1:<module>
trace.co             t:1:f

@irreg irreg added the bug label Nov 13, 2023
@jondy
Copy link
Contributor

jondy commented Nov 14, 2023

I think you may misunderstand this option.

First of all, --restrict is only used for package, not for scripts.

I don't know where do you read the documentation? Is it for Pyarmor 8.

Could you list the reference link?

@jondy jondy added invalid no title, unclear description, or no support for this issue and removed bug labels Nov 14, 2023
@jondy jondy closed this as completed Nov 14, 2023
@irreg
Copy link
Author

irreg commented Nov 14, 2023

Sorry. I modified it too much to make it simple.
The problem occurs that you can import even in the case of package.

Of course, I had read the documentation and understood that the restrict option was for package only, but I didn't think that individual files in a package should not be specifiled for encryption, and since it appeared to me that there was no change in behavior, I misunderstood.

File Structure

root
|- a.py
|- p
.... |- t.py

a.py

import p.t as t

t.f()

t.py

No change

Reproduction procedures

  1. Execute: pyarmor gen --restrict p
  2. Copy a.py into the dist folder
  3. Execute in dist folder: python a.py

Expect an error in steps 3, but no error in fact

@irreg
Copy link
Author

irreg commented Nov 21, 2023

@jondy
I am sorry to repeatedly interrupt your busy schedule, but could you please confirm the contents of this issue?

@irreg
Copy link
Author

irreg commented Nov 28, 2023

I have revised the content to make it a bit clearer. Also, after rechecking, it seems that not only --restrict, but also --private causes similar problems.

Reproduction procedures

File Structure

root
|- a.py
|- p
    |- s.py
    |- t.py

a.py

try:
    import p.s as s
    s.a()
except Exception as e:
    print(e)
try:
    import p.t as t
    t.a()
except Exception as e:
    print(e)

s.py and t.py

def a():
    print("protected")
  1. Execute: pyarmor gen --restrict p
  2. Copy a.py into the dist folder
  3. Move to dist folder and execute: python a.py

Expected result

unauthorized use of script (1:1085)
unauthorized use of script (1:1085)

Actual result

unauthorized use of script (1:1085)
protected

Trace Log (pyarmor.trace.log)

trace.co             p.s:1:<module>
trace.co             p.s:1:a
trace.co             p.t:1:<module>
trace.co             p.t:1:a

@jondy
Copy link
Contributor

jondy commented Nov 29, 2023

Can't reproduced, it raises 2 exceptions in my test, please make sure no local configuration to change something

@irreg
Copy link
Author

irreg commented Nov 29, 2023

Thank you very much. We will examine the situation closely.

@irreg
Copy link
Author

irreg commented Nov 29, 2023

Deleting the pyarmor configuration had no effect. It was reproduced in the environment immediately after installing pyarmor for the first time.

I don't know why it doesn't reproduce in your environment at this time, but I fixed it so that the reproduction procedure can be performed automatically.
I'll keep investigating for a while.

Reproduction procedures

a.py

import string
import os
import subprocess

func = """
def a():
    print("protected")
"""

os.makedirs(r"p", exist_ok=True)
for s in string.ascii_lowercase:
    with open(rf"p\{s}.py", "w") as f:
        f.write(func)

subprocess.run("python -m venv .venv")
subprocess.run(".venv\scripts\python -m pip install pyarmor==8.4.4")
subprocess.run(".venv\scripts\pyarmor gen --restrict p")


with open(rf"dist\main.py", "w") as f:
    for s in string.ascii_lowercase:
        func = f"""
try:
    import p.{s}
    p.{s}.a()
except Exception as e:
    print(f"{s}.py: {{e}}")
else:
    print(f"{s}.py: could be accessed")
"""
        f.write(func)

subprocess.run(r".venv\scripts\python dist\main.py")
  1. Execute: python a.py

Expected result

a.py: unauthorized use of script (1:1087)
b.py: unauthorized use of script (1:1087)
c.py: unauthorized use of script (1:1087)
d.py: unauthorized use of script (1:1087)
e.py: unauthorized use of script (1:1087)
f.py: unauthorized use of script (1:1087)
g.py: unauthorized use of script (1:1087)
h.py: unauthorized use of script (1:1087)
i.py: unauthorized use of script (1:1087)
j.py: unauthorized use of script (1:1087)
k.py: unauthorized use of script (1:1087)
l.py: unauthorized use of script (1:1087)
m.py: unauthorized use of script (1:1087)
n.py: unauthorized use of script (1:1087)
o.py: unauthorized use of script (1:1087)
p.py: unauthorized use of script (1:1087)
q.py: unauthorized use of script (1:1087)
r.py: unauthorized use of script (1:1087)
s.py: unauthorized use of script (1:1087)
t.py: unauthorized use of script (1:1087)
u.py: unauthorized use of script (1:1087)
v.py: unauthorized use of script (1:1087)
w.py: unauthorized use of script (1:1087)
x.py: unauthorized use of script (1:1087)
y.py: unauthorized use of script (1:1087)
z.py: unauthorized use of script (1:1087)

Actual result

a.py: unauthorized use of script (1:1087)
b.py: unauthorized use of script (1:1087)
c.py: unauthorized use of script (1:1087)
d.py: unauthorized use of script (1:1087)
e.py: unauthorized use of script (1:1087)
f.py: unauthorized use of script (1:1087)
g.py: unauthorized use of script (1:1087)
h.py: unauthorized use of script (1:1087)
protected
i.py: could be accessed
j.py: unauthorized use of script (1:1087)
k.py: unauthorized use of script (1:1087)
l.py: unauthorized use of script (1:1087)
m.py: unauthorized use of script (1:1087)
protected
n.py: could be accessed
o.py: unauthorized use of script (1:1087)
p.py: unauthorized use of script (1:1087)
q.py: unauthorized use of script (1:1087)
r.py: unauthorized use of script (1:1087)
s.py: unauthorized use of script (1:1087)
protected
t.py: could be accessed
u.py: unauthorized use of script (1:1087)
v.py: unauthorized use of script (1:1087)
w.py: unauthorized use of script (1:1087)
x.py: unauthorized use of script (1:1087)
y.py: unauthorized use of script (1:1087)
z.py: unauthorized use of script (1:1087)

@irreg
Copy link
Author

irreg commented Jan 16, 2024

I have tried multiple environments, Windows 10, 11, and linux(Ubuntu), and it occurs in all of them.
We are unable to find an environment where the problem does not occur in our environments.

We would like to closely examine the conditions under which the problem occurs, so can you please provide us with information on your environments in which the problem does not reproduce itself?

@jondy
Copy link
Contributor

jondy commented Jan 16, 2024

Make sure you're using Pyarmor 8.4.6

@irreg
Copy link
Author

irreg commented Jan 16, 2024

Confirmed with the latest version.

Microsoft Windows [Version 10.0.18363.592]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\Admin\Desktop\src12_auto>python a.py
Collecting pyarmor==8.4.6
  Using cached pyarmor-8.4.6-py3-none-any.whl (2.7 MB)
Collecting pyarmor.cli.core~=5.4.3
  Using cached pyarmor.cli.core-5.4.3-cp38-none-win_amd64.whl (402 kB)
Installing collected packages: pyarmor.cli.core, pyarmor
Successfully installed pyarmor-8.4.6 pyarmor.cli.core-5.4.3
WARNING: You are using pip version 20.2.3; however, version 23.3.2 is available.
You should consider upgrading via the 'C:\Users\Admin\Desktop\src12_auto\.venv\scripts\python.exe -m pip install --upgrade pip' command.
INFO     Python 3.8.9
INFO     Pyarmor 8.4.6 (group), ******, **************
INFO     Platform windows.x86_64
INFO     search inputs ...
INFO     find package at p
INFO     find 1 top resources
INFO     start to generate runtime files
INFO     target platforms {'windows.amd64'}
INFO     write dist\pyarmor_runtime_******\pyarmor_runtime.pyd
INFO     generate runtime files OK
INFO     start to obfuscate scripts
INFO     process resource "p"
INFO     obfuscating file a.py
INFO     write dist\p\a.py
INFO     obfuscating file b.py
INFO     write dist\p\b.py
INFO     obfuscating file c.py
INFO     write dist\p\c.py
INFO     obfuscating file d.py
INFO     write dist\p\d.py
INFO     obfuscating file e.py
INFO     write dist\p\e.py
INFO     obfuscating file f.py
INFO     write dist\p\f.py
INFO     obfuscating file g.py
INFO     write dist\p\g.py
INFO     obfuscating file h.py
INFO     write dist\p\h.py
INFO     obfuscating file i.py
INFO     write dist\p\i.py
INFO     obfuscating file j.py
INFO     write dist\p\j.py
INFO     obfuscating file k.py
INFO     write dist\p\k.py
INFO     obfuscating file l.py
INFO     write dist\p\l.py
INFO     obfuscating file m.py
INFO     write dist\p\m.py
INFO     obfuscating file n.py
INFO     write dist\p\n.py
INFO     obfuscating file o.py
INFO     write dist\p\o.py
INFO     obfuscating file p.py
INFO     write dist\p\p.py
INFO     obfuscating file q.py
INFO     write dist\p\q.py
INFO     obfuscating file r.py
INFO     write dist\p\r.py
INFO     obfuscating file s.py
INFO     write dist\p\s.py
INFO     obfuscating file t.py
INFO     write dist\p\t.py
INFO     obfuscating file u.py
INFO     write dist\p\u.py
INFO     obfuscating file v.py
INFO     write dist\p\v.py
INFO     obfuscating file w.py
INFO     write dist\p\w.py
INFO     obfuscating file x.py
INFO     write dist\p\x.py
INFO     obfuscating file y.py
INFO     write dist\p\y.py
INFO     obfuscating file z.py
INFO     write dist\p\z.py
INFO     obfuscate scripts OK
a.py: unauthorized use of script (1:1087)
b.py: unauthorized use of script (1:1087)
c.py: unauthorized use of script (1:1087)
d.py: unauthorized use of script (1:1087)
e.py: unauthorized use of script (1:1087)
f.py: unauthorized use of script (1:1087)
g.py: unauthorized use of script (1:1087)
h.py: unauthorized use of script (1:1087)
protected
i.py: could be accessed
j.py: unauthorized use of script (1:1087)
k.py: unauthorized use of script (1:1087)
l.py: unauthorized use of script (1:1087)
m.py: unauthorized use of script (1:1087)
protected
n.py: could be accessed
o.py: unauthorized use of script (1:1087)
p.py: unauthorized use of script (1:1087)
q.py: unauthorized use of script (1:1087)
r.py: unauthorized use of script (1:1087)
s.py: unauthorized use of script (1:1087)
protected
t.py: could be accessed
u.py: unauthorized use of script (1:1087)
v.py: unauthorized use of script (1:1087)
w.py: unauthorized use of script (1:1087)
x.py: unauthorized use of script (1:1087)
y.py: unauthorized use of script (1:1087)
z.py: unauthorized use of script (1:1087)

C:\Users\Admin\Desktop\src12_auto>

@jondy
Copy link
Contributor

jondy commented Jan 16, 2024

Reproduced, I'll check it.

jondy added a commit that referenced this issue Jan 16, 2024
@jondy
Copy link
Contributor

jondy commented Jan 16, 2024

It will be fixed in next release, or apply this patch for quick workaround

--- a/pyarmor/cli/context.py
+++ b/pyarmor/cli/context.py
@@ -466,7 +466,7 @@ class Context(object):
 
     @property
     def exclude_restrict_modules(self):
-        return self._opts('builder', 'exclude_restrict_modules')
+        return self._opts('builder', 'exclude_restrict_modules').split()

@irreg
Copy link
Author

irreg commented Jan 16, 2024

Thanks for the quick fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid no title, unclear description, or no support for this issue
Projects
None yet
Development

No branches or pull requests

2 participants