Skip to content

Commit 9f4d1c6

Browse files
committed
PS: PowerShell doesn't have a notion of true, false, null, etc. In the extracted AST these are just variables with special names. We insert synthesized AST elements that represent these special variables.
1 parent 7adb020 commit 9f4d1c6

File tree

1 file changed

+85
-0
lines changed
  • powershell/ql/lib/semmle/code/powershell/ast/internal

1 file changed

+85
-0
lines changed

powershell/ql/lib/semmle/code/powershell/ast/internal/Synthesis.qll

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,88 @@ private module CmdArguments {
567567
}
568568
}
569569
}
570+
571+
/**
572+
* Synthesize literals from known constant strings.
573+
*/
574+
private module LiteralSynth {
575+
private class LiteralSynth extends Synthesis {
576+
final override predicate isRelevant(Raw::Ast a) {
577+
exists(Raw::VarAccess va | a = va |
578+
va.getUserPath().toLowerCase() = "true"
579+
or
580+
va.getUserPath().toLowerCase() = "false"
581+
or
582+
va.getUserPath().toLowerCase() = "null"
583+
or
584+
Raw::isEnvVariableAccess(va, _)
585+
or
586+
Raw::isAutomaticVariableAccess(va, _)
587+
)
588+
}
589+
590+
final override Expr getResultAstImpl(Raw::Ast r) {
591+
exists(Raw::Ast parent, ChildIndex i | this.child(parent, i, _, r) |
592+
result = TBoolLiteral(parent, i) or
593+
result = TNullLiteral(parent, i) or
594+
result = TEnvVariable(parent, i) or
595+
result = TAutomaticVariable(parent, i)
596+
)
597+
}
598+
599+
private predicate child(Raw::Ast parent, ChildIndex i, Child child, Raw::VarAccess va) {
600+
exists(string s |
601+
parent.getChild(toRawChildIndex(i)) = va and
602+
va.getUserPath().toLowerCase() = s
603+
|
604+
s = "true" and
605+
child = SynthChild(BoolLiteralKind(true))
606+
or
607+
s = "false" and
608+
child = SynthChild(BoolLiteralKind(false))
609+
or
610+
s = "null" and
611+
child = SynthChild(NullLiteralKind())
612+
or
613+
Raw::isEnvVariableAccess(va, s) and
614+
child = SynthChild(EnvVariableKind(s))
615+
or
616+
Raw::isAutomaticVariableAccess(va, s) and
617+
child = SynthChild(AutomaticVariableKind(s))
618+
)
619+
}
620+
621+
override predicate child(Raw::Ast parent, ChildIndex i, Child child) {
622+
this.child(parent, i, child, _)
623+
}
624+
625+
final override predicate booleanValue(BoolLiteral b, boolean value) {
626+
exists(Raw::Ast parent, ChildIndex i |
627+
b = TBoolLiteral(parent, i) and
628+
this.child(parent, i, SynthChild(BoolLiteralKind(value)))
629+
)
630+
}
631+
632+
final override predicate envVariableName(EnvVariable var, string name) {
633+
exists(Raw::Ast parent, ChildIndex i |
634+
var = TEnvVariable(parent, i) and
635+
this.child(parent, i, SynthChild(EnvVariableKind(name)))
636+
)
637+
}
638+
639+
final override predicate automaticVariableName(AutomaticVariable var, string name) {
640+
exists(Raw::Ast parent, ChildIndex i |
641+
var = TAutomaticVariable(parent, i) and
642+
this.child(parent, i, SynthChild(AutomaticVariableKind(name)))
643+
)
644+
}
645+
646+
final override Location getLocation(Ast n) {
647+
exists(Raw::VarAccess va |
648+
this.child(_, _, _, va) and
649+
n = getResultAst(va) and
650+
result = va.getLocation()
651+
)
652+
}
653+
}
654+
}

0 commit comments

Comments
 (0)