Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix - cookie name for block node #886

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import ai.privado.cache.{DatabaseDetailsCache, RuleCache}
import ai.privado.model.{Constants, DatabaseDetails, NodeType, RuleInfo}
import ai.privado.tagger.PrivadoParallelCpgPass
import ai.privado.utility.Utilities.addRuleTags
import io.shiftleft.codepropertygraph.generated.nodes.{Call, Identifier, Literal}
import io.shiftleft.codepropertygraph.generated.nodes.{Block, Call, Identifier, Literal}
import io.shiftleft.codepropertygraph.generated.{Cpg, Operators}
import io.shiftleft.semanticcpg.language.*

Expand Down Expand Up @@ -62,6 +62,24 @@ class RegularSinkTagger(cpg: Cpg, ruleCache: RuleCache) extends PrivadoParallelC
.code
.headOption
.getOrElse(node.code)
case node: Block =>
/*
Handles case
serverSetCookieWithMaxAge({ /* <=== @libs/common/utils/cookie */
ctx: context,
maxAge: ONE_YEAR_IN_SECONDS,
name: CookieName.LANG_PREFERENCE,
value: userPreferredLocale,
});
*/
node.astChildren.isCall
.name(Operators.assignment)
.where(_.argument.argumentIndex(1).ast.code("name"))
.argument
.argumentIndex(2)
.code
.headOption
.getOrElse(node.code)
case node => node.code
}).stripPrefix("\"").stripSuffix("\"")
val newRuleIdToUse = ruleInfo.id + "." + cookieName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ai.privado.languageEngine.javascript.tagger.sink

import ai.privado.cache.{RuleCache, TaggerCache}
import ai.privado.entrypoint.PrivadoInput
import ai.privado.languageEngine.javascript.tagger.sink.RegularSinkTagger
import ai.privado.model.*
import better.files.File
import io.joern.jssrc2cpg.{Config, JsSrc2Cpg}
import io.shiftleft.codepropertygraph.generated.Cpg
import io.shiftleft.semanticcpg.language.*
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class RegularSinkTaggerTest extends AnyWordSpec with Matchers with BeforeAndAfterAll {

"Cookie name" should {
"be tag properly for block node argument" in {
val cpg = code("""
|const langPreferenceCookie = cookies[CookieName.LANG_PREFERENCE];
|if (isUserSpanish(transformedBrowserLocale) || chromeFlag) {
| if (langPreferenceCookie) {
| userPreferredLocale = langPreferenceCookie;
| } else {
| setCookie({ /* <=== @libs/common/utils/cookie */
| ctx: context,
| maxAge: ONE_YEAR_IN_SECONDS,
| name: CookieName.LANG_PREFERENCE,
| value: userPreferredLocale,
| });
| }
|}
|""".stripMargin)

cpg.tag.nameExact(Constants.id).value.headOption shouldBe Some(
Constants.cookieWriteRuleId + ".CookieName.LANG_PREFERENCE"
)
}
}
}

val sinkRule = List(
RuleInfo(
Constants.cookieWriteRuleId,
"Web Storage Cookie(Write)",
"",
Array(),
List("(?i)(.*setcookie.*)"),
false,
"",
Map(),
NodeType.REGULAR,
"",
CatLevelOne.SINKS,
catLevelTwo = Constants.storages,
Language.JAVASCRIPT,
Array()
)
)
def code(code: String): Cpg = {
val inputDir = File.newTemporaryDirectory()
(inputDir / "sample.js").write(code)
val outputFile = File.newTemporaryFile()
val rule: ConfigAndRules =
ConfigAndRules(List(), sinkRule, List(), List(), List(), List(), List(), List(), List(), List())
val ruleCache = new RuleCache()
ruleCache.setRule(rule)
val config = Config().withInputPath(inputDir.toString()).withOutputPath(outputFile.toString())
val cpg = new JsSrc2Cpg().createCpgWithAllOverlays(config).get
new RegularSinkTagger(cpg, ruleCache).createAndApply()
cpg
}
Loading