Changeset 78497 in webkit


Ignore:
Timestamp:
Feb 14, 2011 12:48:12 PM (13 years ago)
Author:
abarth@webkit.org
Message:

2011-02-14 Adam Barth <abarth@webkit.org>

Reviewed by Eric Seidel.

Add basic parser for Content Security Policy
https://bugs.webkit.org/show_bug.cgi?id=54379

Add a constructor for copying a Vector into a String. I suspect there
are a number of call sites that are doing this manually that would
benefit from being moved to this API.

  • wtf/text/WTFString.h: (WTF::String::String):

2011-02-14 Adam Barth <abarth@webkit.org>

Reviewed by Eric Seidel.

Add basic parser for Content Security Policy
https://bugs.webkit.org/show_bug.cgi?id=54379

The parser in this patch is very basic. It just segments the CSP
header into directives. The exactly syntax will likely change a bit as
we discuss the details in public-web-security, but this parser will
allow us to make progress.

Sadly, this patch does not contain any tests. That's because CSP
policies do not have any observable effects yet. Hopefully we'll get
enough sketched out in the next couple patches to begin writing tests.

  • page/ContentSecurityPolicy.cpp: (WebCore::CSPDirective::CSPDirective): (WebCore::CSPDirective::name): (WebCore::CSPDirective::value): (WebCore::ContentSecurityPolicy::~ContentSecurityPolicy): (WebCore::ContentSecurityPolicy::didReceiveHeader): (WebCore::ContentSecurityPolicy::parse):
  • page/ContentSecurityPolicy.h:
