appledger 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
appledger/__init__.py ADDED
@@ -0,0 +1,6 @@
1
+ """AppLedger honest check: security feedback in plain language for apps built with AI."""
2
+
3
+ from .scanner import ScanError, check, doctor
4
+
5
+ __version__ = "0.1.0"
6
+ __all__ = ["ScanError", "check", "doctor", "__version__"]
appledger/__main__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .scanner import main
2
+
3
+ main()
appledger/client.py ADDED
@@ -0,0 +1,78 @@
1
+ """Send a project to the hosted AppLedger scanning service and get the findings back.
2
+
3
+ The project is zipped (without dependencies, build output, media or other large files),
4
+ sent over HTTPS, scanned with Trivy and Opengrep, and deleted on the server right after.
5
+ """
6
+
7
+ import io
8
+ import json
9
+ import os
10
+ import uuid
11
+ import urllib.error
12
+ import urllib.request
13
+ import zipfile
14
+ from pathlib import Path
15
+
16
+ from . import __version__
17
+ from .scanner import ScanError, app_files
18
+
19
+ API_URL = os.environ.get("APPLEDGER_API", "https://appledger.net/api/check.php")
20
+ MAX_FILE_BYTES = 1_000_000
21
+ MAX_ZIP_BYTES = 10_000_000
22
+ SKIP_SUFFIXES = {
23
+ ".png", ".jpg", ".jpeg", ".gif", ".webp", ".ico", ".svg", ".mp4", ".mov", ".mp3", ".wav", ".pdf",
24
+ ".zip", ".gz", ".tgz", ".7z", ".rar", ".exe", ".dll", ".so", ".dylib", ".bin", ".class", ".jar",
25
+ ".pyc", ".db", ".sqlite", ".sqlite3", ".woff", ".woff2", ".ttf", ".otf", ".psd", ".xlsx", ".docx",
26
+ }
27
+
28
+
29
+ def build_zip(app_dir):
30
+ """Zip the files the scanners need. Returns (zip bytes, number of files, skipped files)."""
31
+ app_dir = Path(app_dir)
32
+ buf = io.BytesIO()
33
+ count, skipped = 0, []
34
+ with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
35
+ for path in sorted(app_files(app_dir)):
36
+ rel = path.relative_to(app_dir).as_posix()
37
+ if path.suffix.lower() in SKIP_SUFFIXES or path.is_symlink():
38
+ continue
39
+ if path.stat().st_size > MAX_FILE_BYTES:
40
+ skipped.append(rel)
41
+ continue
42
+ zf.write(path, rel)
43
+ count += 1
44
+ data = buf.getvalue()
45
+ if count == 0:
46
+ raise ScanError(f"No files to check in {app_dir}.")
47
+ if len(data) > MAX_ZIP_BYTES:
48
+ raise ScanError(f"The project is too big to send ({len(data) // 1_000_000} MB after compression, "
49
+ f"limit {MAX_ZIP_BYTES // 1_000_000} MB). Check a smaller folder, or use local mode.")
50
+ return data, count, skipped
51
+
52
+
53
+ def hosted_scan(app_dir):
54
+ """Returns (findings, scanner names) from the AppLedger service."""
55
+ data, count, skipped = build_zip(app_dir)
56
+ boundary = uuid.uuid4().hex
57
+ body = (f"--{boundary}\r\nContent-Disposition: form-data; name=\"project\"; filename=\"project.zip\"\r\n"
58
+ f"Content-Type: application/zip\r\n\r\n").encode() + data + f"\r\n--{boundary}--\r\n".encode()
59
+ req = urllib.request.Request(API_URL, data=body, method="POST", headers={
60
+ "Content-Type": f"multipart/form-data; boundary={boundary}",
61
+ "Accept": "application/json",
62
+ "User-Agent": f"appledger/{__version__}",
63
+ })
64
+ try:
65
+ with urllib.request.urlopen(req, timeout=180) as resp:
66
+ result = json.load(resp)
67
+ except urllib.error.HTTPError as e:
68
+ try:
69
+ message = json.load(e).get("error", "")
70
+ except (ValueError, AttributeError):
71
+ message = ""
72
+ raise ScanError(message or f"The AppLedger service answered with an error ({e.code}). Try again later.")
73
+ except (urllib.error.URLError, TimeoutError) as e:
74
+ raise ScanError(f"Couldn't reach the AppLedger service ({getattr(e, 'reason', e)}). "
75
+ "Check your internet connection, or run in local mode.")
76
+ if not result.get("ok"):
77
+ raise ScanError(result.get("error", "The AppLedger service couldn't check this project."))
78
+ return result["findings"], result["scanners"]