Issues (3)

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/HasSlug.php (1 issue)

Labels
Severity

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 Spatie\Sluggable;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Str;
7
8
trait HasSlug
9
{
10
    protected SlugOptions $slugOptions;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
11
12
    abstract public function getSlugOptions(): SlugOptions;
13
14
    protected static function bootHasSlug()
15
    {
16
        static::creating(function (Model $model) {
17
            $model->generateSlugOnCreate();
18
        });
19
20
        static::updating(function (Model $model) {
21
            $model->generateSlugOnUpdate();
22
        });
23
    }
24
25
    protected function generateSlugOnCreate()
26
    {
27
        $this->slugOptions = $this->getSlugOptions();
28
29
        if (! $this->slugOptions->generateSlugsOnCreate) {
30
            return;
31
        }
32
33
        $this->addSlug();
34
    }
35
36
    protected function generateSlugOnUpdate()
37
    {
38
        $this->slugOptions = $this->getSlugOptions();
39
40
        if (! $this->slugOptions->generateSlugsOnUpdate) {
41
            return;
42
        }
43
44
        $this->addSlug();
45
    }
46
47
    public function generateSlug()
48
    {
49
        $this->slugOptions = $this->getSlugOptions();
50
51
        $this->addSlug();
52
    }
53
54
    protected function addSlug()
55
    {
56
        $this->ensureValidSlugOptions();
57
58
        $slug = $this->generateNonUniqueSlug();
59
60
        if ($this->slugOptions->generateUniqueSlugs) {
61
            $slug = $this->makeSlugUnique($slug);
62
        }
63
64
        $slugField = $this->slugOptions->slugField;
65
66
        $this->$slugField = $slug;
67
    }
68
69
    protected function generateNonUniqueSlug(): string
70
    {
71
        $slugField = $this->slugOptions->slugField;
72
73
        if ($this->hasCustomSlugBeenUsed() && ! empty($this->$slugField)) {
74
            return $this->$slugField;
75
        }
76
77
        return Str::slug($this->getSlugSourceString(), $this->slugOptions->slugSeparator, $this->slugOptions->slugLanguage);
78
    }
79
80
    protected function hasCustomSlugBeenUsed(): bool
81
    {
82
        $slugField = $this->slugOptions->slugField;
83
84
        return $this->getOriginal($slugField) != $this->$slugField;
85
    }
86
87
    protected function getSlugSourceString(): string
88
    {
89
        if (is_callable($this->slugOptions->generateSlugFrom)) {
90
            $slugSourceString = $this->getSlugSourceStringFromCallable();
91
92
            return $this->generateSubstring($slugSourceString);
93
        }
94
95
        $slugSourceString = collect($this->slugOptions->generateSlugFrom)
96
            ->map(fn (string $fieldName): string => data_get($this, $fieldName, ''))
97
            ->implode($this->slugOptions->slugSeparator);
98
99
        return $this->generateSubstring($slugSourceString);
100
    }
101
102
    protected function getSlugSourceStringFromCallable(): string
103
    {
104
        return call_user_func($this->slugOptions->generateSlugFrom, $this);
105
    }
106
107
    protected function makeSlugUnique(string $slug): string
108
    {
109
        $originalSlug = $slug;
110
        $i = 1;
111
112
        while ($this->otherRecordExistsWithSlug($slug) || $slug === '') {
113
            $slug = $originalSlug.$this->slugOptions->slugSeparator.$i++;
114
        }
115
116
        return $slug;
117
    }
118
119
    protected function otherRecordExistsWithSlug(string $slug): bool
120
    {
121
        $key = $this->getKey();
122
123
        if ($this->getIncrementing()) {
124
            $key ??= '0';
125
        }
126
127
        $query = static::where($this->slugOptions->slugField, $slug)
128
            ->where($this->getKeyName(), '!=', $key)
129
            ->withoutGlobalScopes();
130
131
        if ($this->usesSoftDeletes()) {
132
            $query->withTrashed();
133
        }
134
135
        return $query->exists();
136
    }
137
138
    protected function usesSoftDeletes(): bool
139
    {
140
        return (bool) in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this));
141
    }
142
143
    protected function ensureValidSlugOptions()
144
    {
145
        if (is_array($this->slugOptions->generateSlugFrom) && ! count($this->slugOptions->generateSlugFrom)) {
146
            throw InvalidOption::missingFromField();
147
        }
148
149
        if (! strlen($this->slugOptions->slugField)) {
150
            throw InvalidOption::missingSlugField();
151
        }
152
153
        if ($this->slugOptions->maximumLength <= 0) {
154
            throw InvalidOption::invalidMaximumLength();
155
        }
156
    }
157
158
    /**
159
     * Helper function to handle multi-bytes strings if the module mb_substr is present,
160
     * default to substr otherwise.
161
     */
162
    protected function generateSubstring($slugSourceString)
163
    {
164
        if (function_exists('mb_substr')) {
165
            return mb_substr($slugSourceString, 0, $this->slugOptions->maximumLength);
166
        }
167
168
        return substr($slugSourceString, 0, $this->slugOptions->maximumLength);
169
    }
170
}
171