Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125090 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 14CDF1A00BD for ; Wed, 21 Aug 2024 12:04:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1724241970; bh=DBVkScx6oBI3bxatpcpXBRLReA5zUxTOQtwaoqMaUXY=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=ixPIOnNSm0P1mkRdt3mtu6xs8VChUOsiZq9QwfMUuqYJlPXuMSaqJ7bFEl6PPRi+d VSkwMk1uq9+VTuDJp/w1oRwJVF1SUWvuCQ7r0vlm1qxi5tr9NFJujLlA9PpZmHhqmV kQrvhuVvI3OhyzhcDIUf74x0LacFtwUsbk/8MmUcWiJmpBPYTyvfPTXfghmPS1KMOH v1q2vI+ArgGcwdY1H/yJNs5jjCD/jjke26oJ2PdW/jIjZYC22AhiKiaPzs6i+ym16c 8kYM4Ee28jtZZJu/qMfF5A3igkOcrvoCIWnSC0NkigI9dZdUjfr1Xd5uRZrqql1+r9 bnhJzd1h70p7w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 45789180069 for ; Wed, 21 Aug 2024 12:06:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout3-smtp.messagingengine.com (fout3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 21 Aug 2024 12:06:08 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.nyi.internal [10.202.2.43]) by mailfout.nyi.internal (Postfix) with ESMTP id 4DFA9138FFBE; Wed, 21 Aug 2024 08:04:18 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-03.internal (MEProxy); Wed, 21 Aug 2024 08:04:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1724241858; x= 1724328258; bh=uELefJ8pRH0PESbsfd9YDoNGZzYorVdlnN3QzM6Q/BA=; b=x X0MPiU+2pfZifBLjFt9/nhgyTqQcgzUovF1XCTzcFjk7RQsLr83TLUWgktFg6tCr aDxTmnlOPL2mdzJdQ2AUMEmhqAvFKrCxadnfRb9DEycnkqChZcSqlAyNxNmm6zH2 h8g+2fpMiguCsLi2w2mlBQdTZKV/DL8Qr5s606d+sNdRJNqvG5LVOyEKs6WKV7Vh fYZfpU1WN22hD/OBZzU9UzKnmlm96hW9I1ViXVXAT+y4FMHiRCZZjVQPil5poRqr Ym/muqFmScHbG6cVWycGDmsfSSinVAWmJe6VUp4Adfj5VtqGJSYhlcVNoMq4Kht/ HKyL+63HB/kxd3vDmPrqA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1724241858; x=1724328258; bh=uELefJ8pRH0PESbsfd9YDoNGZzYo rVdlnN3QzM6Q/BA=; b=BKEhylAgogGogbIfz9pfIRRdDTbcO641kbm6iMX4jPhJ qZiCWPvUNJFxw5cL6VxOwgxz3QEZ+nJ5J5HjRyGHyD5ljUgqzXKadAtqe+6yD0Wn s3hg5cpebTbYBSjKbCJIBSz9QB97i/Icf5v8TsauCso+qciI2Ub9pq3nlF+bnCbo h2F04hXcOjk0vmQW0MMGp65YTkm7hlIN/CCvDg2RlzORJtLmfI6TQd789+dFTyaw Ql830NhJ9KqNn3cRi41ysiUdM8zE7n4dobY5jIh55mZl4PiexdDYnmtJcsn7ozAB bxvxd92UwyY+SiUrCd5hRbmPYgEpAxY1J35AgIW/vw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddruddukedggeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepofggfffhvfevkfgjfhfutgesrgdtreerredtjeen ucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtoh guvghsqeenucggtffrrghtthgvrhhnpedvveetveeifedtiedvhfekteefleetueeijeet gfdtveeitdejteejtefftddujeenucffohhmrghinhephhhtthhphhgvrgguvghrshhhrg gurghlrhgvrgguhigsvggvnhhsvghnthdrihhnpdhhthhtphgtohhnthhrohhllhgvrhhs vghttgdrihhtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homheprhhosgessghothhtlhgvugdrtghouggvshdpnhgspghrtghpthhtohepfedpmhho uggvpehsmhhtphhouhhtpdhrtghpthhtohepjhhohhhnsegtohhgghgvshhhrghllhdroh hrghdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvghtpdhr tghpthhtohepsghilhhgvgesshgtrhhiphhtfhhushhiohhnrdgtohhm X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id DFD3515A0061; Wed, 21 Aug 2024 08:04:17 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Wed, 21 Aug 2024 14:03:57 +0200 To: "John Coggeshall" , Bilge Cc: "internals@lists.php.net" Message-ID: In-Reply-To: <7E80614A-63CF-4515-9DF1-C6869218F864@getmailspring.com> References: <48c22449-6cdf-449f-917f-365506e3f2d0@scriptfusion.com> <7E80614A-63CF-4515-9DF1-C6869218F864@getmailspring.com> Subject: Re: [PHP-DEV] [Concept] Flip relative function lookup order (global, then local) Content-Type: multipart/alternative; boundary=4246d22494f84058b5554b35cca9a4ee From: rob@bottled.codes ("Rob Landers") --4246d22494f84058b5554b35cca9a4ee Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Aug 21, 2024, at 10:23, John Coggeshall wrote: >=20 >=20 > On Aug 2 2024, at 4:37 pm, Bilge wrote: >> My only concern is there needs to be an alternative way to do this: i= ntercepting internal calls. Sometimes, whether due to poor architecture = or otherwise, we just need to be able to replace an internal function ca= ll. One example I can think of recently is where I had to replace `heade= r()` with a void function in tests, just to stop some legacy code emitti= ng headers before the main framework kicked in, then unable to emit its = own response because HTTP headers had already been sent. In a perfect wo= rld it shouldn't be necessary, but sometimes it is, so I think for this = proposal to be palpable there must still be a way to achieve this. >=20 > Just a tangent thought to the above, but I've always been a little con= cerned with the idea that a malicious composer package could potentially= do nasty things because PHP looks at the local namespace first for func= tions. For example, if a composer package focused on Laravel that define= s malicious versions of internal functions for common namespaces like `A= pp\Models` , `App\Http\Controllers` , etc. it could do some nasty stuff = -- and supply-chain attacks aren't exactly uncommon. Even worse is Word= press or any other PHP-based software package that allows arbitrary plug= ins to be installed by non-technical users who really would have no idea= if the package was safe even if they were looking at the code. >=20 > // something.php > namespace App\Models; >=20 > function password_hash(string $password, string|int|null $algo, array = $options =3D []): string > { > print("Hello"); > return $password; > } >=20 > // my code > namespace App\Models; >=20 > include "something.php"; >=20 > password_hash('foobar', PASSWORD_DEFAULT); If this is an attack vector for your application, then fully qualified n= ames is the way to go (WordPress does this nearly everywhere, for exampl= e). >=20 > I don't recall why local namespace first won, but IMO it wasn't a grea= t call out the gate for that reason alone. Yes, you can always use `\pa= ssword_hash` instead of `password_hash` , but making the default insecu= re and slower is silly IMO -- and not fixing it because of BC seems like= the weaker argument here. >=20 > John It's not (at least for me) the BC break. It's being able to override glo= bal functions. There are legitimate use-cases outside of testing. For ex= ample, consider when a global function signature changes. In your librar= y, you have to check the php version. You can change this 100 times for = every single call, or you can just wrap it in a function that supports t= he old signature and proxies it to the new signature. In other words, it= provides options that may be better than the alternative. =E2=80=94 Rob --4246d22494f84058b5554b35cca9a4ee Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable

