Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ProcessMaker/Exception/MissingBpmnReferenceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Exception;

use Exception;

class MissingBpmnReferenceException extends Exception
{
}
170 changes: 170 additions & 0 deletions ProcessMaker/Http/Controllers/Api/DeployProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Http\Controllers\Api;

use DOMDocument;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use ProcessMaker\Exception\MissingBpmnReferenceException;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Process;
use ProcessMaker\Services\ProcessBpmnReferenceService;
use Throwable;

class DeployProcess extends Controller
{
/**
* A whitelist of attributes that should not be
* sanitized by our SanitizeInput middleware.
*
* @var array
*/
public $doNotSanitize = [
'bpmn',
];

public function __construct(
private readonly ProcessBpmnReferenceService $references
) {
}

public function deployProcess(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'bpmn' => 'required|string',
'name' => 'required|string|max:255',
]);

if ($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $validator->errors(),
], 422);
}

return $this->saveDeployedProcess($request);
}

private function saveDeployedProcess(Request $request): JsonResponse
{
try {
$bpmn = $this->references->resolveNamedReferences($request->input('bpmn'));
$xmlErrors = $this->validateXml($bpmn);
if ($xmlErrors !== []) {
return response()->json([
'success' => false,
'message' => 'Invalid BPMN XML.',
'errors' => ['bpmn' => $xmlErrors],
], 422);
}

[$process, $created] = $this->upsertProcessByName($request, $bpmn);

return response()->json([
'success' => true,
'action' => $created ? 'created' : 'updated',
'process_id' => $process->id,
'name' => $process->name,
]);
} catch (Throwable $exception) {
return $this->deployProcessFailureResponse($exception);
}
}

/**
* @return array{0: Process, 1: bool}
*/
private function upsertProcessByName(Request $request, string $bpmn): array
{
return DB::transaction(function () use ($request, $bpmn): array {
$process = Process::where('name', $request->input('name'))->first();
$created = $process === null;

if ($created) {
$process = new Process();
$process->name = $request->input('name');
$process->description = '';
$process->status = 'ACTIVE';
$process->user_id = $request->user()->id;
}

$process->bpmn = $bpmn;
$process->saveOrFail();

return [$process->refresh(), $created];
});
}

private function deployProcessFailureResponse(Throwable $exception): JsonResponse
{
if ($exception instanceof MissingBpmnReferenceException) {
return response()->json([
'success' => false,
'message' => $exception->getMessage(),
], 422);
}

report($exception);

return response()->json([
'success' => false,
'message' => 'The process could not be saved.',
'error' => $exception->getMessage(),
], 422);
}

public function getProcessBpmn(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
]);

if ($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $validator->errors(),
], 422);
}

$process = Process::where('name', $request->input('name'))->first();
if ($process === null) {
return response()->json([
'success' => false,
'message' => 'Process not found.',
'name' => $request->input('name'),
], 404);
}

return response()->json([
'success' => true,
'process_id' => $process->id,
'name' => $process->name,
'bpmn' => $this->references->restoreNamedReferences((string) $process->bpmn),
]);
}

/**
* @return list<string>
*/
private function validateXml(string $bpmn): array
{
$previous = libxml_use_internal_errors(true);
libxml_clear_errors();

$document = new DOMDocument();
$valid = $document->loadXML($bpmn, LIBXML_NONET);
$errors = array_map(
static fn (\LibXMLError $error): string => trim($error->message),
libxml_get_errors()
);

libxml_clear_errors();
libxml_use_internal_errors($previous);

return $valid ? [] : $errors;
}
}
184 changes: 184 additions & 0 deletions ProcessMaker/Http/Controllers/Api/DeployScreen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Http\Controllers\Api;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use ProcessMaker\Http\Controllers\Controller;
use ProcessMaker\Models\Screen;
use ProcessMaker\Traits\TaskScreenResourceTrait;
use Symfony\Component\Yaml\Yaml;

class DeployScreen extends Controller
{
use TaskScreenResourceTrait;

/**
* A whitelist of attributes that should not be
* sanitized by our SanitizeInput middleware.
*
* @var array
*/
public $doNotSanitize = [
'yml_content',
];

public function deployScreen(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'yml_content' => 'required|string',
'name' => 'required|string',
]);

if ($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $validator->errors(),
], 422);
}

$ymlContent = $request->input('yml_content');
$name = $request->input('name');