Location:
trunk/Source
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r78482 r78497  
     12011-02-14  Adam Barth  <abarth@webkit.org>
     2
     3        Reviewed by Eric Seidel.
     4
     5        Add basic parser for Content Security Policy
     6        https://bugs.webkit.org/show_bug.cgi?id=54379
     7
     8        Add a constructor for copying a Vector into a String.  I suspect there
     9        are a number of call sites that are doing this manually that would
     10        benefit from being moved to this API.
     11
     12        * wtf/text/WTFString.h:
     13        (WTF::String::String):
     14
    1152011-02-14  Pavel Podivilov  <podivilov@chromium.org>
    216
  • trunk/Source/JavaScriptCore/wtf/text/WTFString.h

    r76894 r78497  
    9393    String(const UChar* characters, unsigned length);
    9494
     95    // Construct a string by copying the contents of a vector.  To avoid
     96    // copying, consider using String::adopt instead.
     97    template<size_t inlineCapacity>
     98    explicit String(const Vector<UChar, inlineCapacity>&);
     99
    95100    // Construct a string with UTF-16 data, from a null-terminated source.
    96101    String(const UChar*);
     
    378383
    379384// Definitions of string operations
     385
     386template<size_t inlineCapacity>
     387String::String(const Vector<UChar, inlineCapacity>& vector)
     388    : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : 0)
     389{
     390}
    380391
    381392#ifdef __OBJC__
  • trunk/Source/WebCore/ChangeLog

    r78495 r78497  
     12011-02-14  Adam Barth  <abarth@webkit.org>
     2
     3        Reviewed by Eric Seidel.
     4
     5        Add basic parser for Content Security Policy
     6        https://bugs.webkit.org/show_bug.cgi?id=54379
     7
     8        The parser in this patch is very basic.  It just segments the CSP
     9        header into directives.  The exactly syntax will likely change a bit as
     10        we discuss the details in public-web-security, but this parser will
     11        allow us to make progress.
     12
     13        Sadly, this patch does not contain any tests.  That's because CSP
     14        policies do not have any observable effects yet.  Hopefully we'll get
     15        enough sketched out in the next couple patches to begin writing tests.
     16
     17        * page/ContentSecurityPolicy.cpp:
     18        (WebCore::CSPDirective::CSPDirective):
     19        (WebCore::CSPDirective::name):
     20        (WebCore::CSPDirective::value):
     21        (WebCore::ContentSecurityPolicy::~ContentSecurityPolicy):
     22        (WebCore::ContentSecurityPolicy::didReceiveHeader):
     23        (WebCore::ContentSecurityPolicy::parse):
     24        * page/ContentSecurityPolicy.h:
     25
    1262011-02-14  Andrew Wason  <rectalogic@rectalogic.com>
    227
  • trunk/Source/WebCore/page/ContentSecurityPolicy.cpp

    r78058 r78497  
    3030namespace WebCore {
    3131
     32class CSPDirective {
     33public:
     34    CSPDirective(const String& name, const String& value)
     35        : m_name(name)
     36        , m_value(value)
     37    {
     38    }
     39
     40    const String& name() const { return m_name; }
     41    const String& value() const { return m_value; }
     42
     43private:
     44    String m_name;
     45    String m_value;
     46};
     47
    3248ContentSecurityPolicy::ContentSecurityPolicy()
    3349    : m_isEnabled(false)
     
    3551}
    3652
     53ContentSecurityPolicy::~ContentSecurityPolicy()
     54{
     55}
     56
    3757void ContentSecurityPolicy::didReceiveHeader(const String& header)
    3858{
     59    if (!m_directives.isEmpty())
     60        return; // The first policy wins.
     61
    3962    m_isEnabled = true;
    40     m_header = header;
     63    parse(header);
    4164}
    4265
     
    4669}
    4770
     71void ContentSecurityPolicy::parse(const String& policy)
     72{
     73    ASSERT(m_directives.isEmpty());
     74
     75    if (policy.isEmpty())
     76        return;
     77
     78    enum {
     79        BeforeDirectiveName,
     80        DirectiveName,
     81        AfterDirectiveName,
     82        DirectiveValue,
     83    } state = BeforeDirectiveName;
     84
     85    const UChar* pos = policy.characters();
     86    const UChar* end = pos + policy.length();
     87
     88    Vector<UChar, 32> name;
     89    Vector<UChar, 64> value;
     90
     91    while (pos < end) {
     92        UChar currentCharacter = *pos++;
     93        switch (state) {
     94        case BeforeDirectiveName:
     95            if (isASCIISpace(currentCharacter))
     96                continue;
     97            state = DirectiveName;
     98            // Fall through.
     99        case DirectiveName:
     100            if (!isASCIISpace(currentCharacter)) {
     101                name.append(currentCharacter);
     102                continue;
     103            }
     104            state = AfterDirectiveName;
     105            // Fall through.
     106        case AfterDirectiveName:
     107            if (isASCIISpace(currentCharacter))
     108                continue;
     109            state = DirectiveValue;
     110            // Fall through.
     111        case DirectiveValue:
     112            if (currentCharacter != ';') {
     113                value.append(currentCharacter);
     114                continue;
     115            }
     116            // We use a copy here instead of String::adopt because we expect
     117            // the name and the value to be relatively short, so the copy will
     118            // be cheaper than the extra malloc.
     119            // FIXME: Perform directive-specific parsing of the value.
     120            m_directives.append(CSPDirective(String(name), String(value)));
     121            name.clear();
     122            value.clear();
     123            state = BeforeDirectiveName;
     124            continue;
     125        }
     126    }
    48127}
     128
     129}
  • trunk/Source/WebCore/page/ContentSecurityPolicy.h

    r78058 r78497  
    2727#define ContentSecurityPolicy_h
    2828
     29#include <wtf/Vector.h>
    2930#include <wtf/text/WTFString.h>
    3031
    3132namespace WebCore {
     33
     34class CSPDirective;
    3235
    3336class ContentSecurityPolicy {
     
    3538public:
    3639    ContentSecurityPolicy();
     40    ~ContentSecurityPolicy();
    3741
    3842    void didReceiveHeader(const String&);
     
    4044
    4145private:
     46    typedef Vector<CSPDirective> DirectiveList;
     47
     48    void parse(const String&);
     49
    4250    bool m_isEnabled;
    43     String m_header;
     51    DirectiveList m_directives;
    4452};
    4553
Note: See TracChangeset for help on using the changeset viewer.