Issues (10)

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/Traits/HasUrlTrait.php (4 issues)

Labels

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 Luminark\Url\Traits;
4
5
use Luminark\Url\Models\Url;
6
7
trait HasUrlTrait
8
{
9
    /**
10
     * Temporary internal variable for updated URI.
11
     * 
12
     * @var string
13
     */
14
    protected $_uri = false;
15
    
16
    /**
17
     * Url object representing the URL which points to this resource.
18
     * 
19
     * @return Illuminate\Database\Eloquent\Relations\MorphOne
20
     */
21
    public function url()
22
    {
23
        return $this->morphOne($this->getUrlClass(), 'resource');
0 ignored issues
show
It seems like morphOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
24
    }
25
    
26
    /**
27
     * Overrides Eloquent Model's default attribute getting and gets
28
     * the currently set URI.
29
     * 
30
     * @return string|null URI
31
     */
32
    public function getUriAttribute()
33
    {
34
        return $this->_uri === false 
35
            ? ($this->url ? $this->url->uri : null) 
0 ignored issues
show
The property url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
            : $this->_uri;
37
    }
38
    
39
    /**
40
     * Overrides Eloquent Model's default attribute setting to store the URI
41
     * in memory only. Override this method if you need to have 
42
     * the URI stored as model attribute in the database.
43
     */
44
    public function setUriAttribute($uri)
45
    {
46
        $this->_uri = $uri === false ? null : $uri;
47
    }
48
    
49
    /**
50
     * Saves the URI and related URL object for the model.
51
     * 
52
     * @return Luminark\Url\Interfaces\HasUrlInterface URL resource object
53
     */
54
    public function saveUri($uri = null)
55
    {
56
        $urlClass = $this->getUrlClass();
57
        $originalUrl = $this->url;
58
        $uri = $uri ?: $this->uri;
0 ignored issues
show
The property uri does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
        
60
        if ( ! is_null($uri)) {
61
            $uri = $this->prepareUri($uri);
62
            $this->validateUri($uri);
63
        }
64
        // Check if this resource object is already associated with a Url object
65
        if ( ! $originalUrl && ! is_null($uri)) {
66
            // Associate a new Url object with current resource object
67
            $url = $urlClass::create(['uri' => $uri]);
68
            $url->resource()->associate($this);
69
            $url->save();
70
        // Dissociate content from URL if uri set to null
71
        } elseif (is_null($uri)) {
72
            $originalUrl->resource()->dissociate();
73
            $originalUrl->delete();
74
            $this->url = null;
75
        // Redirect old Url object to new Url object
76
        } elseif ($originalUrl && $originalUrl->uri !== $uri) {
77
            $originalUrl->resource()->dissociate();
78
            $originalUrl->save();
79
            $newUrl = $urlClass::firstOrCreate(['uri' => $uri]);
80
            $this->redirectUrl($originalUrl, $newUrl);
81
            $newUrl->resource()->associate($this);
82
            $newUrl->save();
83
        }
84
        
85
        // Refresh the model with updated URL object
86
        $this->load('url');
0 ignored issues
show
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87
        
88
        return $this;
89
    }
90
    
91
    /**
92
     * Gets the class for URL object needed to define Eloquent relationship
93
     * between resource and URL models. Override this method if Url class
94
     * is being extended.
95
     * 
96
     * @return string Url class
97
     */
98
    protected function getUrlClass()
99
    {
100
        return Url::class;
101
    }
102
103
    /**
104
     * Transforms URI value as required before storing it.
105
     * 
106
     * @return string Transformed URI value
107
     */
108
    protected function prepareUri($uri)
109
    {
110
        // Remove starting and trailing slash from URI
111
        $uri = preg_replace('/\/$/', '', $uri);
112
        $uri = preg_replace('/^\//', '', $uri);
113
        $uri = strtolower($uri);
114
115
        return $uri;
116
    }
117
118
    /**
119
     * Validates the URL value and makes sure it is unique. Override this 
120
     * method if custom validation is needed.
121
     * 
122
     * @return boolean URI validity status
123
     */
124
    protected function validateUri($uri)
125
    {
126
        $urlClass = $this->getUrlClass();
127
        $url = $urlClass::find($uri);
128
        
129
        return $url ? false : true;
130
    }
131
132
    /**
133
     * Modifies the $originalUrl to redirect to $newUrl.
134
     * 
135
     * @param Url $originalUrl The URL that will be redirecting
136
     * @param Url $newUrl The URL that will be redirected to
137
     */
138
    protected function redirectUrl(Url $originalUrl, Url $newUrl)
139
    {
140
        $newUrl->redirectsTo()->dissociate();
141
        $newUrl->save();
142
        $originalUrl->redirectsTo()->associate($newUrl);
143
        $originalUrl->save();
144
    }
145
}
146