mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-17 21:04:43 +00:00
fix(ui): 关闭服务器表单密码框的系统密码自动填充
系统对 NSSecureTextField 默认提供钥匙串/密码 App 的自动填充入口,且不 取决于字段语义:实测底层字段 contentType 已是 nil,既有 .textContentType(nil) 不足以关掉它,AppKit 也没有公开开关。 改用字段级私有开关 _setPasswordAutofillEnabled:(responds(to:) 守卫, 方法消失时静默回退、不崩溃)。PasswordAutofillOptOut 以同帧背景探针 认领承载密码控件的文本框,只关密码列自己;明文/密文切换换成新实例后 重新认领。 新增 PasswordAutofillOptOutTests 固定认领范围与切换行为;全量单测 1022 通过,人工验证通过。取舍与后果记入 ADR-0028。
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// 关掉系统给密码输入框的「密码自动填充」入口。
|
||||
///
|
||||
/// SwiftUI 的 `SecureField` 在 macOS 上由 `NSSecureTextField` 承载;系统对安全
|
||||
/// 输入框默认提供钥匙串/密码 App 的自动填充入口。`textContentType(nil)` 只清掉
|
||||
/// 字段语义(实测底层字段 `contentType` 已是 nil),关不掉入口;AppKit 没有公开
|
||||
/// 开关,唯一开关是字段级的私有 `_setPasswordAutofillEnabled:`(默认开)。这里在
|
||||
/// 探针认领到的文本框上按需调用它:方法不存在(系统换实现)时保持系统默认
|
||||
/// 行为,不做替代实现,也不让 KVC 抛异常。
|
||||
///
|
||||
/// 用法:贴在密码控件(`Group { TextField / SecureField }`)的 `.background(...)`
|
||||
/// 上。探针与控件同帧,据此在祖先里认领同帧的文本框,因此同一表单里的其他
|
||||
/// 输入框不会被误伤。
|
||||
struct PasswordAutofillOptOut: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> ProbeView {
|
||||
ProbeView()
|
||||
}
|
||||
|
||||
func updateNSView(_ view: ProbeView, context: Context) {
|
||||
view.disablePasswordAutofill()
|
||||
}
|
||||
|
||||
/// 背景探针:由 `.background` 撑满密码控件,据此认领承载控件的文本框。
|
||||
final class ProbeView: NSView {
|
||||
/// 已处理过的文本框:明文/密文切换会换成新实例,换实例即重新处理。
|
||||
private weak var claimedField: NSTextField?
|
||||
|
||||
private static let setterSelector = NSSelectorFromString("_setPasswordAutofillEnabled:")
|
||||
|
||||
override func viewDidMoveToWindow() {
|
||||
super.viewDidMoveToWindow()
|
||||
disablePasswordAutofill()
|
||||
}
|
||||
|
||||
override func setFrameSize(_ newSize: NSSize) {
|
||||
super.setFrameSize(newSize)
|
||||
disablePasswordAutofill()
|
||||
}
|
||||
|
||||
override func setFrameOrigin(_ newOrigin: NSPoint) {
|
||||
super.setFrameOrigin(newOrigin)
|
||||
disablePasswordAutofill()
|
||||
}
|
||||
|
||||
func disablePasswordAutofill() {
|
||||
let target = convert(bounds, to: nil)
|
||||
guard window != nil, !target.isEmpty else { return }
|
||||
guard let field = Self.field(overlapping: target, from: superview) else { return }
|
||||
guard field !== claimedField else { return }
|
||||
claimedField = field
|
||||
guard field.responds(to: Self.setterSelector) else { return }
|
||||
field.setValue(false, forKey: "passwordAutofillEnabled")
|
||||
}
|
||||
|
||||
/// 逐层向上找第一个含候选文本框的祖先视图。按交叠面积认领而不是逐边
|
||||
/// 相等:探针与控件同帧,取整差异不应该让认领落空。
|
||||
private static func field(overlapping target: NSRect, from view: NSView?) -> NSTextField? {
|
||||
var ancestor = view
|
||||
while let current = ancestor {
|
||||
if let match = bestMatch(in: current, target: target) { return match.field }
|
||||
ancestor = current.superview
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private static func bestMatch(
|
||||
in view: NSView, target: NSRect
|
||||
) -> (field: NSTextField, area: CGFloat)? {
|
||||
var best: (field: NSTextField, area: CGFloat)?
|
||||
if let field = view as? NSTextField, field.window != nil,
|
||||
let area = intersectionArea(of: field, with: target), area > 0
|
||||
{
|
||||
best = (field, area)
|
||||
}
|
||||
for subview in view.subviews {
|
||||
guard let candidate = bestMatch(in: subview, target: target) else { continue }
|
||||
if candidate.area > (best?.area ?? 0) { best = candidate }
|
||||
}
|
||||
return best
|
||||
}
|
||||
|
||||
private static func intersectionArea(of field: NSTextField, with target: NSRect) -> CGFloat? {
|
||||
let rect = field.convert(field.bounds, to: nil)
|
||||
let intersection = rect.intersection(target)
|
||||
guard !intersection.isNull else { return nil }
|
||||
return intersection.width * intersection.height
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -361,8 +361,10 @@ struct ServerFormFieldsGrid: View {
|
||||
Group {
|
||||
if fields.showPassword {
|
||||
TextField("密码", text: $fields.password)
|
||||
.background(PasswordAutofillOptOut())
|
||||
} else {
|
||||
SecureField("密码", text: $fields.password)
|
||||
.background(PasswordAutofillOptOut())
|
||||
}
|
||||
}
|
||||
.textFieldStyle(.roundedBorder)
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
import Testing
|
||||
|
||||
@testable import ShadowsocksX_NG2
|
||||
|
||||
/// 系统对安全输入框默认提供「密码自动填充」,`textContentType(nil)` 只清字段
|
||||
/// 语义、关不掉它;生产代码改字段级私有开关 `_setPasswordAutofillEnabled:`
|
||||
/// (`PasswordAutofillOptOut` 探针认领同帧文本框后经 KVC 写入)。
|
||||
///
|
||||
/// 这些用例固定认领范围:只关密码列自己的文本框,同一表单的其他输入框保持
|
||||
/// 系统默认。私有开关哪天消失,用例失败即提示重做这个缝,而不是静默回退。
|
||||
@MainActor
|
||||
struct PasswordAutofillOptOutTests {
|
||||
@Test
|
||||
func securePasswordFieldOptsOutWithoutTouchingNeighbours() async throws {
|
||||
let fields = ServerFormFields.newForm()
|
||||
let hosted = await Self.hostGrid(fields)
|
||||
|
||||
let textFields = Self.textFields(in: hosted.host)
|
||||
let secure = textFields.filter { $0 is NSSecureTextField }
|
||||
#expect(secure.count == 1, "密码列是全表单唯一的安全输入框")
|
||||
#expect(Self.disabledAutofillFields(in: hosted.host).count == 1, "只允许关掉密码列")
|
||||
#expect(Self.passwordAutofillEnabled(try #require(secure.first)) == false)
|
||||
#expect(
|
||||
textFields.filter { !($0 is NSSecureTextField) }
|
||||
.allSatisfy { Self.passwordAutofillEnabled($0) == true },
|
||||
"名称/服务器地址/端口等字段保持系统默认")
|
||||
}
|
||||
|
||||
@Test
|
||||
func visiblePasswordBranchOptsOutToo() async throws {
|
||||
let fields = ServerFormFields.newForm()
|
||||
fields.showPassword = true
|
||||
let hosted = await Self.hostGrid(fields)
|
||||
|
||||
let textFields = Self.textFields(in: hosted.host)
|
||||
#expect(textFields.allSatisfy { !($0 is NSSecureTextField) }, "明文分支不渲染安全输入框")
|
||||
#expect(Self.disabledAutofillFields(in: hosted.host).count == 1, "只允许关掉密码列")
|
||||
}
|
||||
|
||||
@Test
|
||||
func togglingPasswordVisibilityKeepsAutofillOff() async throws {
|
||||
let fields = ServerFormFields.newForm()
|
||||
let hosted = await Self.hostGrid(fields)
|
||||
#expect(Self.disabledAutofillFields(in: hosted.host).count == 1)
|
||||
|
||||
// 明文/密文切换会换掉承载的文本框,认领必须跟着新实例重做。
|
||||
fields.showPassword = true
|
||||
await Self.settle(hosted.host)
|
||||
#expect(Self.textFields(in: hosted.host).allSatisfy { !($0 is NSSecureTextField) })
|
||||
#expect(Self.disabledAutofillFields(in: hosted.host).count == 1)
|
||||
|
||||
fields.showPassword = false
|
||||
await Self.settle(hosted.host)
|
||||
#expect(Self.textFields(in: hosted.host).filter { $0 is NSSecureTextField }.count == 1)
|
||||
#expect(Self.disabledAutofillFields(in: hosted.host).count == 1)
|
||||
}
|
||||
|
||||
// MARK: 夹具
|
||||
|
||||
/// 复用生产表单:探针、栅格与字段实现都在真实布局里跑一遍。
|
||||
private struct GridHarness: View {
|
||||
@ObservedObject var fields: ServerFormFields
|
||||
@FocusState private var fieldFocus: ServerFormField?
|
||||
|
||||
var body: some View {
|
||||
ServerFormFieldsGrid(
|
||||
fields: fields, plugin: nil, isEditable: true, fieldFocus: $fieldFocus)
|
||||
}
|
||||
}
|
||||
|
||||
/// 探针在窗口坐标里认领文本框:无窗口的 hosting view 不构成呈现,认领会落空,
|
||||
/// 因此夹具必须给一个离屏窗口(不呈现、不抢 key)。
|
||||
private static func hostGrid(
|
||||
_ fields: ServerFormFields
|
||||
) async -> (window: NSWindow, host: NSHostingView<GridHarness>) {
|
||||
let host = NSHostingView(rootView: GridHarness(fields: fields))
|
||||
host.frame = NSRect(x: 0, y: 0, width: 560, height: 720)
|
||||
let window = NSWindow(
|
||||
contentRect: NSRect(x: 0, y: 0, width: 560, height: 720),
|
||||
styleMask: [.titled], backing: .buffered, defer: false)
|
||||
window.contentView = host
|
||||
await settle(host)
|
||||
return (window, host)
|
||||
}
|
||||
|
||||
/// 让 SwiftUI 的渲染事务与探针的布局回调都跑完再断言。
|
||||
private static func settle(_ host: NSHostingView<GridHarness>) async {
|
||||
for _ in 0..<2 {
|
||||
await Task { @MainActor in }.value
|
||||
host.layoutSubtreeIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
private static func textFields(in view: NSView) -> [NSTextField] {
|
||||
if let field = view as? NSTextField { return [field] + view.subviews.flatMap(textFields) }
|
||||
return view.subviews.flatMap(textFields)
|
||||
}
|
||||
|
||||
private static func disabledAutofillFields(in view: NSView) -> [NSTextField] {
|
||||
textFields(in: view).filter { passwordAutofillEnabled($0) == false }
|
||||
}
|
||||
|
||||
/// BOOL 返回值不能走 `perform`(会被当成对象返回值),只能取 IMP 直调。
|
||||
private static func passwordAutofillEnabled(_ field: NSTextField) -> Bool? {
|
||||
let selector = NSSelectorFromString("_isPasswordAutofillEnabled")
|
||||
guard let method = class_getInstanceMethod(type(of: field), selector) else { return nil }
|
||||
typealias Getter = @convention(c) (AnyObject, Selector) -> Bool
|
||||
return unsafeBitCast(method_getImplementation(method), to: Getter.self)(field, selector)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
# 服务器表单密码字段关掉系统密码自动填充
|
||||
|
||||
Status: Accepted (2026-10-05)
|
||||
|
||||
服务器表单(新建表单与详情页表单共用同一栅格)的密码字段关掉 macOS 的密码自动填充入口:`PasswordAutofillOptOut` 背景探针按同帧认领承载密码控件的 `NSTextField`,再写字段级私有开关 `_setPasswordAutofillEnabled:`(KVC 键 `passwordAutofillEnabled`,`responds(to:)` 守卫)。
|
||||
|
||||
系统对 `NSSecureTextField` 默认提供钥匙串/密码 App 的自动填充入口,且不取决于字段语义:实测底层字段 `contentType` 已是 `nil`(既有 `.textContentType(nil)` 确实生效),入口照旧出现。AppKit 没有公开开关,唯一可用的是这个私有字段位。
|
||||
|
||||
## Considered options
|
||||
|
||||
- 只用 `.textContentType(nil)` 或改语义类型(`.oneTimeCode`):前者已实测无效;后者把密码框谎报成一次性验证码字段,语义错误,且会引入空的自动填充弹层。
|
||||
- 在密码框前放一个不可见的安全输入框吸收入口(纯公开 API):要自己维护隐藏字段的焦点与制表顺序,还依赖「系统只给窗口里第一个安全输入框加入口」这条未文档化行为,比私有开关更脆。
|
||||
- 自绘掩码输入框(普通 `NSTextField` 加自绘圆点):零私有 API,但失去安全输入(键盘记录防护),光标、选区与输入法都要重做,代价大于收益。
|
||||
- 维持现状:入口一直出现在新建服务器表单里(详情页只因字段只读或已预填,看起来没有该入口)。
|
||||
|
||||
## Consequences
|
||||
|
||||
- 这是本仓库第一处私有 API。Apple 换实现(方法消失)时探针静默回退到系统默认行为、不会崩溃;`PasswordAutofillOptOutTests` 会失败,提示重做这个缝。
|
||||
- 关闭范围是字段级而非全局:同一表单的名称、服务器地址、端口等字段保持系统默认,新建表单与详情页表单共用这一份实现。
|
||||
- 明文/密文切换会换掉承载的文本框,认领必须跟着新实例重做;该行为已由用例固定。
|
||||
Reference in New Issue
Block a user