Block URL's with yaml file (PHP8)

Hi,

I'd like to block URL's using a yaml file for a specific service. I have specified an URL that should be public as follows.

# Serve php scripts.
- url: /abcdef.php$
  script: abcdef.php
  secure: always

The other php URL's should be available. I can't find info on this in the documentation. Anybody have any idea?

Solved Solved
0 4 129
1 ACCEPTED SOLUTION

You're absolutely right. There's no built-in functionality in PHP itself to automatically block URLs solely based on a YAML file. You'll always need some PHP code to handle the processing and enforcement.

View solution in original post

4 REPLIES 4

Hi @John8543855,

Welcome to Google Cloud Community!

Blocking URLs with PHP and YAML:

  1. Load YAML configuration: Use yaml_parse_file to read allowed URLs from a YAML file.
  2. Define blocked URLs: Create a separate PHP array containing URLs to block.
  3. Check for blocked URLs: Use in_array to compare requested URL with blocked URLs.
    • If blocked: Display error (e.g., 403 Forbidden) and exit script.
  4. Process allowed URLs: If not blocked, proceed with normal script execution.

YAML for additional configurations:

  • Define allowed file extensions or exceptions in the YAML file.

Remember:

  • Adapt the approach based on your framework.
  • Implement error handling for YAML loading issues.
  • Consider performance for a large number of blocked URLs.
  • Securely store the YAML file to avoid exposing sensitive information.

Explore advanced libraries for complex URL blocking:

  • Symfony Access Control
  • Spatie Laravel Route Discovery

Ok, so this has to be programmed. It's not available so that it's automatic from the yaml file.

You're absolutely right. There's no built-in functionality in PHP itself to automatically block URLs solely based on a YAML file. You'll always need some PHP code to handle the processing and enforcement.

Ok. I thought there would be something because when PHP5.5 could be run you had to specify what URLs where available in the yaml file. If you didn't they couldn't be reached.