Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize cli arguments #1133

Merged
merged 4 commits into from
Feb 7, 2024
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
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,11 @@ private static function isCliScript(): bool
return PHP_SAPI === 'cli';
}

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

private function discoverCliName(): string
{
global $argc, $argv;
Expand All @@ -441,7 +446,7 @@ private function discoverCliName(): string
return self::DEFAULT_NAME;
}

$cliScriptName = basename($argv[0]);
$cliScriptName = self::sanitizeCliName(basename($argv[0]));
if (
($argc < 2)
|| (count($argv) < 2)
Expand All @@ -455,7 +460,7 @@ private function discoverCliName(): string
return $cliScriptName;
}

$txName = $cliScriptName . ' ' . $argv[1];
$txName = $cliScriptName . ' ' . self::sanitizeCliName($argv[1]);
($loggerProxy = $this->logger->ifDebugLevelEnabled(__LINE__, __FUNCTION__))
&& $loggerProxy->log(
'CLI script is Laravel ' . self::LARAVEL_ARTISAN_COMMAND_SCRIPT . ' command with arguments'
Expand Down
Loading