magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Public accessibility of cron.php causes errors and security concerns

Open Gurdit opened this issue 3 months ago • 13 comments

Preconditions and environment

Magento / Adobe Commerce 2.4.8

Steps to reproduce

  1. Install Magento 2
  2. Open a browser and navigate to https://yourdomain.com/cron.php.

Expected result

  1. cron.php should not be publicly accessible in a production environment.
  2. Magento cron jobs should only run via CLI (bin/magento cron:run) and system cron tasks.

Actual result

  1. cron.php is exposed to the web by default.
  2. If executed via HTTP, it can cause runtime errors, e.g.: TypeError: implode(): Argument #1 ($array) must be of type array, string given in /app/bvu7cj4frt2um/vendor/magento/module-cron/Observer/ProcessCronQueueObserver.php:967

Additional information

  • cron.php exposure appears to be a legacy carryover from Magento 1, where web crons were common on shared hosting.
  • Current Adobe Commerce documentation recommends system-level CLI cron only (bin/magento cron:run).
  • Executing cron.php via HTTP can cause errors (e.g. implode(): Argument #1 ($array) must be of type array, string given) and may unnecessarily increase the attack surface.
  • In modern deployments, cron.php is not required, but its presence in pub/ causes confusion about best practices.
  • Clarification is needed whether this file should be considered deprecated or officially supported.

Release note

No response

Triage and priority

  • [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [x] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Gurdit avatar Sep 12 '25 12:09 Gurdit

Hi @Gurdit. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce.


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Sep 12 '25 12:09 m2-assistant[bot]

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • [ ] 5. Add label Issue: Confirmed once verification is complete.
  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Sep 12 '25 12:09 m2-assistant[bot]

You should have an entry in .htaccess (within pub folder) that prevents cron.php execution, like this:

https://github.com/magento/magento2/blob/c8ff03033f9d79d9e6b6482086940153a28c6443/pub/.htaccess#L225-L234

devchris79 avatar Sep 12 '25 15:09 devchris79

@Gurdit, If you are running on NGINX, convert the .htaccess rules into the corresponding NGINX configuration.

WebHostingAce avatar Sep 15 '25 01:09 WebHostingAce

@devchris79 @WebHostingAce Thanks for your Contribution!!

Hi @Gurdit,

Thanks for your reporting and collaboration.

Could you please refer the above comments https://github.com/magento/magento2/issues/40190#issuecomment-3285755135 https://github.com/magento/magento2/issues/40190#issuecomment-3290172608 and please let us know if you are still facing any issue.

Thanks.

engcom-Bravo avatar Sep 15 '25 09:09 engcom-Bravo

Possible Strategies / Fixes

You’ll have to choose a path depending on your constraints (hosting, infra, CDNs, etc.). Below are options; I’ll also sketch a sample patch.

Strategy Pros Cons / Risks
Block HTTP access via web server (Apache / Nginx rules) Simple, doesn’t require code change. Can return 403/404 for cron.php via HTTP. Requires control of server config. Might block legitimate HTTP calls if any custom use.
In-code guard in cron.php itself Self-protecting: if accessed via HTTP, exit early or redirect. cron.php may be overwritten in upgrades; you must maintain patch.
Remove/disable cron.php file in pub/ (or root) via installation process Eliminates the endpoint entirely. Some legacy plugins might expect it; may break fallback, legacy workflows.
Wrap cron-logic so HTTP execution fails safely Even if cron.php is invoked, it doesn’t crash (fails silently or safe exit). Adds overhead and complexity; must ensure no side effects.
Documentation / deprecation / backward-compatibility announcement Sets expectations for users; upstream can follow with official removal. Doesn’t solve in existing deployed sites; reactive.

Sketching a Patch / Guard Logic

If you go with the in-code guard approach, place early checks in pub/cron.php before executing the core cron logic:

<?php // pub/cron.php (or root/cron.php) — Patch start

// If request is via HTTP (i.e. $_SERVER['REQUEST_METHOD'] is set), block it if (php_sapi_name() !== 'cli' && isset($_SERVER['REQUEST_METHOD'])) { // Option A: send 404 header("HTTP/1.1 404 Not Found"); exit;
</span><span><span class="hljs-comment">// Option B: send 403 Forbidden</span></span><span>
</span><span><span class="hljs-comment">// header("HTTP/1.1 403 Forbidden");</span></span><span>
</span><span><span class="hljs-comment">// exit;</span></span><span>

}

// ... rest of normal cron.php logic

Additionally, wrap around parts where arrays or implode is used, to avoid runtime errors when input is malformed:

// Example where implode is used: make sure input is array $someVar = /* whatever source */; if (!is_array($someVar)) { $someVar = []; // or cast / convert } $joined = implode(',', $someVar);

The key is: short-circuit HTTP requests.


Alternative Server-Level Blocks

Apache (.htaccess inside pub/)

<Files "cron.php"> Require all denied </Files>

Nginx

location = /cron.php { deny all; return 404; }

Steps You’d Take (Practical)

  1. Identify exactly where cron.php resides in your Magento setup (root/cron.php or pub/cron.php).

  2. Decide whether your environment allows server config changes.

    • If yes → block at Apache/Nginx level (preferred, cleaner).

    • If no → patch cron.php with in-code guard.

  3. If patching, keep it in a diff file or patch folder so it’s version-controlled.

  4. Add unit / integration tests (if possible) for HTTP vs CLI invocation.

  5. Deploy to staging first, then verify:

    • curl https://yourdomain.com/cron.php gives 404/403 or no execution.

    • Cron tasks via CLI (bin/magento cron:run) still run as expected.

    • No side effects in other flows.

  6. For completeness, consider filing a pull request (if your change is generic) or join discussion upstream to deprecate/remove the public cron.php.

koushikch7 avatar Sep 24 '25 18:09 koushikch7

@koushikch7 Thanks for your Contribution!!.

Hi @Gurdit,

Kindly refer the above comment and please let us know if you are still facing any issue.

Thanks.

engcom-Bravo avatar Sep 25 '25 11:09 engcom-Bravo

Would be good to know if anybody still relies on the pub/cron.php file to exist, because in my opinion nobody should rely on it and we should simply remove it from the codebase.

hostep avatar Sep 25 '25 13:09 hostep

Hello @Gurdit,

Thanks for the report and collaboration!

We have tried to reproduce the issue in the latest development branch i.e. 2.4-develop but the issue is not reproducible for us in both Apache and Nginx.

Both gives us 404 page not found error as below:

Image

Please try to reproduce the issue in the 2.4-develop branch and let us know if we missed anything.

Thank you

engcom-Hotel avatar Oct 06 '25 12:10 engcom-Hotel

Hello @Gurdit,

Gentle reminder for this issue.

Thank you

engcom-Hotel avatar Oct 13 '25 08:10 engcom-Hotel

Would be good to know if anybody still relies on the pub/cron.php file to exist, because in my opinion nobody should rely on it and we should simply remove it from the codebase.

I agree with @hostep's comment above, I think it is not recommended to use the web version of cron.php, hence confirming this issue.

Hello @Gurdit,

Thank you for bringing this issue to our attention. If you have a potential solution, we encourage you to submit a pull request. Your contributions significantly enhance Magento Open Source for everyone. Should you need any assistance, please don't hesitate to ask!

engcom-Hotel avatar Dec 10 '25 12:12 engcom-Hotel

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-16194 is successfully created for this GitHub issue.

github-jira-sync-bot avatar Dec 10 '25 12:12 github-jira-sync-bot

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

m2-assistant[bot] avatar Dec 10 '25 12:12 m2-assistant[bot]