Skip to content

Commit 875018c

Browse files
[symfony/framework-bundle] Secure session defaults with "cookie_secure: auto" and "cookie_samesite: lax"
1 parent 57119c1 commit 875018c

File tree

8 files changed

+206
-0
lines changed

8 files changed

+206
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
framework:
2+
secret: '%env(APP_SECRET)%'
3+
#default_locale: en
4+
#csrf_protection: true
5+
#http_method_override: true
6+
7+
# Enables session support. Note that the session will ONLY be started if you read or write from it.
8+
# Remove or comment this section to explicitly disable session support.
9+
session:
10+
handler_id: ~
11+
cookie_secure: auto
12+
cookie_samesite: lax
13+
14+
#esi: true
15+
#fragments: true
16+
php_errors:
17+
log: true
18+
19+
cache:
20+
# Put the unique name of your app here: the prefix seed
21+
# is used to compute stable namespaces for cache keys.
22+
#prefix_seed: your_vendor_name/app_name
23+
24+
# The app cache caches to the filesystem by default.
25+
# Other options include:
26+
27+
# Redis
28+
#app: cache.adapter.redis
29+
#default_redis_provider: redis://localhost
30+
31+
# APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues)
32+
#app: cache.adapter.apcu
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
test: true
3+
session:
4+
storage_id: session.storage.mock_file
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file is the entry point to configure your own services.
2+
# Files in the packages/ subdirectory configure your dependencies.
3+
4+
# Put parameters here that don't need to change on each machine where the app is deployed
5+
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
6+
parameters:
7+
8+
services:
9+
# default configuration for services in *this* file
10+
_defaults:
11+
autowire: true # Automatically injects dependencies in your services.
12+
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
13+
public: false # Allows optimizing the container by removing unused services; this also means
14+
# fetching services directly from the container via $container->get() won't work.
15+
# The best practice is to be explicit about your dependencies anyway.
16+
17+
# makes classes in src/ available to be used as services
18+
# this creates a service per class whose id is the fully-qualified class name
19+
App\:
20+
resource: '../src/*'
21+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
22+
23+
# controllers are imported separately to make sure services can be injected
24+
# as action arguments even if you don't extend any base controller class
25+
App\Controller\:
26+
resource: '../src/Controller'
27+
tags: ['controller.service_arguments']
28+
29+
# add more service definitions when explicit configuration is needed
30+
# please note that last definitions always *replace* previous ones
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"bundles": {
3+
"Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle": ["all"]
4+
},
5+
"copy-from-recipe": {
6+
"config/": "%CONFIG_DIR%/",
7+
"public/": "%PUBLIC_DIR%/",
8+
"src/": "%SRC_DIR%/"
9+
},
10+
"composer-scripts": {
11+
"cache:clear": "symfony-cmd",
12+
"assets:install %PUBLIC_DIR%": "symfony-cmd"
13+
},
14+
"env": {
15+
"APP_ENV": "dev",
16+
"APP_SECRET": "%generate(secret)%",
17+
"#TRUSTED_PROXIES": "127.0.0.1,127.0.0.2",
18+
"#TRUSTED_HOSTS": "localhost,example.com"
19+
},
20+
"gitignore": [
21+
"/.env",
22+
"/%PUBLIC_DIR%/bundles/",
23+
"/%VAR_DIR%/",
24+
"/vendor/"
25+
]
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<bg=blue;fg=white> </>
2+
<bg=blue;fg=white> What's next? </>
3+
<bg=blue;fg=white> </>
4+
5+
* <fg=blue>Run</> your application:
6+
1. Change to the project directory
7+
2. Create your code repository with the <comment>git init</comment> command
8+
3. Execute the <comment>php -S 127.0.0.1:8000 -t public</> command
9+
4. Browse to the <comment>http://localhost:8000/</> URL.
10+
11+
Quit the server with CTRL-C.
12+
Run <comment>composer require server --dev</> for a better web server.
13+
14+
* <fg=blue>Read</> the documentation at <comment>https://symfony.com/doc</>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use App\Kernel;
4+
use Symfony\Component\Debug\Debug;
5+
use Symfony\Component\Dotenv\Dotenv;
6+
use Symfony\Component\HttpFoundation\Request;
7+
8+
require __DIR__.'/../vendor/autoload.php';
9+
10+
// The check is to ensure we don't use .env in production
11+
if (!isset($_SERVER['APP_ENV'])) {
12+
if (!class_exists(Dotenv::class)) {
13+
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
14+
}
15+
(new Dotenv())->load(__DIR__.'/../.env');
16+
}
17+
18+
$env = $_SERVER['APP_ENV'] ?? 'dev';
19+
$debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env));
20+
21+
if ($debug) {
22+
umask(0000);
23+
24+
Debug::enable();
25+
}
26+
27+
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) {
28+
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
29+
}
30+
31+
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) {
32+
Request::setTrustedHosts(explode(',', $trustedHosts));
33+
}
34+
35+
$kernel = new Kernel($env, $debug);
36+
$request = Request::createFromGlobals();
37+
$response = $kernel->handle($request);
38+
$response->send();
39+
$kernel->terminate($request, $response);

symfony/framework-bundle/4.2/src/Controller/.gitignore

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
6+
use Symfony\Component\Config\Loader\LoaderInterface;
7+
use Symfony\Component\Config\Resource\FileResource;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
10+
use Symfony\Component\Routing\RouteCollectionBuilder;
11+
12+
class Kernel extends BaseKernel
13+
{
14+
use MicroKernelTrait;
15+
16+
const CONFIG_EXTS = '.{php,xml,yaml,yml}';
17+
18+
public function getCacheDir()
19+
{
20+
return $this->getProjectDir().'/var/cache/'.$this->environment;
21+
}
22+
23+
public function getLogDir()
24+
{
25+
return $this->getProjectDir().'/var/log';
26+
}
27+
28+
public function registerBundles()
29+
{
30+
$contents = require $this->getProjectDir().'/config/bundles.php';
31+
foreach ($contents as $class => $envs) {
32+
if (isset($envs['all']) || isset($envs[$this->environment])) {
33+
yield new $class();
34+
}
35+
}
36+
}
37+
38+
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
39+
{
40+
$container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php'));
41+
// Feel free to remove the "container.autowiring.strict_mode" parameter
42+
// if you are using symfony/dependency-injection 4.0+ as it's the default behavior
43+
$container->setParameter('container.autowiring.strict_mode', true);
44+
$container->setParameter('container.dumper.inline_class_loader', true);
45+
$confDir = $this->getProjectDir().'/config';
46+
47+
$loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
48+
$loader->load($confDir.'/{packages}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
49+
$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
50+
$loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');
51+
}
52+
53+
protected function configureRoutes(RouteCollectionBuilder $routes)
54+
{
55+
$confDir = $this->getProjectDir().'/config';
56+
57+
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
58+
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
59+
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
60+
}
61+
}

0 commit comments

Comments
 (0)