This commit is contained in:
commit
ad0f04b1d4
6 files changed
+54
-15
No files matched your search
@@ -21,7 +21,7 @@ class EpayProbe
|
||||
if(($fields['out_trade_no']??null)!==$tradeNo||!is_string($fields['sign']??null)||!preg_match('/\A[0-9a-f]{32}\z/i',$fields['sign'])||!is_scalar($fields['money']??null)||(int)round((float)$fields['money']*100)!==$amount)throw new RuntimeException('invalid_probe_payload');
|
||||
$method='POST';$type=$p->config['type'];
|
||||
for($hop=0;$hop<4;$hop++){
|
||||
$r=$this->transport->request($method,$url,$fields);$status=$r['status'];$body=$r['body'];
|
||||
$r=$this->transport->request($method,$url,$fields,$hop>0);$status=$r['status'];$body=$r['body'];
|
||||
$meta=['http'=>$status,'host'=>$r['host'],'body_sha256'=>$r['sha256']];
|
||||
if($status===404||$status===410)return $this->result('hard_failure','cashier_not_found',$meta);
|
||||
if($status>=500||$status===408||$status===429)return $this->result('soft_failure','cashier_transient',$meta);
|
||||
|
||||
@@ -6,23 +6,33 @@ use RuntimeException;
|
||||
|
||||
class HttpsTransport
|
||||
{
|
||||
public function request(string $method,string $url,array $fields=[]): array
|
||||
public function request(string $method,string $url,array $fields=[],bool $downstream=false): array
|
||||
{
|
||||
$parts=parse_url($url);$host=strtolower($parts['host']??'');
|
||||
if (($parts['scheme']??'')!=='https'||!$host||isset($parts['user'])||isset($parts['pass'])||isset($parts['fragment'])||($parts['port']??443)!==443) throw new RuntimeException('unsafe_url');
|
||||
if(!in_array($host,config('payment_health.allowed_hosts',[]),true)) throw new RuntimeException('unapproved_host');
|
||||
$ip=$this->publicAddress($host);
|
||||
[$host,$ip]=$this->endpoint($url,$downstream);
|
||||
$client=Http::withoutRedirecting()->connectTimeout(10)->timeout(25)->withUserAgent('XB-PaymentHealth/1.0')
|
||||
->withOptions(['verify'=>true,'curl'=>[CURLOPT_RESOLVE=>[$host.':443:'.(str_contains($ip,':')?'['.$ip.']':$ip)]]]);
|
||||
$response=$method==='POST'?$client->asForm()->post($url,$fields):$client->get($url);
|
||||
$body=$response->body();if(strlen($body)>2097152)throw new RuntimeException('oversized_cashier_response');
|
||||
return ['status'=>$response->status(),'body'=>$body,'location'=>$response->header('Location'),'host'=>$host,'sha256'=>hash('sha256',$body)];
|
||||
}
|
||||
protected function endpoint(string $url,bool $downstream):array
|
||||
{
|
||||
$parts=parse_url($url);$host=strtolower($parts['host']??'');
|
||||
if (($parts['scheme']??'')!=='https'||!$host||isset($parts['user'])||isset($parts['pass'])||isset($parts['fragment'])||($parts['port']??443)!==443) throw new RuntimeException('unsafe_url');
|
||||
if(filter_var(trim($host,'[]'),FILTER_VALIDATE_IP))throw new RuntimeException('ip_literal_not_allowed');
|
||||
if(!preg_match('/\A(?=.{1,253}\z)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}\z/D',$host))throw new RuntimeException('unsafe_url');
|
||||
// Only EpayProbe may mark a URL as a handoff after a validated gateway response.
|
||||
if(!in_array($host,config('payment_health.allowed_hosts',[]),true)&&!($downstream&&config('payment_health.dynamic_handoffs',true)))throw new RuntimeException('unapproved_host');
|
||||
return [$host,$this->publicAddress($host)];
|
||||
}
|
||||
protected function records(string $host):array
|
||||
{return dns_get_record($host,DNS_A|DNS_AAAA)?:[];}
|
||||
protected function publicAddress(string $host): string
|
||||
{
|
||||
if(filter_var($host,FILTER_VALIDATE_IP)) throw new RuntimeException('ip_literal_not_allowed');
|
||||
$records=dns_get_record($host,DNS_A|DNS_AAAA);$ips=[];
|
||||
$records=$this->records($host);$ips=[];
|
||||
foreach($records?:[] as $r){$ip=$r['ip']??$r['ipv6']??null;if(!$ip)continue;
|
||||
if(str_starts_with(strtolower($ip),'::ffff:'))throw new RuntimeException('nonpublic_dns');
|
||||
if(!filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_NO_PRIV_RANGE|FILTER_FLAG_NO_RES_RANGE))throw new RuntimeException('nonpublic_dns');$ips[]=$ip;}
|
||||
if(!$ips)throw new RuntimeException('dns_unavailable');
|
||||
return $ips[0];
|
||||
|
||||
@@ -23,7 +23,7 @@ class Monitor
|
||||
foreach(Payment::orderBy('id')->get()as$p){
|
||||
if(!$this->probe->supports($p)||in_array($p->id,config('payment_health.excluded_ids',[]),true)){$results[]=['id'=>$p->id,'status'=>'unsupported_or_excluded'];continue;}
|
||||
$id=(string)$p->uuid;$revision=$this->revision($p);$old=$state['channels'][$id]??[];
|
||||
$policy=hash('sha256',json_encode(config('payment_health.allowed_hosts',[])));
|
||||
$policy=hash('sha256',json_encode(['handoff_v2',config('payment_health.allowed_hosts',[]),(bool)config('payment_health.dynamic_handoffs',true)]));
|
||||
if(($old['status']??null)==='review_required'&&($old['policy']??null)!==$policy)$old['status']='policy_changed';
|
||||
if(($old['revision']??null)!==$revision)$old=['slot'=>$old['slot']??null];
|
||||
$lease=Ownership::lease($p);
|
||||
|
||||
@@ -8,7 +8,8 @@ return [
|
||||
'daily_probe_limit' => 8,
|
||||
'soft_failure_threshold' => 2,
|
||||
'state_path' => storage_path('app/private/payment-health/state.json'),
|
||||
// New downstream hosts require an explicit operator registration.
|
||||
// Gateway entry points stay pinned; HTTPS handoffs returned by them may be dynamic.
|
||||
'dynamic_handoffs' => env('PAYMENT_HEALTH_DYNAMIC_HANDOFFS', true),
|
||||
'allowed_hosts' => array_values(array_filter(array_map('trim', explode(',', env('PAYMENT_HEALTH_ALLOWED_HOSTS', 'pay.airspay.top,q.orltjrp.cn,api.shangri-la.vc'))))),
|
||||
'excluded_ids' => array_values(array_filter(array_map('intval', explode(',', env('PAYMENT_HEALTH_EXCLUDED_IDS', ''))))),
|
||||
'telehub_url' => env('PAYMENT_HEALTH_TELEHUB_URL', 'https://telehub.gemcook.net'),
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
|
||||
每通道每6小时槽最多一次,即使进程中断、同轮修改配置或重复执行也不补单;每实例每日最多8次探测。每次金额32元,只通过原PaymentService/EPay插件生成独立PH前缀的合成待支付请求;不创建应用用户或订单,不扫码、不付款。第三方仍会保留待支付记录。该合成订单不在应用订单表,任何意外回调仍须通过原真实订单查找/验证,不能开通权益。未知结果不自动重发同笔请求。
|
||||
|
||||
只跟随配置中精确批准的HTTPS主机,校验证书并拒绝私网/保留IP、IP字面量、用户信息、非443端口和未知下游。初始请求使用POST正文,不将商户签名挂在初始URL;不输出密钥、签名、二维码、用户数据或探测完整链接。已批准默认主机为pay.airspay.top、q.orltjrp.cn、api.shangri-la.vc;新主机停止并标记review_required,必须登记后才恢复探测。未适配、缺配置及排除通道明确跳过,不误关。
|
||||
初始网关仍须在PAYMENT_HEALTH_ALLOWED_HOSTS中精确登记。用户2026-10-30明确要求落实动态下游:默认PAYMENT_HEALTH_DYNAMIC_HANDOFFS=true,从已验证网关响应中解析的HTTP重定向、HTML表单或已支持的JS跳转,可跟随动态HTTPS收银域名,无需逐域名批准。每跳重新校验证书、域名格式与DNS公网地址,拒绝HTTP、IP字面量、私网/保留及IPv4映射IPv6地址、用户信息、非443端口和片段,最多4次请求;DNS校验后用CURLOPT_RESOLVE固定本次连接地址,自动重定向始终关闭。新域名不自动成为后续任务的可信入口,也不自动加入任何持久白名单。可将动态开关设false回到严格名单模式。
|
||||
|
||||
初始请求使用POST正文,不将商户签名挂在初始URL;不输出密钥、签名、二维码、用户数据或探测完整链接。允许名单原值pay.airspay.top、q.orltjrp.cn、api.shangri-la.vc保持兼容,当前配置实际入口为pay.airspay.top。未适配、缺配置及排除通道明确跳过,不误关。规则策略版本变化允许旧unapproved_host状态在下一槽重新检测,但保留本轮claim与每日预算,不同槽前不补单。其它不安全目标仍进入review_required,不降低HTTPS或内网隔离规则。
|
||||
|
||||
网关200不代表成功。最终页须无已知业务错误、金额匹配,并具备支付URI及二维码渲染证据;PNG路径还检查真实QR三个定位图形,普通方图不算。确认404/410或已知商户/通道/过期错误立即停用;网络超时、DNS失败或5xx连续两轮才停用;普通无二维码200为不确定,告警但不擅自启用。检测只证明当时收银展示可用,不证明实际到账。
|
||||
|
||||
@@ -24,7 +26,7 @@
|
||||
|
||||
事件正文与稳定幂等键一并持久化;故障error、恢复notification,目标base。各实例独立TeleHub调用凭据,仅授权这两类和base,服务地址/Key分别为PAYMENT_HEALTH_TELEHUB_URL与PAYMENT_HEALTH_TELEHUB_KEY,Key只在服务器.env及私有备份。请求超时用原幂等键重试,不新建消息;已接收则查询本事件UUID回执。sent/failed/uncertain/cancelled终态不盲目补发,接收/排队不算Telegram已确认。持续同一故障不刷屏,首次正常检查静默。
|
||||
|
||||
可配置`PAYMENT_HEALTH_EXCLUDED_IDS`及完整`PAYMENT_HEALTH_ALLOWED_HOSTS`。DomainHub已评估:XB当前未接收其配置快照,本轮使用实例.env中精确登记地址,不另建后台或声称已接入DomainHub。
|
||||
可配置`PAYMENT_HEALTH_EXCLUDED_IDS`、入口`PAYMENT_HEALTH_ALLOWED_HOSTS`及`PAYMENT_HEALTH_DYNAMIC_HANDOFFS`。DomainHub已评估:XB当前未接收其配置快照,本轮使用实例.env中精确登记地址,不另建后台或声称已接入DomainHub。
|
||||
|
||||
## 验证与发布
|
||||
|
||||
@@ -40,7 +42,7 @@
|
||||
|
||||
功能提交XB `278baa4796f004d77ae03945d49072f4b395aeb9`,主分支合并 `625b6820112cf2a8bad6a72c14a3402d6c8b02af`;HN独立分支同功能提交 `2621df96e0d56f8af892c082c6a0ac103a72ad30`。均已经白色机Git推送,sitejp02两站分别快进到上述功能提交。DPL/HN均已配置检测与自动控制为true,读取现有项目scheduler执行北京时间00/06/12/18点调度;未新增白色机业务调度。未来衍生实例部署此版本后默认开启,仍须配置自己的告警凭据;本轮没有部署FUC或未知实例。
|
||||
|
||||
首轮在2026-10-30第2槽(12:00—18:00)各发出支付宝/微信32元探测各一笔。两站微信下游api.shangri-la.vc返回404,均自动关闭ID2且SQL归属有效;支付宝遇到未批准下游返回review_required,停止跟随及后续探测,ID1保持启用,不能声称支付宝本轮收银验证通过。当前结果不授权新增下游;不能通过扩大允许列表、删除状态或重新发单绕过批准。旧支付宝二维码成功仅是历史证据。
|
||||
首轮在2026-10-30第2槽(12:00—18:00)各发出支付宝/微信32元探测各一笔。两站微信下游api.shangri-la.vc返回404,均自动关闭ID2且SQL归属有效;支付宝遇到未批准下游返回review_required,停止跟随及后续探测,ID1保持启用,不能声称支付宝本轮收银验证通过。这是动态下游调整前的历史首轮结果;后续用户已明确批准动态HTTPS下游策略,仍不得删除状态或同槽重新发单。旧支付宝二维码成功仅是历史证据。
|
||||
|
||||
TeleHub已分别建立最小权限调用方:DPL xb-dpl-payment-health-prod(ID8)、HN xb-hn-payment-health-prod(ID9),只允许error/notification与base,凭据仅在各站私有.env及root私有备份。首轮各两条异常消息均查询到sent,共4条真实告警已送达;没有改全局投递开关。恢复消息代码已隔离验证,尚无真实通道恢复事件。
|
||||
|
||||
@@ -49,3 +51,7 @@ TeleHub已分别建立最小权限调用方:DPL xb-dpl-payment-health-prod(I
|
||||
备份与服务器收据:sitejp02 `/root/codex-backups/xb-payment-health/20261030/`,每站子目录含before.json、v2_payment/v2_settings变更前SQL、配置缓存备份、applied.json、acceptance.json和回调验证收据;.env变更前及TeleHub私有记录位于同一受保护根目录。保留正在使用的state.json,不覆盖业务表恢复。
|
||||
|
||||
首次部署因root严格umask使新增源码不可读,在首轮请求前停止;已仅修复本次app/config/tests/docs源码文件0644及父目录0755。后续Git检出使用022,秘密和备份保持私有;配置在独立候选APP_CONFIG_CACHE生成,验证后原子替换并赋予各站运行用户0640。仅通过对应FPM Unix socket定向失效本次PHP文件与配置的OPcache,未重启共享FPM或更改网络。成功收据applied.json优先于原失败留档failed.txt。
|
||||
|
||||
## 动态下游调整(2026-10-30)
|
||||
|
||||
用户指出支付商收银地址动态分配,并明确要求“调整,落实”。本次共用修改仅涉及HttpsTransport、EpayProbe、Monitor的策略指纹及payment_health配置;应用到HN/DPL,默认允许可信网关链上的动态HTTPS收银域名,不扩大为任意初始入口或内网访问,不改商户参数、订单、模型钩子和回调验签。先隔离测试动态表单/重定向、入口名单、HTTPS/私网限制、同槽不重复及下一槽解锁,再Git发布。回退设置PAYMENT_HEALTH_DYNAMIC_HANDOFFS=false并原子刷新缓存,保留原账本;回退代码同样不删除claim。真实检测沿既定6小时槽运行,不额外补单或实际付款。
|
||||
@@ -25,9 +25,10 @@ class FakeReporter extends TeleHubReporter
|
||||
class QueueTransport extends HttpsTransport
|
||||
{
|
||||
public array $calls=[];public array $responses=[];
|
||||
public function request(string $method,string $url,array $fields=[]):array{
|
||||
protected function publicAddress(string $host):string{return '93.184.216.34';}
|
||||
public function request(string $method,string $url,array $fields=[],bool $downstream=false):array{
|
||||
$this->calls[]=[$method,$url,$fields];
|
||||
if(!in_array(parse_url($url,PHP_URL_HOST),config('payment_health.allowed_hosts'),true))throw new \RuntimeException('unapproved_host');
|
||||
$this->endpoint($url,$downstream);
|
||||
$r=array_shift($this->responses);return ['status'=>$r[0],'body'=>$r[1],'location'=>$r[2]??'','host'=>parse_url($url,PHP_URL_HOST),'sha256'=>hash('sha256',$r[1])];
|
||||
}
|
||||
}
|
||||
@@ -96,10 +97,31 @@ class PaymentHealthTest extends TestCase
|
||||
$t=new QueueTransport;$t->responses=[[200,'<form action="https://api.shangri-la.vc/submit.php" method="POST"><input name="money" value="32.00"></form>'],[404,'Not Found']];
|
||||
$r=(new TestEpayProbe($t,new QrEvidence))->probe(Payment::find(1),'probe-123');$this->assertSame('hard_failure',$r['status']);$this->assertSame('cashier_not_found',$r['reason']);$this->assertCount(2,$t->calls);$this->assertSame('POST',$t->calls[0][0]);$this->assertStringNotContainsString('sign=',$t->calls[0][1]);
|
||||
}
|
||||
public function test_unapproved_handoff_stops_and_generic_200_is_not_healthy():void{
|
||||
public function test_strict_opt_out_stops_new_handoff_and_generic_200_is_not_healthy():void{
|
||||
config(['payment_health.dynamic_handoffs'=>false]);
|
||||
$t=new QueueTransport;$t->responses=[[200,'<form action="https://new.example.invalid/pay" method="POST"></form>']];$r=(new TestEpayProbe($t,new QrEvidence))->probe(Payment::find(1),'probe-123');$this->assertSame('review_required',$r['status']);
|
||||
$t=new QueueTransport;$t->responses=[[200,'二维码 32.00 微信 正在跳转']];$r=(new TestEpayProbe($t,new QrEvidence))->probe(Payment::find(1),'probe-123');$this->assertSame('indeterminate',$r['status']);
|
||||
}
|
||||
public function test_dynamic_handoff_and_redirect_are_followed_but_new_entry_is_rejected():void
|
||||
{
|
||||
$this->assertTrue(config('payment_health.dynamic_handoffs'));
|
||||
$t=new QueueTransport;$t->responses=[[200,'<form action="https://cashier.example.invalid/pay" method="POST"><input name="money" value="32.00"></form>'],[302,'','https://other.example.invalid/qr'],[200,'32.00 <div id="qr"></div><script>new QRCode(document.getElementById("qr"),"weixin://wxpay/bizpayurl?pr=fixture")</script>']];
|
||||
$r=(new TestEpayProbe($t,new QrEvidence))->probe(Payment::find(1),'probe-123');$this->assertSame('healthy',$r['status']);$this->assertCount(3,$t->calls);
|
||||
try{$t->request('POST','https://cashier.example.invalid/pay');$this->fail('Untrusted entry accepted');}catch(\RuntimeException $e){$this->assertSame('unapproved_host',$e->getMessage());}
|
||||
}
|
||||
public function test_dynamic_handoffs_still_reject_unsafe_destinations():void
|
||||
{
|
||||
foreach(['http://cashier.example.invalid/pay','https://127.0.0.1/pay','https://[::1]/pay','https://user:[email protected]/pay','https://cashier.example.invalid:8443/pay','file:///etc/passwd']as$url){
|
||||
$t=new QueueTransport;$t->responses=[[302,'',$url]];$r=(new TestEpayProbe($t,new QrEvidence))->probe(Payment::find(1),'probe-123');$this->assertSame('review_required',$r['status'],$url);$this->assertCount(2,$t->calls);
|
||||
}
|
||||
$t=new class extends HttpsTransport {public function check(string $url):array{return $this->endpoint($url,true);}protected function records(string $host):array{return [['ip'=>'127.0.0.1']];}};
|
||||
try{$t->check('https://cashier.example.invalid/pay');$this->fail('Private DNS allowed');}catch(\RuntimeException $e){$this->assertSame('nonpublic_dns',$e->getMessage());}
|
||||
}
|
||||
public function test_policy_upgrade_unlocks_next_slot_without_duplicate_same_slot_probe():void
|
||||
{
|
||||
$this->probe->outcomes=[['status'=>'review_required','reason'=>'unapproved_host']];$this->runMonitor();$s=$this->state();$s['channels']['channel1']['policy']=hash('sha256',json_encode(config('payment_health.allowed_hosts')));file_put_contents($this->dir.'/state.json',json_encode($s));
|
||||
$this->runMonitor();$this->assertCount(1,$this->probe->calls);$this->nextSlot();$this->runMonitor();$this->assertCount(2,$this->probe->calls);$this->assertSame('healthy',$this->state()['channels']['channel1']['status']);
|
||||
}
|
||||
public function test_qr_requires_amount_payment_uri_and_rendering():void{
|
||||
$q=new QrEvidence;$body='<p>32.00</p><div id="qr"></div><script>new QRCode(document.getElementById("qr"),"weixin://wxpay/bizpayurl?pr=fixture-only")</script>';
|
||||
$this->assertTrue($q->valid($body,'wxpay',3200));$this->assertFalse($q->valid(str_replace('32.00','16.00',$body),'wxpay',3200));$this->assertFalse($q->valid('32.00 微信二维码','wxpay',3200));
|
||||
|
||||
Reference in new issue
Block a user