Issues (31)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Table.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace frictionlessdata\tableschema;
4
5
use frictionlessdata\tableschema\DataSources\CsvDataSource;
6
use frictionlessdata\tableschema\Exceptions\DataSourceException;
7
8
/**
9
 * represents a data source which validates against a table schema
10
 * provides interfaces for validating the data and iterating over it
11
 * casts all values to their native values according to the table schema.
12
 */
13
class Table implements \Iterator
14
{
15
    public $csvDialect;
16
17
    /**
18
     * @param DataSources\DataSourceInterface $dataSource
19
     * @param Schema                          $schema
20
     * @param object                          $csvDialect
21
     *
22
     * @throws Exceptions\DataSourceException
23
     */
24
    public function __construct($dataSource, $schema = null, $csvDialect = null)
25
    {
26
        $this->csvDialect = new CsvDialect($csvDialect);
27
        if (!is_a($dataSource, 'frictionlessdata\\tableschema\\DataSources\\BaseDataSource')) {
28
            // TODO: more advanced data source detection
29
            $dataSource = new CsvDataSource($dataSource);
30
        }
31
        if (is_a($dataSource, 'frictionlessdata\\tableschema\\DataSources\\CsvDataSource')) {
32
            $dataSource->setCsvDialect($this->csvDialect);
33
        }
34
        $this->dataSource = $dataSource;
35
        if (!is_a($schema, 'frictionlessdata\\tableschema\\Schema')) {
36
            if ($schema) {
37
                $schema = new Schema($schema);
38
            } else {
39
                $schema = new InferSchema();
40
            }
41
        }
42
        $this->schema = $schema;
43
        $this->dataSource->open();
44
        $this->uniqueFieldValues = [];
45
    }
46
47
    /**
48
     * @param DataSources\DataSourceInterface $dataSource
49
     * @param Schema                          $schema
50
     * @param int                             $numPeekRows
51
     *
52
     * @return array of validation errors
53
     */
54
    public static function validate($dataSource, $schema, $numPeekRows = 10, $csvDialect = null)
55
    {
56
        try {
57
            $table = new static($dataSource, $schema, $csvDialect);
58
        } catch (Exceptions\DataSourceException $e) {
59
            return [new SchemaValidationError(SchemaValidationError::LOAD_FAILED, $e->getMessage())];
60
        }
61
        if ($numPeekRows > 0) {
62
            $i = 0;
63
            try {
64
                foreach ($table as $row) {
65
                    if (++$i > $numPeekRows) {
66
                        break;
67
                    }
68
                }
69
            } catch (Exceptions\DataSourceException $e) {
0 ignored issues
show
catch (\frictionlessdata... $e->getMessage()))); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70
                // general error in getting the next row from the data source
71
                return [new SchemaValidationError(SchemaValidationError::ROW_VALIDATION, [
72
                    'row' => $i,
73
                    'error' => $e->getMessage(),
74
                ])];
75
            } catch (Exceptions\FieldValidationException $e) {
76
                // validation error in one of the fields
77
                return array_map(function ($validationError) use ($i) {
78
                    return new SchemaValidationError(SchemaValidationError::ROW_FIELD_VALIDATION, [
79
                        'row' => $i + 1,
80
                        'field' => $validationError->extraDetails['field'],
81
                        'error' => $validationError->extraDetails['error'],
82
                        'value' => $validationError->extraDetails['value'],
83
                    ]);
84
                }, $e->validationErrors);
85
            }
86
        }
87
88
        return [];
89
    }
90
91
    public function schema($numPeekRows = 10)
92
    {
93
        $this->ensureInferredSchema($numPeekRows);
94
95
        return $this->schema;
96
    }
97
98
    public function headers($numPeekRows = 10)
99
    {
100
        $this->ensureInferredSchema($numPeekRows);
101
102
        return array_keys($this->schema->fields());
103
    }
104
105
    public function read()
106
    {
107
        $rows = [];
108
        foreach ($this as $row) {
109
            $rows[] = $row;
110
        }
111
112
        return $rows;
113
    }
114
115
    public function save($outputDataSource)
116
    {
117
        return $this->dataSource->save($outputDataSource);
118
    }
119
120
    /**
121
     * called on each iteration to get the next row
122
     * does validation and casting on the row.
123
     *
124
     * @return mixed[]
125
     *
126
     * @throws Exceptions\FieldValidationException
127
     * @throws Exceptions\DataSourceException
128
     */
129
    public function current()
130
    {
131
        if (count($this->castRows) > 0) {
132
            $row = array_shift($this->castRows);
133
        } else {
134
            $row = $this->schema->castRow($this->dataSource->getNextLine());
135
            foreach ($this->schema->fields() as $field) {
136
                if ($field->unique()) {
137
                    if (!array_key_exists($field->name(), $this->uniqueFieldValues)) {
138
                        $this->uniqueFieldValues[$field->name()] = [];
139
                    }
140
                    $value = $row[$field->name()];
141
                    if (in_array($value, $this->uniqueFieldValues[$field->name()])) {
142
                        throw new DataSourceException('field must be unique', $this->currentLine);
143
                    } else {
144
                        $this->uniqueFieldValues[$field->name()][] = $value;
145
                    }
146
                }
147
            }
148
        }
149
150
        return $row;
151
    }
152
153
    // not interesting, standard iterator functions
154
    // to simplify we prevent rewinding - so you can only iterate once
155
    public function __destruct()
156
    {
157
        $this->dataSource->close();
158
    }
159
160
    public function rewind()
161
    {
162
        if ($this->currentLine == 0) {
163
            $this->currentLine = 1;
164
        } elseif (count($this->castRows) == 0) {
165
            $this->currentLine = 1;
166
            $this->dataSource->open();
167
        }
168
    }
169
170
    public function key()
171
    {
172
        return $this->currentLine - count($this->castRows);
173
    }
174
175
    public function next()
176
    {
177
        if (count($this->castRows) == 0) {
178
            ++$this->currentLine;
179
        }
180
    }
181
182
    public function valid()
183
    {
184
        return count($this->castRows) > 0 || !$this->dataSource->isEof();
185
    }
186
187
    protected $currentLine = 0;
188
    protected $dataSource;
189
    protected $schema;
190
    protected $uniqueFieldValues;
191
    protected $castRows = [];
192
193
    protected function isInferSchema()
194
    {
195
        return is_a($this->schema, 'frictionlessdata\\tableschema\\InferSchema');
196
    }
197
198
    protected function ensureInferredSchema($numPeekRows = 10)
199
    {
200
        if ($this->isInferSchema() && count($this->schema->fields()) == 0) {
201
            // need to fetch some rows first
202
            if ($numPeekRows > 0) {
203
                $i = 0;
204
                foreach ($this as $row) {
205
                    if (++$i > $numPeekRows) {
206
                        break;
207
                    }
208
                }
209
                // these rows will be returned by next current() call
210
                $this->castRows = $this->schema->lock();
211
            }
212
        }
213
    }
214
}
215