diff --git a/website/integrations/osticket/dev-policy.php b/website/integrations/osticket/dev-policy.php index 3d36fbe6..4d06aa84 100644 --- a/website/integrations/osticket/dev-policy.php +++ b/website/integrations/osticket/dev-policy.php @@ -1,4 +1,8 @@ =$min&&mb_strlen(trim($value),'UTF-8')<=$max;} +function dune_dev_id($value){return is_string($value)&&preg_match('/^[1-9][0-9]*$/D',$value)&&filter_var($value,FILTER_VALIDATE_INT)!==false?(int)$value:false;} +function dune_dev_origin_valid($origin){return $origin===null||$origin==='https://support.dunerealms.com';} // Central SSO identity only. Never accept ranks or actor IDs from submitted fields. function dune_dev_team($identity) { return is_array($identity) && (bool)array_intersect($identity['ranks']??[], ['owners','corestaff','web_network','web_net_admin','web_dev']); @@ -14,7 +18,7 @@ function dune_dev_owner($identity) { } function dune_dev_transition($identity,$action,$state,$note) { if(!dune_dev_team($identity)||($action!=='complete'&&!dune_dev_owner($identity)))throw new RuntimeException('Only unimatrix999 or grmacken can approve or deny tasks.'); - if(!is_string($note)||strlen(trim($note))<10||strlen($note)>10000)throw new RuntimeException('Provide a decision reason or completion report of 10–10,000 characters.'); + if(!dune_dev_text_valid($note,10,10000))throw new RuntimeException('Provide a decision reason or completion report of 10–10,000 characters.'); if($action==='complete'&&$state==='approved')return 'completed'; if($action==='approve'&&$state==='pending')return 'approved'; if($action==='deny'&&$state==='pending')return 'denied'; @@ -44,7 +48,7 @@ function dune_dev_validate($input) { $values=[];$errors=[]; foreach(dune_dev_fields() as$key=>[$label,$required,$max,$type,$hint]) { $value=is_string($input[$key]??null)?trim($input[$key]):''; - if(($required&&$value==='')||strlen($value)>$max||($type==='select'&&!in_array($value,$hint,true)))$errors[]=$label.': enter a valid value (maximum '.$max.' characters).'; + if(($required&&$value==='')||!dune_dev_text_valid($value,0,$max)|| (isset($input[$key])&&!is_string($input[$key]))||($type==='select'&&!in_array($value,$hint,true)))$errors[]=$label.': enter a valid value (maximum '.$max.' characters).'; $values[$key]=$value; } if(($input['privacy']??'')!=='1')$errors[]='Confirm that the request contains no passwords, tokens or unnecessary personal data.'; diff --git a/website/integrations/osticket/dev-tasks.php b/website/integrations/osticket/dev-tasks.php index 591f65ac..116ef006 100644 --- a/website/integrations/osticket/dev-tasks.php +++ b/website/integrations/osticket/dev-tasks.php @@ -10,6 +10,7 @@ $esc=static fn($v)=>htmlspecialchars((string)$v,ENT_QUOTES,'UTF-8');$errors=[];$ $_SESSION['dune_dev_csrf']??=bin2hex(random_bytes(32));$_SESSION['dune_dev_submission']??=bin2hex(random_bytes(32)); $team=dune_dev_team($identity);$owner=dune_dev_owner($identity);$priorityEditor=dune_dev_priority_editor($identity); if($_SERVER['REQUEST_METHOD']==='POST'){ + if(!dune_dev_origin_valid($_SERVER['HTTP_ORIGIN']??null)){http_response_code(403);exit('Invalid request origin.');} if(!is_string($_POST['csrf']??null)||!hash_equals($_SESSION['dune_dev_csrf'],$_POST['csrf'])){http_response_code(403);exit('Invalid request token. Reload the form.');} $action=$_POST['action']??''; try{ @@ -37,21 +38,21 @@ if($_SERVER['REQUEST_METHOD']==='POST'){ } }elseif($action==='brief'){ if(!$priorityEditor)throw new RuntimeException('Only the authorised reviewers and Dev can edit execution briefs.'); - $id=filter_var($_POST['id']??'',FILTER_VALIDATE_INT);$brief=is_string($_POST['brief']??null)?trim($_POST['brief']):'';if(!$id||strlen($brief)<30||strlen($brief)>30000)throw new RuntimeException('Enter an execution brief of 30–30,000 characters.'); + $id=dune_dev_id($_POST['id']??null);$brief=is_string($_POST['brief']??null)?trim($_POST['brief']):'';if(!$id||!dune_dev_text_valid($brief,30,30000))throw new RuntimeException('Enter an execution brief of 30–30,000 characters.'); $q=$db->prepare("UPDATE dune_dev_tasks SET execution_spec=?,updated_at=NOW() WHERE ticket_id=? AND approval='approved'");$q->execute([$brief,$id]);if(!$q->rowCount())throw new RuntimeException('Only an approved task can have an execution brief.'); $q=$db->prepare('INSERT INTO dune_dev_task_events(ticket_id,actor_id,action,note,created_at) VALUES(?,?,?,?,NOW())');$q->execute([$id,$identity['id'],'brief-updated',$brief]); header('Location: /dev-tasks.php?id='.$id.'&saved=1',true,303);exit; }elseif($action==='priority'){ if(!$priorityEditor)throw new RuntimeException('Only the authorised reviewers and Dev can set task priority.'); - $id=filter_var($_POST['id']??'',FILTER_VALIDATE_INT);$priority=$_POST['priority']??'';if(!$id||!in_array($priority,['Low','Medium','High'],true))throw new RuntimeException('Select Low, Medium or High.'); + $id=dune_dev_id($_POST['id']??null);$priority=$_POST['priority']??'';if(!$id||!in_array($priority,['Low','Medium','High'],true))throw new RuntimeException('Select Low, Medium or High.'); $q=$db->prepare('UPDATE dune_dev_tasks SET priority=?,updated_at=NOW() WHERE ticket_id=?');$q->execute([$priority,$id]); $q=$db->prepare('INSERT INTO dune_dev_task_events(ticket_id,actor_id,action,note,created_at) SELECT ticket_id,?,?,?,NOW() FROM dune_dev_tasks WHERE ticket_id=?');$q->execute([$identity['id'],'priority-changed',$priority,$id]); header('Location: /dev-tasks.php?id='.$id.'&saved=1',true,303);exit; }elseif(in_array($action,['approve','deny','complete'],true)){ if(!$team||($action!=='complete'&&!$owner))throw new RuntimeException('Only unimatrix999 or grmacken can approve or deny tasks.'); - $id=filter_var($_POST['id']??'',FILTER_VALIDATE_INT);if(!$id)throw new RuntimeException('Invalid task.'); + $id=dune_dev_id($_POST['id']??null);if(!$id)throw new RuntimeException('Invalid task.'); $note=is_string($_POST['note']??null)?trim($_POST['note']):''; - if(strlen($note)<10||strlen($note)>10000)throw new RuntimeException('Provide a decision reason or completion report of 10–10,000 characters.'); + if(!dune_dev_text_valid($note,10,10000))throw new RuntimeException('Provide a decision reason or completion report of 10–10,000 characters.'); $lock='dev-ticket:'.$id;$q=$db->prepare('SELECT GET_LOCK(?,5)');$q->execute([$lock]);if(!(int)$q->fetchColumn())throw new RuntimeException('Task is being updated. Retry shortly.'); try{ $q=$db->prepare('SELECT * FROM dune_dev_tasks WHERE ticket_id=?');$q->execute([$id]);$row=$q->fetch();$ticket=Ticket::lookup($id); diff --git a/website/integrations/osticket/tests/dev-policy-test.php b/website/integrations/osticket/tests/dev-policy-test.php index d2a108fb..9830a0ad 100644 --- a/website/integrations/osticket/tests/dev-policy-test.php +++ b/website/integrations/osticket/tests/dev-policy-test.php @@ -6,4 +6,24 @@ check(!dune_dev_submitter($member));check(dune_dev_submitter($vip));check(dune_d foreach(['owners','corestaff','web_network','web_net_admin']as$r)check(dune_dev_team(['ranks'=>[$r]])); check(dune_dev_transition($owner,'approve','pending','Approved for delivery')==='approved');check(dune_dev_transition($owner,'deny','pending','Not in current scope')==='denied');check(dune_dev_transition($dev,'complete','approved','Tests pass; merge and deployment documented')==='completed'); foreach([[$dev,'approve','pending'],[$dev,'deny','pending'],[$vip,'complete','approved'],[$owner,'complete','pending'],[$owner,'approve','denied'],[$owner,'deny','completed']]as[$u,$a,$s]){try{dune_dev_transition($u,$a,$s,'Valid detailed reason');check(false);}catch(RuntimeException$e){check(true);}} -[$v,$err]=dune_dev_validate([]);check(count($err)>=7);$valid=['title'=>'A task','type'=>'Feature','system'=>'Web','objective'=>'Implement a thing','current'=>'Existing behaviour','expected'=>'Desired behaviour','acceptance'=>'Observable result','priority'=>'Medium','privacy'=>'1'];[$v,$err]=dune_dev_validate($valid);check(!$err);check(str_contains(dune_dev_prompt($v),'Observable result'));$valid['type']='malicious';[, $err]=dune_dev_validate($valid);check(count($err)===1);$valid['type']='Feature';$valid['objective']=str_repeat('x',12001);[, $err]=dune_dev_validate($valid);check(count($err)===1);check(dune_dev_owner(['username'=>'grmacken','ranks'=>['web_net_admin']]));check(!dune_dev_owner(['username'=>'grmacken','ranks'=>['user']]));check(dune_dev_priority_editor($dev));check(!dune_dev_priority_editor($staff));check(str_contains(dune_dev_execution_brief(['objective'=>'Test','acceptance'=>'Pass']),'## Acceptance criteria'));echo "$n policy assertions passed\n"; +[$v,$err]=dune_dev_validate([]);check(count($err)>=7);$valid=['title'=>'A task','type'=>'Feature','system'=>'Web','objective'=>'Implement a thing','current'=>'Existing behaviour','expected'=>'Desired behaviour','acceptance'=>'Observable result','priority'=>'Medium','privacy'=>'1'];[$v,$err]=dune_dev_validate($valid);check(!$err);check(str_contains(dune_dev_prompt($v),'Observable result'));$valid['type']='malicious';[, $err]=dune_dev_validate($valid);check(count($err)===1);$valid['type']='Feature';$valid['objective']=str_repeat('x',12001);[, $err]=dune_dev_validate($valid);check(count($err)===1);check(dune_dev_owner(['username'=>'grmacken','ranks'=>['web_net_admin']]));check(!dune_dev_owner(['username'=>'grmacken','ranks'=>['user']]));check(dune_dev_priority_editor($dev));check(!dune_dev_priority_editor($staff));check(str_contains(dune_dev_execution_brief(['objective'=>'Test','acceptance'=>'Pass']),'## Acceptance criteria')); +$valid['objective']='Valid objective';$valid['title']=str_repeat('é',160);[, $err]=dune_dev_validate($valid);check(!$err); +$valid['title']=str_repeat('é',161);[, $err]=dune_dev_validate($valid);check(count($err)===1); +$valid['title']='Valid';$valid['risk']=['array'];[, $err]=dune_dev_validate($valid);check(count($err)===1); +$valid['risk']='';$valid['title']="\xC3\x28";[, $err]=dune_dev_validate($valid);check(count($err)===1); +foreach(['1','344','2147483647']as$id)check(dune_dev_id($id)>0); +foreach(['0','-1','1.2','1e2',' 1','1 OR 1=1',[],null,str_repeat('9',30)]as$id)check(dune_dev_id($id)===false); +check(dune_dev_origin_valid(null));check(dune_dev_origin_valid('https://support.dunerealms.com'));check(!dune_dev_origin_valid('https://evil.invalid'));check(!dune_dev_origin_valid('null')); +check(dune_dev_text_valid(str_repeat('水',10000),10,10000));check(!dune_dev_text_valid(str_repeat('水',10001),10,10000)); +check(!dune_dev_text_valid(' ',10,10000));check(!dune_dev_text_valid([],0,10000)); +check(dune_dev_transition($owner,'approve','pending',str_repeat('水',10000))==='approved'); +$base=['title'=>'Task','type'=>'Feature','system'=>'Web','objective'=>'Outcome','current'=>'Current','expected'=>'Expected','acceptance'=>'Checks','priority'=>'Medium','privacy'=>'1']; +foreach(dune_dev_fields()as$key=>[$label,$required,$max,$type,$hint]){ + $input=$base;$input[$key]=[];[, $err]=dune_dev_validate($input);check((bool)$err); + if($type==='select')continue; + $input=$base;$input[$key]=str_repeat('水',$max);[, $err]=dune_dev_validate($input);check(!$err); + $input[$key].='水';[, $err]=dune_dev_validate($input);check((bool)$err); + if($required){$input=$base;$input[$key]=' ';[, $err]=dune_dev_validate($input);check((bool)$err);} +} +$input=$base;$input['privacy']='0';[, $err]=dune_dev_validate($input);check((bool)$err); +echo "$n policy assertions passed\n";