$parsedData = $this->parseDeployYaml($ymlContent);
if ($parsedData instanceof JsonResponse) {
return $parsedData;
}

$screen = Screen::where('title', $name)->first();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review

Severity: MEDIUM

Description: POST /api/1.0/deploy-screen and POST /api/1.0/package-skeleton/deploy-screen are only behind auth:api. Any logged-in user, including a default Requester who lacks create-screens and edit-screens, can create a screen or replace the screen whose title matches name. yml_content is exempt from SanitizeInput and is copied onto config, computed, watchers, and custom_css. Screen::save() publishes a non-draft version because Screen does not limit versionable attributes. Task and request UIs then load that latest published version and pass config, custom_css, watchers, and computed into the screen renderer.

Impact: A low-privilege authenticated user can change the form that other participants see and submit. That crosses the screen-editor boundary the rest of the API enforces with can:create-screens and can:edit-screens,screen: the attacker can alter collected fields, hide or replace controls, and supply the custom CSS, watchers, and computed properties the task and request renderers apply. Impact is the shared published screen document, not server takeover.

Remediation: On both deploy-screen routes, authorize before save: can:create-screens when the title does not exist, and can:edit-screens on the resolved Screen when it does, including the same project-asset check CustomAuthorize uses for screens.update. Reject the write when the caller fails that check. Validate type and the config, computed, watchers, and custom_css payload with the same rules as ScreenController::update before assignment.

$created = $screen === null;

if ($created) {
$screen = new Screen();
$screen->title = $name;
$screen->description = $parsedData['description'] ?? '';
$screen->type = $parsedData['type'] ?? 'FORM';
}

$this->applyYamlFields($screen, $parsedData, $created);
$screen->save();
$screen->refresh();

return response()->json([
'success' => true,
'action' => $created ? 'created' : 'updated',
'screen_id' => $screen->id,
'name' => $screen->title,
'message' => $created
? 'Screen created from YAML.'
: 'Screen updated from YAML.',
]);
}

/**
* @return \Illuminate\Http\Response|JsonResponse
*/
public function getScreenYaml(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string',
]);

if ($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $validator->errors(),
], 422);
}

$name = $request->input('name');
$resolved = $this->resolvePublishedScreenByTitle($name);
if ($resolved instanceof JsonResponse) {
return $resolved;
}

[$screen, $screenVersion] = $resolved;
$config = $this->removeInspectorMetadata($screenVersion->config ?? []);
$data = [
'config' => $config,
'computed' => $screenVersion->computed,
'watchers' => $screenVersion->watchers,
'custom_css' => $screenVersion->custom_css,
];
$yaml = Yaml::dump($data, 100, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

return response($yaml, 200)
->header('Content-Type', 'text/yaml')
->header('X-Screen-Id', (string) $screen->id)
->header('X-Screen-Name', $screen->title);
}

/**
* @return array{0: Screen, 1: object}|JsonResponse
*/
private function resolvePublishedScreenByTitle(string $name): array|JsonResponse
{
$screen = Screen::where('title', $name)->first();
if ($screen === null) {
return response()->json([
'success' => false,
'message' => 'Screen not found',
'name' => $name,
], 404);
}

$screenVersion = $screen->getPublishedVersion([]);
if ($screenVersion === null) {
return response()->json([
'success' => false,
'message' => 'Screen does not have a published version',
'name' => $name,
'screen_id' => $screen->id,
], 404);
}

return [$screen, $screenVersion];
}

/**
* @return array<string, mixed>|JsonResponse
*/
private function parseDeployYaml(string $ymlContent): array|JsonResponse
{
try {
$parsedData = Yaml::parse($ymlContent) ?? [];
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'Invalid YAML content',
'error' => $e->getMessage(),
], 400);
}

if (!is_array($parsedData)) {
return response()->json([
'success' => false,
'message' => 'Invalid YAML content',
'error' => 'YAML must parse to a mapping of screen fields',
], 400);
}

return $parsedData;
}

private function applyYamlFields(Screen $screen, array $parsedData, bool $created): void
{
$defaults = [
'config' => [],
'computed' => [],
'watchers' => [],
'custom_css' => '',
];

foreach ($defaults as $field => $default) {
if (array_key_exists($field, $parsedData)) {
$screen->{$field} = $parsedData[$field];
} elseif ($created) {
$screen->{$field} = $default;
}
}
}
}
Loading
Loading