Code Monkey home page Code Monkey logo

cors-psr7's People

Contributors

mrhash avatar neomerx avatar nikserg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cors-psr7's Issues

Update to v2

  • Improve performance and caching support;
  • Improve support in IE #31;
  • Refactor;
  • Newer PHPUnit;
  • Add strong typing, min PHP 7.1.

Support for 'file://' Origins

When requests are sent from locally stored files their Origin header would be file://

Currently CORS analyzer throws exception that such URL cannot be parsed.

Analyzer should better write to logs that such headers cannot be parsed and return AnalysisResultInterface::TYPE_REQUEST_OUT_OF_CORS_SCOPE

Use native functions

Call global function such as array_merge as \array_merge which improves performance a bit.

In some cases it incorrectly checks `Host` header

I am trying to implement this as a middleware on Silex Framework (version 2). Silex by default does not support PSR 7 request and responses, so I use DiactorosFactory Library to convert the Symfony Foundation Request/Response to & from PSR 7 Request and Response.

In the CORS Settings, I have something like this

->setServerOrigin([
    'scheme' => 'http',
    'host'   => 'www.something.test',
    'port'   => 80,
])

And the check fails even when the call is made to the host that is configured above i.e. www.something.test.

I am making a preflight request to http://www.something.test and when it comes to Analyzer.php's isSameHost method $host get's value of www.something.test and $hostURL which should be parsed object, has host property as null.

$host = $this->getRequestHostHeader($request);

This is the because internally it uses php's function parse_url to parse url if it is string.

$parsedUrl = parse_url($url);

and after parsing www.something.test returns array which looks like

array(..) {
  ["path"]=>
  string(5) "www.something.test"
}

and thus does not have enough information to match the host.

$this->scheme = $this->getArrayValue($parsedUrl, self::URL_KEY_SCHEME);
$this->host = $this->getArrayValue($parsedUrl, self::URL_KEY_HOST);
$this->port = (int)$this->getArrayValue($parsedUrl, self::URL_KEY_PORT, self::DEFAULT_PORT);

I am not sure why

cors-psr7/src/Analyzer.php

Lines 320 to 321 in 24944f3

$host = $this->getRequestHostHeader($request);
$hostUrl = $host === null ? null : $this->factory->createParsedUrl($host);

$host where which returns www.something.test passed to createParsedURL as it does not have enough data to properly parse all the request.

Tag 1.1 release?

Would it be possible to tag an 1.1 release?

This is because versions before 1.0.4 throw an error with PHP 7.2 because of assert('is_string($url) || is_array($url)'); in ParsedUrl.php. Currently only way of ignoring everything below 1.0.4 would be to manually list all the minor releases.

"neomerx/cors-psr7": "1.0.4|1.0.5|1.0.6|1.0.7|1.0.8|1.0.9|1.0.11|1.0.12|1.0.13",

This not optimal and requires manually updating composer.json on my side always when neomerx/cors-psr7 has a new release.

Add support for all ('*') allowed headers

As configuration of allowed headers might be frustrating process (send request => fail => change conf => send request => next header is not allowed => change conf, frustration => ...) it might be useful to have option for all headers allowed '*'. As it's insecure it should not be enabled by default. But it could be an option for development.

The idea in influenced by neomerx/cors-illuminate#11

Add logs

Add logs and ability to turn it on/off with

  • Psr\Log\LoggerInterface
  • LoggerAwareInterface

Debug for overall app logic and info for important decisions such as request deny.

Related to neomerx/cors-illuminate#8

Can't extend Analyzer class

Hi

Thanks for an awesome library

Due to the use of static:: in

$isSimpleMethod = isset(static::SIMPLE_METHODS[$requestMethod]);

Because the class constants are private they can't be accessed from the child class. And when you extend the analyzer it will automatically try to use the child scope when using static:: so it would work if you used self:: in the Analyzer class.

Would you be open to a pull-request to fix this?

Licence logo is broken

They have changed something and logo has become broken. Also, 2017 could be changed to 2018 as well.

