Skip to content

Commit 8172e32

Browse files
SONARPHP-1582 S3330: Only raise for variable cookies (#1337)
1 parent 89aec73 commit 8172e32

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

php-checks/src/main/java/org/sonar/php/checks/HttpOnlyCheck.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void visitFunctionCall(FunctionCallTree tree) {
7070
createIssueIfHttpOnlyIsFalse(argument.get().value(), tree);
7171
} else if (tree.callArguments().size() != 3) {
7272
// if only 3 argument are defined there is an ambiguity so we don't raise issue
73-
context().newIssue(this, tree.callee(), MESSAGE);
73+
createIssueIfCookieValueIsNotHardcoded(tree);
7474
}
7575
}
7676
if (isSymfonyCookieCreation(tree)) {
@@ -109,4 +109,16 @@ private void createIssueIfHttpOnlyIsFalse(ExpressionTree argument, FunctionCallT
109109
context().newIssue(this, tree.callee(), MESSAGE).secondary(argument, null);
110110
}
111111
}
112+
113+
private void createIssueIfCookieValueIsNotHardcoded(FunctionCallTree tree) {
114+
Optional<CallArgumentTree> cookieValue = CheckUtils.argument(tree, "value", 1);
115+
if (cookieValue.isEmpty() || isHardcodedOrNullCookieValue(cookieValue.get())) {
116+
return;
117+
}
118+
context().newIssue(this, tree.callee(), MESSAGE);
119+
}
120+
121+
private static boolean isHardcodedOrNullCookieValue(CallArgumentTree cookieValue) {
122+
return cookieValue.value().is(Kind.NULL_LITERAL) || cookieValue.value().is(Kind.REGULAR_STRING_LITERAL);
123+
}
112124
}

php-checks/src/test/resources/checks/HttpOnlyCheck.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
34
setcookie($name, $value, $expire, $path, $domain, true, false); // Noncompliant {{Make sure creating this cookie without the "httpOnly" flag is safe here.}}
45
//^^^^^^^^^ ^^^^^ <
56
setrawcookie($name, $value, $expire, $path, $domain, true, false); // Noncompliant {{Make sure creating this cookie without the "httpOnly" flag is safe here.}}
@@ -13,6 +14,16 @@
1314
setcookie($name, $value, $expire, $path); // Noncompliant
1415
setcookie($name, $value); // Noncompliant
1516

17+
18+
setcookie("tokenIdentity"); // Compliant; cookie value is empty by default
19+
setcookie("tokenIdentity", path:"/", value:'foo', expires_or_options:time() - 42000); // Compliant; cookie value is hardcoded
20+
setcookie(session_name(), '', time() - 3600, '/'); // Compliant; cookie value is hardcoded
21+
setcookie(session_name(), "", time() - 3600, '/'); // Compliant; cookie value is hardcoded
22+
\setcookie(\session_name(), null, -1, \OC::$WEBROOT ? : '/'); // Compliant; cookie value is null
23+
setcookie("tokenIdentity", 'foo', time() - 42000, "/"); // Compliant; cookie value is hardcoded
24+
setcookie("tokenIdentity", "foo", time() - 42000, "/"); // Compliant; cookie value is hardcoded
25+
26+
1627
setrawcookie($name, $value, $expire, $path, $domain, true, foo(false));
1728
setrawcookie($name, $value, $expire, $path, $domain, true, true);
1829
setrawcookie($name, $value, $expire, $path, $domain, false); // Noncompliant

0 commit comments

Comments
 (0)