u-chan-secrets 3.0.2 → 4.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.
@@ -1,33 +1,33 @@
1
1
  export function buildJxaManagementScript(req) {
2
- return `ObjC.import('Cocoa');
3
- ObjC.import('stdlib');
4
- var alert = $.NSAlert.alloc.init;
5
- alert.messageText = ${JSON.stringify(req.title)};
6
- alert.informativeText = ${JSON.stringify('Phrase: ' + req.phrase)};
7
- ['Refresh','Previous','Next','Lock','Unlock','Revoke selected','Close'].forEach(function(t){ alert.addButtonWithTitle(t); });
8
- var view = $.NSView.alloc.initWithFrame($.NSMakeRect(0,0,640,420));
9
- var scroll = $.NSScrollView.alloc.initWithFrame($.NSMakeRect(0,50,640,370));
10
- scroll.hasVerticalScroller = true;
11
- var text = $.NSTextView.alloc.initWithFrame($.NSMakeRect(0,0,620,370));
12
- text.editable = false;
13
- text.string = ${JSON.stringify(req.lines.join('\n'))};
14
- scroll.documentView = text;
15
- view.addSubview(scroll);
16
- var domains = ${JSON.stringify(req.domains ?? [])};
17
- var select = $.NSPopUpButton.alloc.initWithFramePullsDown($.NSMakeRect(0,5,640,32),false);
18
- domains.forEach(function(d){ select.addItemWithTitle(d); });
19
- view.addSubview(select);
20
- alert.accessoryView = view;
21
- var response = Number(alert.runModal) - 1000;
22
- var actions = ['refresh','previous','next','lock','unlock','revoke','close'];
23
- var result = {action: actions[response] || 'close'};
24
- if (result.action === 'revoke') {
25
- var index = Number(select.indexOfSelectedItem);
26
- if (index < 0 || index >= domains.length) result.action = 'refresh';
27
- else result.domain = domains[index];
28
- }
29
- $.NSFileHandle.fileHandleWithStandardOutput.writeData(
30
- $.NSString.alloc.initWithString(JSON.stringify(result)).dataUsingEncoding($.NSUTF8StringEncoding));
31
- $.exit(0);
2
+ return `ObjC.import('Cocoa');
3
+ ObjC.import('stdlib');
4
+ var alert = $.NSAlert.alloc.init;
5
+ alert.messageText = ${JSON.stringify(req.title)};
6
+ alert.informativeText = ${JSON.stringify('Phrase: ' + req.phrase)};
7
+ ['Refresh','Previous','Next','Lock','Unlock','Revoke selected','Close'].forEach(function(t){ alert.addButtonWithTitle(t); });
8
+ var view = $.NSView.alloc.initWithFrame($.NSMakeRect(0,0,640,420));
9
+ var scroll = $.NSScrollView.alloc.initWithFrame($.NSMakeRect(0,50,640,370));
10
+ scroll.hasVerticalScroller = true;
11
+ var text = $.NSTextView.alloc.initWithFrame($.NSMakeRect(0,0,620,370));
12
+ text.editable = false;
13
+ text.string = ${JSON.stringify(req.lines.join('\n'))};
14
+ scroll.documentView = text;
15
+ view.addSubview(scroll);
16
+ var domains = ${JSON.stringify(req.domains ?? [])};
17
+ var select = $.NSPopUpButton.alloc.initWithFramePullsDown($.NSMakeRect(0,5,640,32),false);
18
+ domains.forEach(function(d){ select.addItemWithTitle(d); });
19
+ view.addSubview(select);
20
+ alert.accessoryView = view;
21
+ var response = Number(alert.runModal) - 1000;
22
+ var actions = ['refresh','previous','next','lock','unlock','revoke','close'];
23
+ var result = {action: actions[response] || 'close'};
24
+ if (result.action === 'revoke') {
25
+ var index = Number(select.indexOfSelectedItem);
26
+ if (index < 0 || index >= domains.length) result.action = 'refresh';
27
+ else result.domain = domains[index];
28
+ }
29
+ $.NSFileHandle.fileHandleWithStandardOutput.writeData(
30
+ $.NSString.alloc.initWithString(JSON.stringify(result)).dataUsingEncoding($.NSUTF8StringEncoding));
31
+ $.exit(0);
32
32
  `;
33
33
  }
@@ -1,191 +1,191 @@
1
- export const AUTH_HELPER_CSHARP = `using System;
2
- using System.Diagnostics;
3
- using System.Runtime.InteropServices;
4
- using System.Security.Principal;
5
-
6
- namespace UChan.Platform.Windows.Assets
7
- {
8
- public static class AuthHelper
9
- {
10
- private const uint PROCESS_QUERY_INFORMATION = 0x0400;
11
- private const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
12
- private const uint PROCESS_DUP_HANDLE = 0x0040;
13
- private const uint TOKEN_QUERY = 0x0008;
14
- private const uint DUPLICATE_SAME_ACCESS = 0x0002;
15
-
16
- [DllImport("kernel32.dll", SetLastError = true)]
17
- private static extern IntPtr GetCurrentProcess();
18
-
19
- [DllImport("kernel32.dll", SetLastError = true)]
20
- private static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, uint processId);
21
-
22
- [DllImport("kernel32.dll", SetLastError = true)]
23
- private static extern bool DuplicateHandle(
24
- IntPtr hSourceProcessHandle,
25
- IntPtr hSourceHandle,
26
- IntPtr hTargetProcessHandle,
27
- out IntPtr lpTargetHandle,
28
- uint dwDesiredAccess,
29
- bool bInheritHandle,
30
- uint dwOptions);
31
-
32
- [DllImport("kernel32.dll", SetLastError = true)]
33
- private static extern bool GetNamedPipeClientProcessId(IntPtr pipeHandle, out uint clientProcessId);
34
-
35
- [DllImport("advapi32.dll", SetLastError = true)]
36
- private static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle);
37
-
38
- [DllImport("kernel32.dll", SetLastError = true)]
39
- private static extern bool CloseHandle(IntPtr hObject);
40
-
41
- private static string GetProcessUserSid(uint pid)
42
- {
43
- IntPtr hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
44
- if (hProcess == IntPtr.Zero)
45
- {
46
- hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid);
47
- }
48
- if (hProcess == IntPtr.Zero) return null;
49
-
50
- try
51
- {
52
- IntPtr hToken;
53
- if (!OpenProcessToken(hProcess, TOKEN_QUERY, out hToken))
54
- {
55
- return null;
56
- }
57
-
58
- try
59
- {
60
- using (WindowsIdentity identity = new WindowsIdentity(hToken))
61
- {
62
- if (identity.User != null)
63
- {
64
- return identity.User.Value;
65
- }
66
- }
67
- }
68
- finally
69
- {
70
- CloseHandle(hToken);
71
- }
72
- }
73
- finally
74
- {
75
- CloseHandle(hProcess);
76
- }
77
- return null;
78
- }
79
-
80
- public static int Main(string[] args)
81
- {
82
- if (args.Length < 1)
83
- {
84
- Console.Error.WriteLine("Usage: auth-helper <serverPid> [targetSid]");
85
- return 1;
86
- }
87
-
88
- uint serverPid;
89
- if (!uint.TryParse(args[0], out serverPid))
90
- {
91
- Console.Error.WriteLine("Invalid server PID");
92
- return 1;
93
- }
94
-
95
- string targetSid = args.Length > 1 ? args[1] : null;
96
- bool hasTargetSid = !string.IsNullOrEmpty(targetSid) && !string.Equals(targetSid, "dummy", StringComparison.OrdinalIgnoreCase);
97
-
98
- long pipeHandle = 0;
99
- if (args.Length > 2)
100
- {
101
- long.TryParse(args[2], out pipeHandle);
102
- }
103
-
104
- string matchedSid = null;
105
- string fallbackNonSystemSid = null;
106
- string anySid = null;
107
-
108
- IntPtr hServer = OpenProcess(PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION, false, serverPid);
109
- if (hServer != IntPtr.Zero)
110
- {
111
- try
112
- {
113
- // Exact handle of this connection when the server passes it; otherwise scan.
114
- int start = pipeHandle > 0 ? (int)pipeHandle : 4;
115
- int end = pipeHandle > 0 ? (int)pipeHandle + 4 : 0x10000;
116
- for (int h = start; h < end; h += 4)
117
- {
118
- IntPtr hLocal;
119
- if (DuplicateHandle(hServer, (IntPtr)h, GetCurrentProcess(), out hLocal, 0, false, DUPLICATE_SAME_ACCESS))
120
- {
121
- try
122
- {
123
- uint candidatePid;
124
- if (GetNamedPipeClientProcessId(hLocal, out candidatePid) && candidatePid != 0 && candidatePid != serverPid)
125
- {
126
- string sid = GetProcessUserSid(candidatePid);
127
- if (!string.IsNullOrEmpty(sid))
128
- {
129
- if (hasTargetSid && string.Equals(sid, targetSid, StringComparison.OrdinalIgnoreCase))
130
- {
131
- matchedSid = sid;
132
- break;
133
- }
134
-
135
- if (!string.Equals(sid, "S-1-5-18", StringComparison.OrdinalIgnoreCase))
136
- {
137
- fallbackNonSystemSid = sid;
138
- }
139
-
140
- if (anySid == null)
141
- {
142
- anySid = sid;
143
- }
144
- }
145
- }
146
- }
147
- finally
148
- {
149
- CloseHandle(hLocal);
150
- }
151
- }
152
- }
153
- }
154
- finally
155
- {
156
- CloseHandle(hServer);
157
- }
158
- }
159
-
160
- string clientSid = matchedSid ?? fallbackNonSystemSid ?? anySid;
161
-
162
- // Fail closed: if clientSid could not be identified on any pipe handle, reject
163
- if (string.IsNullOrEmpty(clientSid))
164
- {
165
- Console.Error.WriteLine("Could not identify client process on pipe");
166
- return 1;
167
- }
168
-
169
- // Verify string sid parses as SecurityIdentifier
170
- try
171
- {
172
- SecurityIdentifier secId = new SecurityIdentifier(clientSid);
173
- }
174
- catch
175
- {
176
- Console.Error.WriteLine("Resolved SID is invalid");
177
- return 1;
178
- }
179
-
180
- if (hasTargetSid && !string.Equals(clientSid, targetSid, StringComparison.OrdinalIgnoreCase))
181
- {
182
- Console.Error.WriteLine("SID mismatch: expected " + targetSid + " but got " + clientSid);
183
- return 1;
184
- }
185
-
186
- Console.WriteLine("AUTHORIZED:" + clientSid);
187
- return 0;
188
- }
189
- }
190
- }
1
+ export const AUTH_HELPER_CSHARP = `using System;
2
+ using System.Diagnostics;
3
+ using System.Runtime.InteropServices;
4
+ using System.Security.Principal;
5
+
6
+ namespace UChan.Platform.Windows.Assets
7
+ {
8
+ public static class AuthHelper
9
+ {
10
+ private const uint PROCESS_QUERY_INFORMATION = 0x0400;
11
+ private const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
12
+ private const uint PROCESS_DUP_HANDLE = 0x0040;
13
+ private const uint TOKEN_QUERY = 0x0008;
14
+ private const uint DUPLICATE_SAME_ACCESS = 0x0002;
15
+
16
+ [DllImport("kernel32.dll", SetLastError = true)]
17
+ private static extern IntPtr GetCurrentProcess();
18
+
19
+ [DllImport("kernel32.dll", SetLastError = true)]
20
+ private static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, uint processId);
21
+
22
+ [DllImport("kernel32.dll", SetLastError = true)]
23
+ private static extern bool DuplicateHandle(
24
+ IntPtr hSourceProcessHandle,
25
+ IntPtr hSourceHandle,
26
+ IntPtr hTargetProcessHandle,
27
+ out IntPtr lpTargetHandle,
28
+ uint dwDesiredAccess,
29
+ bool bInheritHandle,
30
+ uint dwOptions);
31
+
32
+ [DllImport("kernel32.dll", SetLastError = true)]
33
+ private static extern bool GetNamedPipeClientProcessId(IntPtr pipeHandle, out uint clientProcessId);
34
+
35
+ [DllImport("advapi32.dll", SetLastError = true)]
36
+ private static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle);
37
+
38
+ [DllImport("kernel32.dll", SetLastError = true)]
39
+ private static extern bool CloseHandle(IntPtr hObject);
40
+
41
+ private static string GetProcessUserSid(uint pid)
42
+ {
43
+ IntPtr hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid);
44
+ if (hProcess == IntPtr.Zero)
45
+ {
46
+ hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid);
47
+ }
48
+ if (hProcess == IntPtr.Zero) return null;
49
+
50
+ try
51
+ {
52
+ IntPtr hToken;
53
+ if (!OpenProcessToken(hProcess, TOKEN_QUERY, out hToken))
54
+ {
55
+ return null;
56
+ }
57
+
58
+ try
59
+ {
60
+ using (WindowsIdentity identity = new WindowsIdentity(hToken))
61
+ {
62
+ if (identity.User != null)
63
+ {
64
+ return identity.User.Value;
65
+ }
66
+ }
67
+ }
68
+ finally
69
+ {
70
+ CloseHandle(hToken);
71
+ }
72
+ }
73
+ finally
74
+ {
75
+ CloseHandle(hProcess);
76
+ }
77
+ return null;
78
+ }
79
+
80
+ public static int Main(string[] args)
81
+ {
82
+ if (args.Length < 1)
83
+ {
84
+ Console.Error.WriteLine("Usage: auth-helper <serverPid> [targetSid]");
85
+ return 1;
86
+ }
87
+
88
+ uint serverPid;
89
+ if (!uint.TryParse(args[0], out serverPid))
90
+ {
91
+ Console.Error.WriteLine("Invalid server PID");
92
+ return 1;
93
+ }
94
+
95
+ string targetSid = args.Length > 1 ? args[1] : null;
96
+ bool hasTargetSid = !string.IsNullOrEmpty(targetSid) && !string.Equals(targetSid, "dummy", StringComparison.OrdinalIgnoreCase);
97
+
98
+ long pipeHandle = 0;
99
+ if (args.Length > 2)
100
+ {
101
+ long.TryParse(args[2], out pipeHandle);
102
+ }
103
+
104
+ string matchedSid = null;
105
+ string fallbackNonSystemSid = null;
106
+ string anySid = null;
107
+
108
+ IntPtr hServer = OpenProcess(PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION, false, serverPid);
109
+ if (hServer != IntPtr.Zero)
110
+ {
111
+ try
112
+ {
113
+ // Exact handle of this connection when the server passes it; otherwise scan.
114
+ int start = pipeHandle > 0 ? (int)pipeHandle : 4;
115
+ int end = pipeHandle > 0 ? (int)pipeHandle + 4 : 0x10000;
116
+ for (int h = start; h < end; h += 4)
117
+ {
118
+ IntPtr hLocal;
119
+ if (DuplicateHandle(hServer, (IntPtr)h, GetCurrentProcess(), out hLocal, 0, false, DUPLICATE_SAME_ACCESS))
120
+ {
121
+ try
122
+ {
123
+ uint candidatePid;
124
+ if (GetNamedPipeClientProcessId(hLocal, out candidatePid) && candidatePid != 0 && candidatePid != serverPid)
125
+ {
126
+ string sid = GetProcessUserSid(candidatePid);
127
+ if (!string.IsNullOrEmpty(sid))
128
+ {
129
+ if (hasTargetSid && string.Equals(sid, targetSid, StringComparison.OrdinalIgnoreCase))
130
+ {
131
+ matchedSid = sid;
132
+ break;
133
+ }
134
+
135
+ if (!string.Equals(sid, "S-1-5-18", StringComparison.OrdinalIgnoreCase))
136
+ {
137
+ fallbackNonSystemSid = sid;
138
+ }
139
+
140
+ if (anySid == null)
141
+ {
142
+ anySid = sid;
143
+ }
144
+ }
145
+ }
146
+ }
147
+ finally
148
+ {
149
+ CloseHandle(hLocal);
150
+ }
151
+ }
152
+ }
153
+ }
154
+ finally
155
+ {
156
+ CloseHandle(hServer);
157
+ }
158
+ }
159
+
160
+ string clientSid = matchedSid ?? fallbackNonSystemSid ?? anySid;
161
+
162
+ // Fail closed: if clientSid could not be identified on any pipe handle, reject
163
+ if (string.IsNullOrEmpty(clientSid))
164
+ {
165
+ Console.Error.WriteLine("Could not identify client process on pipe");
166
+ return 1;
167
+ }
168
+
169
+ // Verify string sid parses as SecurityIdentifier
170
+ try
171
+ {
172
+ SecurityIdentifier secId = new SecurityIdentifier(clientSid);
173
+ }
174
+ catch
175
+ {
176
+ Console.Error.WriteLine("Resolved SID is invalid");
177
+ return 1;
178
+ }
179
+
180
+ if (hasTargetSid && !string.Equals(clientSid, targetSid, StringComparison.OrdinalIgnoreCase))
181
+ {
182
+ Console.Error.WriteLine("SID mismatch: expected " + targetSid + " but got " + clientSid);
183
+ return 1;
184
+ }
185
+
186
+ Console.WriteLine("AUTHORIZED:" + clientSid);
187
+ return 0;
188
+ }
189
+ }
190
+ }
191
191
  `;