Giter Site home page Giter Site logo

Comments (13)

paragonie-scott avatar paragonie-scott commented on May 17, 2024

Something like this?

function injectCSPHeader(\Psr\Http\Message\MessageInterface $message, $legacy = false)
{
    if ($this->needsCompile) {
        $this->compile();
    }
    // Are we doing a report-only header?
    $which = $this->reportOnly 
        ? 'Content-Security-Policy-Report-Only'
        : 'Content-Security-Policy';
    $message->withAddedHeader($which, $this->compiled);
    if ($legacy) {
        // Add deprecated headers for compatibility with old clients
        $message->withAddedHeader('X-'.$which, $this->compiled);
        $which = $this->reportOnly 
            ? 'X-Webkit-CSP-Report-Only'
            : 'X-Webkit-CSP';
        $message->withAddedHeader($which, $this->compiled);
    }
    return $message;
}

from csp-builder.

Ocramius avatar Ocramius commented on May 17, 2024

@paragonie-scott either that or simply return the array of headers

Note that $message is immutable, therefore every with call returns a new object (referring to the snippet above)

from csp-builder.

paragonie-scott avatar paragonie-scott commented on May 17, 2024

84ad3c5

How does that look?

from csp-builder.

Ocramius avatar Ocramius commented on May 17, 2024

@paragonie-scott looks good: is it covered by tests?

from csp-builder.

paragonie-scott avatar paragonie-scott commented on May 17, 2024

The PSR-7 part isn't, yet. I don't really use PSR-7 anywhere directly so I'll need to find a way to add a unit test without adding a dependency to e.g. Guzzle.

from csp-builder.

Ocramius avatar Ocramius commented on May 17, 2024

@paragonie-scott $this->getMock('Name\Of\Interface', ['the', 'list', 'of', 'methods', 'that', 'are', 'expected', 'to', 'exist'])

from csp-builder.

paragonie-scott avatar paragonie-scott commented on May 17, 2024

https://github.com/paragonie/csp-builder/blob/master/src/CSPBuilder.php#L269

This will break when it returns null.

from csp-builder.

Ocramius avatar Ocramius commented on May 17, 2024

@paragonie-scott you can use $this->getMock()->expects(self::exactly(2))->method('withFoo')->willReturnSelf();

from csp-builder.

paragonie-scott avatar paragonie-scott commented on May 17, 2024

Expectation failed for method name is equal to string:withHeader when invoked 2 time(s).
Method was expected to be called 2 times, actually called 0 times.

.PHP Fatal error: Class Mock_MessageInterface_a0173770 contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Psr\Http\Message\MessageInterface::withHeader) in /mnt/share/csp-builder/vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Generator.php(305) : eval()'d code on line 270

I've never used mocking before, and I have no idea what I'm even doing.

from csp-builder.

Ocramius avatar Ocramius commented on May 17, 2024

@paragonie-scott I'll send a PR :-)

from csp-builder.

Ocramius avatar Ocramius commented on May 17, 2024

@paragonie-scott see #6

from csp-builder.

paragonie-scott avatar paragonie-scott commented on May 17, 2024

Has this been adequately addressed in the latest release?

from csp-builder.

Ocramius avatar Ocramius commented on May 17, 2024

Looks like this was done in 1.2.0, thanks!

from csp-builder.

Related Issues (20)

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.