Update PHP version and libraries

PHP minimum to 5.6 as 5.5 no longer supported
PHP Unit also move to 5.7 as 4.x branch is also not supported

Not sure how it plays out with HHVM as this platform is not supported by PHP Unit.

3rd party issue. Analyzer::isSameHost fails to compare Host when SSL + zend-diactoros

When the request header 'Host' value is in form of Host: example.com (without port) the parse_url method isn't capable of parsing the url correctly.

Feeding a url without a prefixed '' to parse_url will result in a non discovered domain name if the port is not present.

But I think a Host header in this form should parseable as url - or am I mistaking?

Host: example.com

https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
https://github.com/neomerx/cors-psr7/blob/master/src/Analyzer.php#L256
https://github.com/neomerx/cors-psr7/blob/master/tests/Http/ParsedUrlTest.php#L59

Make compatibility with IE easier

If using the exposed header keys they are set as an array into the http response header. As a result, there are several header lines with the same name in the response.

The Internet Explorer cannot handle the headers, even if it would be correct according to w3c specification.

In "/src/Strategies/Settings.php" the following function should look like this.

BEFORE:

public function getResponseExposedHeaders(RequestInterface $request)
    {
        return $this->getEnabledItems($this->settings[self::KEY_EXPOSED_HEADERS]);
    }

AFTER:

public function getResponseExposedHeaders(RequestInterface $request)
    {
        return implode(', ', $this->getEnabledItems($this->settings[self::KEY_EXPOSED_HEADERS]));
    }

It would be nice if you could recognize this issue and update the project.
Other browsers are not be affected by changing the returned value to a comma-separated string.

Improve CS

Scrutinizer lowers scoring for supplementary code such as default configuration sample.

What kind of check does `serServerOrigin` do?

What kind of test does the following code does?

     $this->settings->setServerOrigin([
        'scheme' => 'http',
        'host'   => 'example.com',
        'port'   => 123,
    ])

To pass the checks, should the request be coming from http://example.com port 123 or to http://example.com:123?

I am trying to implement the library on Silex Framework and I am trying to match the request is coming to the one I am specifying, and it is not working.

$this->settings->setServerOrigin([

Improve logging for Origin part

Currently when a request without origin or origin not matching the server's one incomes the component logs at debug level

Request is not CORS (request origin is empty or equals to server one).  Check config settings for Server Origin.

There are two issues here:

  • From the log it's not clear is it a problem with the request (no origin) or there is no match for the origin. It should be 2 distinct messages depending on the situation.
  • debug level should be bumped to info because if a developer activates logging it more likely to expect the request to be CORS so this issue should be more visible.

That's the corresponding code.

Allowing all headers should be removed

Currently there is a config option Settings::VALUE_ALLOW_ALL_HEADERS which should allow all headers pass through CORS. It works fine for internal lib logic. No problem here. The problem is that this value * is actually sent to client in Access-Control-Allow-Headers and browser don't understand this value.

It looks the only possible way is listing all allowed headers and special * should be removed.

It was added mostly to make development easier. However since logging has been added to the lib this feature is not so important.

It is recommended avoid using Settings::VALUE_ALLOW_ALL_HEADERS and just list all allowed headers in Settings::KEY_ALLOWED_HEADERS

Add cache support for Setttings

Currently Settings could be configured only with invoking methods and settings cannot be effectively cached and restored back from cache.

Add to Settings abilities to configure it with config in array format with methods setSettings and getSettings.

Document advanced usage

Document how to integrate the package with systems (e.g. access control) that want to send allowed methods/headers per request individually.

Unnecessary strict `psr/log`

Is there a reason for only supporting psr/log:^3.0? This library doesn't seem to utilize the changes that went into 3.0 specifically and should have no trouble supporting ^2.0 || ^3.0, right?

I only did some shallow testing by installing 2.0 and running the unit tests. But maybe I'm missing something?

Framework support

Definitely as a separate project. Possible integration

  • Laravel
  • Lumen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.