=
On Wed, Aug 21, 2024, at 10:23, John Coggeshall wrote:


On Aug 2 2024, at = 4:37 pm, Bilge <bilge@scriptfusion.com> wrote:
My only concern is there needs = to be an alternative way to do this: intercepting internal calls. Someti= mes, whether due to poor architecture or otherwise, we just need to be a= ble to replace an internal function call. One example I can think of rec= ently is where I had to replace `header()` with a void function in tests= , just to stop some legacy code emitting headers before the main framewo= rk kicked in, then unable to emit its own response because HTTP headers = had already been sent. In a perfect world it shouldn't be necessary, but= sometimes it is, so I think for this proposal to be palpable there must= still be a way to achieve this.

Just a tangent thought to the above, but I've always been a lit= tle concerned with the idea that a malicious composer package could pote= ntially do nasty things because PHP looks at the local namespace first f= or functions. For example, if a composer package focused on Laravel that= defines malicious versions of internal functions for common namespaces = like App\Models , App\Http\Controllers&nb= sp;, etc. it could do some nasty stuff -- and supply-chain attacks aren'= t exactly uncommon.  Even worse is Wordpress or any other PHP-based= software package that allows arbitrary plugins to be installed by non-t= echnical users who really would have no idea if the package was safe eve= n if they were looking at the code.

<?ph= p
// something.php
namespace App\Models;
=

function password_hash(string $password, strin= g|int|null $algo, array $options =3D []): string
{
   print("Hello");
   return $p= assword;
}

<?php
=
// my code
namespace App\Models;

include "something.php";

password_= hash('foobar', PASSWORD_DEFAULT);

<= div>If this is an attack vector for your application, then fully qualifi= ed names is the way to go (WordPress does this nearly everywhere, for ex= ample).


I don't recall why local namespace first won, but= IMO it wasn't a great call out the gate for that reason alone.  Ye= s, you can always use \password_hash  instead of password_hash , but making the default insecure and slower = is silly IMO -- and not fixing it because of BC seems like the weaker ar= gument here.

John

It's not (at least for me) the BC break. It's being abl= e to override global functions. There are legitimate use-cases outside o= f testing. For example, consider when a global function signature change= s. In your library, you have to check the php version. You can change th= is 100 times for every single call, or you can just wrap it in a functio= n that supports the old signature and proxies it to the new signature. I= n other words, it provides options that may be better than the alternati= ve.

=E2=80=94 Rob
--4246d22494f84058b5554b35cca9a4ee--