universal-screenshot-mcp 1.0.0

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.
@@ -0,0 +1,72 @@
1
+ import { promises as fsPromises } from 'fs';
2
+ import { dirname, join, resolve, relative } from 'path';
3
+ const defaultFileSystem = {
4
+ realpath: (path) => fsPromises.realpath(path),
5
+ };
6
+ /**
7
+ * Validate an output path to prevent path traversal attacks (SEC-004).
8
+ * Uses fs.realpath() to resolve symlinks, preventing TOCTOU attacks.
9
+ */
10
+ export async function validateOutputPath(customPath, defaultName, config, fileSystem = defaultFileSystem) {
11
+ // Treat undefined, empty, or whitespace-only as "no custom path"
12
+ if (!customPath || customPath.trim() === '') {
13
+ // No custom path - use default directory (always safe)
14
+ return { valid: true, path: join(config.defaultOutDir, defaultName) };
15
+ }
16
+ // Reject null bytes (path injection vector)
17
+ if (customPath.includes('\x00') || customPath.includes('%00')) {
18
+ return { valid: false, error: 'Path contains null bytes' };
19
+ }
20
+ // First resolve to absolute form
21
+ let targetPath;
22
+ if (customPath.startsWith('/')) {
23
+ targetPath = resolve(customPath);
24
+ }
25
+ else {
26
+ // Relative paths resolved against defaultOutDir
27
+ targetPath = resolve(config.defaultOutDir, customPath);
28
+ }
29
+ // SEC-004: Resolve symlinks to get the REAL path
30
+ // This prevents attackers from using symlinks to bypass directory restrictions
31
+ let realPath;
32
+ try {
33
+ // Try to resolve the full path (works if file/symlink already exists)
34
+ realPath = await fileSystem.realpath(targetPath);
35
+ }
36
+ catch {
37
+ // Path doesn't exist yet - resolve the parent directory's symlinks
38
+ // This handles the case where we're writing a new file
39
+ const parentDir = dirname(targetPath);
40
+ const fileName = targetPath.substring(parentDir.length + 1);
41
+ try {
42
+ const realParent = await fileSystem.realpath(parentDir);
43
+ realPath = join(realParent, fileName);
44
+ }
45
+ catch {
46
+ // Parent directory doesn't exist either - fail closed
47
+ return { valid: false, error: `Parent directory does not exist: ${parentDir}` };
48
+ }
49
+ }
50
+ // Check if the REAL path (with symlinks resolved) is within any allowed directory
51
+ // Also resolve symlinks in allowed directories for consistent comparison
52
+ for (const allowedDir of config.allowedOutputDirs) {
53
+ let realAllowedDir;
54
+ try {
55
+ realAllowedDir = await fileSystem.realpath(allowedDir);
56
+ }
57
+ catch {
58
+ // Allowed directory doesn't exist, use resolved form
59
+ realAllowedDir = resolve(allowedDir);
60
+ }
61
+ const relativePath = relative(realAllowedDir, realPath);
62
+ // If relative path doesn't start with '..' and isn't absolute, it's within the allowed dir
63
+ if (!relativePath.startsWith('..') && !relativePath.startsWith('/')) {
64
+ return { valid: true, path: realPath };
65
+ }
66
+ }
67
+ // Path is outside all allowed directories (after symlink resolution)
68
+ return {
69
+ valid: false,
70
+ error: 'Output path must be within allowed directories (~/Desktop/Screenshots, ~/Downloads, ~/Documents, or /tmp). Symlinks to other locations are not permitted.',
71
+ };
72
+ }
@@ -0,0 +1,180 @@
1
+ import { promises as dns } from 'dns';
2
+ import { URL } from 'url';
3
+ const defaultDnsResolver = {
4
+ lookup: (hostname, options) => dns.lookup(hostname, options),
5
+ };
6
+ /** Check if an IPv4 address is in a blocked range. */
7
+ export function isBlockedIPv4(ip) {
8
+ const ipv4Match = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
9
+ if (!ipv4Match) {
10
+ return { blocked: false }; // Not an IPv4 address, check elsewhere
11
+ }
12
+ const [, a, b, c, d] = ipv4Match.map(Number);
13
+ // Block loopback: 127.0.0.0/8
14
+ if (a === 127) {
15
+ return { blocked: true, reason: 'Access to loopback addresses is not allowed' };
16
+ }
17
+ // Block private: 10.0.0.0/8
18
+ if (a === 10) {
19
+ return { blocked: true, reason: 'Access to private network (10.x.x.x) is not allowed' };
20
+ }
21
+ // Block private: 172.16.0.0/12 (172.16.x.x - 172.31.x.x)
22
+ if (a === 172 && b >= 16 && b <= 31) {
23
+ return { blocked: true, reason: 'Access to private network (172.16-31.x.x) is not allowed' };
24
+ }
25
+ // Block private: 192.168.0.0/16
26
+ if (a === 192 && b === 168) {
27
+ return { blocked: true, reason: 'Access to private network (192.168.x.x) is not allowed' };
28
+ }
29
+ // Block link-local and cloud metadata: 169.254.0.0/16
30
+ if (a === 169 && b === 254) {
31
+ return { blocked: true, reason: 'Access to link-local/metadata addresses (169.254.x.x) is not allowed' };
32
+ }
33
+ // Block 0.0.0.0/8
34
+ if (a === 0) {
35
+ return { blocked: true, reason: 'Access to 0.x.x.x addresses is not allowed' };
36
+ }
37
+ // Block CGNAT / Shared Address Space: 100.64.0.0/10 (100.64.x.x - 100.127.x.x)
38
+ // Used by AWS VPC, Tailscale, carrier-grade NAT
39
+ if (a === 100 && b >= 64 && b <= 127) {
40
+ return { blocked: true, reason: 'Access to shared/CGNAT addresses (100.64-127.x.x) is not allowed' };
41
+ }
42
+ // Block benchmark testing: 198.18.0.0/15 (198.18.x.x - 198.19.x.x)
43
+ if (a === 198 && (b === 18 || b === 19)) {
44
+ return { blocked: true, reason: 'Access to benchmark addresses (198.18-19.x.x) is not allowed' };
45
+ }
46
+ // Block broadcast: 255.255.255.255
47
+ if (a === 255 && b === 255 && c === 255 && d === 255) {
48
+ return { blocked: true, reason: 'Access to broadcast address is not allowed' };
49
+ }
50
+ return { blocked: false };
51
+ }
52
+ /** Check if an IPv6 address is blocked (localhost, link-local, etc.). */
53
+ export function isBlockedIPv6(ip) {
54
+ const normalized = ip.toLowerCase();
55
+ // Block IPv6 loopback
56
+ if (normalized === '::1') {
57
+ return { blocked: true, reason: 'Access to IPv6 localhost is not allowed' };
58
+ }
59
+ // Block IPv6 link-local (fe80::/10)
60
+ // fe80::/10 means the first 10 bits are 1111111010, covering fe80:: through febf::
61
+ // We parse the first 16-bit group and check (group & 0xFFC0) === 0xFE80
62
+ const firstGroup = normalized.split(':')[0];
63
+ if (firstGroup) {
64
+ const groupValue = parseInt(firstGroup, 16);
65
+ if (!isNaN(groupValue) && (groupValue & 0xffc0) === 0xfe80) {
66
+ return { blocked: true, reason: 'Access to IPv6 link-local addresses is not allowed' };
67
+ }
68
+ }
69
+ // Block IPv6 unique local (fc00::/7 = fc00:: to fdff::)
70
+ if (normalized.startsWith('fc') || normalized.startsWith('fd')) {
71
+ return { blocked: true, reason: 'Access to IPv6 private addresses is not allowed' };
72
+ }
73
+ // Block IPv4-mapped IPv6 addresses in dotted-decimal form (::ffff:x.x.x.x)
74
+ const ipv4MappedMatch = normalized.match(/^::ffff:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/);
75
+ if (ipv4MappedMatch) {
76
+ const ipv4Check = isBlockedIPv4(ipv4MappedMatch[1]);
77
+ if (ipv4Check.blocked) {
78
+ return { blocked: true, reason: `Access to IPv4-mapped blocked address: ${ipv4Check.reason}` };
79
+ }
80
+ }
81
+ // Block IPv4-mapped IPv6 addresses in hex form (::ffff:HHHH:HHHH)
82
+ // Node.js URL parser normalizes ::ffff:127.0.0.1 to ::ffff:7f00:1
83
+ const ipv4MappedHexMatch = normalized.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);
84
+ if (ipv4MappedHexMatch) {
85
+ const high = parseInt(ipv4MappedHexMatch[1], 16);
86
+ const low = parseInt(ipv4MappedHexMatch[2], 16);
87
+ const a = (high >> 8) & 0xff;
88
+ const b = high & 0xff;
89
+ const c = (low >> 8) & 0xff;
90
+ const d = low & 0xff;
91
+ const ipv4 = `${a}.${b}.${c}.${d}`;
92
+ const ipv4Check = isBlockedIPv4(ipv4);
93
+ if (ipv4Check.blocked) {
94
+ return { blocked: true, reason: `Access to IPv4-mapped blocked address: ${ipv4Check.reason}` };
95
+ }
96
+ }
97
+ return { blocked: false };
98
+ }
99
+ /**
100
+ * Validate a URL for SSRF prevention with DNS rebinding protection (SEC-001).
101
+ * Returns resolvedIp and hostname on success to enable IP pinning.
102
+ */
103
+ export async function validateUrl(urlString, dnsResolver = defaultDnsResolver) {
104
+ let parsed;
105
+ try {
106
+ parsed = new URL(urlString);
107
+ }
108
+ catch {
109
+ return { valid: false, error: 'Invalid URL format' };
110
+ }
111
+ // Only allow http and https protocols
112
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
113
+ return { valid: false, error: 'Only http and https protocols are allowed' };
114
+ }
115
+ const hostname = parsed.hostname.toLowerCase();
116
+ // Block localhost by name
117
+ if (hostname === 'localhost' || hostname === 'localhost.localdomain') {
118
+ return { valid: false, error: 'Access to localhost is not allowed' };
119
+ }
120
+ // Block IPv6 localhost in bracket notation
121
+ if (hostname === '[::1]') {
122
+ return { valid: false, error: 'Access to IPv6 localhost is not allowed' };
123
+ }
124
+ // Check if hostname is a literal IP address
125
+ const isLiteralIPv4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.test(hostname);
126
+ const isLiteralIPv6 = hostname.startsWith('[') && hostname.endsWith(']');
127
+ if (isLiteralIPv4) {
128
+ // Direct IPv4 - validate immediately
129
+ const ipCheck = isBlockedIPv4(hostname);
130
+ if (ipCheck.blocked) {
131
+ return { valid: false, error: ipCheck.reason };
132
+ }
133
+ return { valid: true, resolvedIp: hostname, hostname };
134
+ }
135
+ if (isLiteralIPv6) {
136
+ // Direct IPv6 - validate immediately (strip brackets)
137
+ const ipv6 = hostname.slice(1, -1);
138
+ const ipCheck = isBlockedIPv6(ipv6);
139
+ if (ipCheck.blocked) {
140
+ return { valid: false, error: ipCheck.reason };
141
+ }
142
+ return { valid: true, resolvedIp: ipv6, hostname };
143
+ }
144
+ // Hostname provided - resolve DNS to get actual IP addresses
145
+ // This prevents DNS rebinding attacks (SEC-001)
146
+ try {
147
+ // Resolve all A and AAAA records
148
+ const addresses = await dnsResolver.lookup(hostname, { all: true });
149
+ if (addresses.length === 0) {
150
+ return { valid: false, error: 'DNS resolution returned no addresses' };
151
+ }
152
+ // Check ALL resolved IPs - if any is blocked, reject the entire request
153
+ let firstNonBlockedIp;
154
+ for (const addr of addresses) {
155
+ if (addr.family === 4) {
156
+ const ipCheck = isBlockedIPv4(addr.address);
157
+ if (ipCheck.blocked) {
158
+ return { valid: false, error: `DNS resolved to blocked IP: ${ipCheck.reason}` };
159
+ }
160
+ if (!firstNonBlockedIp)
161
+ firstNonBlockedIp = addr.address;
162
+ }
163
+ else if (addr.family === 6) {
164
+ const ipCheck = isBlockedIPv6(addr.address);
165
+ if (ipCheck.blocked) {
166
+ return { valid: false, error: `DNS resolved to blocked IP: ${ipCheck.reason}` };
167
+ }
168
+ if (!firstNonBlockedIp)
169
+ firstNonBlockedIp = addr.address;
170
+ }
171
+ }
172
+ return { valid: true, resolvedIp: firstNonBlockedIp, hostname };
173
+ }
174
+ catch (dnsError) {
175
+ // DNS resolution failed - could be NXDOMAIN, timeout, or network error
176
+ // Fail closed: reject requests we cannot validate
177
+ const errorMessage = dnsError instanceof Error ? dnsError.message : String(dnsError);
178
+ return { valid: false, error: `DNS resolution failed: ${errorMessage}` };
179
+ }
180
+ }
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "universal-screenshot-mcp",
3
+ "version": "1.0.0",
4
+ "description": "An MCP server for capturing web page screenshots via Puppeteer and cross-platform system screenshots via native OS tools",
5
+ "author": "Seth Bang",
6
+ "license": "Apache-2.0",
7
+ "mcpName": "io.github.sethbang/screenshot-server",
8
+ "keywords": [
9
+ "mcp",
10
+ "screenshot",
11
+ "puppeteer",
12
+ "macos",
13
+ "linux",
14
+ "windows",
15
+ "cross-platform",
16
+ "screencapture",
17
+ "model-context-protocol",
18
+ "claude",
19
+ "universal-screenshot",
20
+ "system-screenshot"
21
+ ],
22
+ "type": "module",
23
+ "engines": {
24
+ "node": ">=18.0.0"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/sethbang/mcp-screenshot-server.git"
29
+ },
30
+ "bugs": {
31
+ "url": "https://github.com/sethbang/mcp-screenshot-server/issues"
32
+ },
33
+ "homepage": "https://github.com/sethbang/mcp-screenshot-server#readme",
34
+ "bin": {
35
+ "universal-screenshot-mcp": "build/index.js"
36
+ },
37
+ "files": [
38
+ "build"
39
+ ],
40
+ "scripts": {
41
+ "build": "tsc && chmod 755 build/index.js",
42
+ "prepare": "npm run build",
43
+ "watch": "tsc --watch",
44
+ "inspector": "npx @modelcontextprotocol/inspector build/index.js",
45
+ "test": "vitest run",
46
+ "test:watch": "vitest",
47
+ "test:coverage": "vitest run --coverage",
48
+ "lint": "eslint src/"
49
+ },
50
+ "dependencies": {
51
+ "@modelcontextprotocol/sdk": "^1.26.0",
52
+ "puppeteer": "^24.36.1",
53
+ "zod": "^3.25.76"
54
+ },
55
+ "devDependencies": {
56
+ "@types/node": "^22.0.0",
57
+ "@typescript-eslint/eslint-plugin": "^8.54.0",
58
+ "@typescript-eslint/parser": "^8.54.0",
59
+ "@vitest/coverage-v8": "^4.0.18",
60
+ "eslint": "^9.39.2",
61
+ "typescript": "^5.7.0",
62
+ "vitest": "^4.0.18"
63
+ }
64
+ }