Skip to content

Commit

Permalink
Sanitize cli arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
intuibase committed Feb 7, 2024
1 parent 9e35092 commit 2073dc8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion agent/native/ext/MemoryTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ void removeFromTrackedAllocatedBlocks(
IntrusiveDoublyLinkedList* allocatedBlocks,
size_t* possibleActuallyRequestedSize )
{
if (!allocatedBlock) {
return;
}

EmbeddedTrackingDataHeader* trackingDataHeader = allocatedBlockToTrackingData( allocatedBlock, originallyRequestedSize );

verifyMagic( "prefix", trackingDataHeader->prefixMagic, prefixMagicExpectedValue );
Expand Down Expand Up @@ -275,7 +279,7 @@ void memoryTrackerBeforeFree(
IntrusiveDoublyLinkedList* allocatedBlocks = isPersistent ? &memTracker->allocatedPersistentBlocks : &memTracker->allocatedRequestScopedBlocks;

ELASTIC_APM_ASSERT( *allocated >= originallyRequestedSize
, "Attempting to free more %s memory than allocated. Allocated: %" PRIu64 ". Attempting to free: %" PRIu64
, "Attempting to free more %s memory than allocated. Allocated: %" PRIu64 ". Attempting to free: %" PRIu64
, allocType( isPersistent ), *allocated, (UInt64)originallyRequestedSize );

*possibleActuallyRequestedSize = originallyRequestedSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ private static function isCliScript(): bool
return PHP_SAPI === 'cli';
}

private function sanitizeCliName(string $name): string {
return preg_replace('/[^a-zA-Z0-9.:_\-]/', '_', $name);
}

private function discoverCliName(): string
{
global $argc, $argv;
Expand Down Expand Up @@ -452,7 +456,7 @@ private function discoverCliName(): string
'Using CLI script name as transaction name',
['cliScriptName' => $cliScriptName, 'argc' => $argc, 'argv' => $argv]
);
return $cliScriptName;
return $this->sanitizeCliName($cliScriptName);
}

$txName = $cliScriptName . ' ' . $argv[1];
Expand All @@ -462,7 +466,7 @@ private function discoverCliName(): string
. ' - including the first argument in transaction name',
['txName' => $txName, 'argc' => $argc, 'argv' => $argv]
);
return $txName;
return $this->sanitizeCliName($txName);
}

/**
Expand Down

0 comments on commit 2073dc8

Please sign in to comment.