Script security context

Is there a way to determine what security context a script is running as? For instance, I would like to know if I’m running at 2, 4, or 5. There doesn’t seem to be a method that exists.

Game scripts should always run at 2. Why do you even need to know in the first place though?
Anyways printidentity() but thats output based.
You could also probably use SecurityCapabilities.fromCurrent() and match it to the thread identity with the same permission (although I don’t know if this would actually work).
Further reference: GitHub - Pseudoreality/Roblox-Identities: A current explanation of Roblox's identities and securities. · GitHub

3 Likes

I suppose it could be used to determine if a module is being imported from a plugin vs a game script. But then why not just check plugin ~= nil?

For some reason, fromCurrent doesn’t return the main securities the thread has access to. I assume it’s because the engine capabilities in this enum are intended for ReflectionService exclusively.

You can see the engine capabilities by probing for specific apis though, as I did for a script on that repository. This code sample should be adequate for most developers on standard Studio, but if your usecase goes beyond these basic three identities, then I would look more into this script

local hasLocalUser = pcall(function() return game.DataCost end)
local hasPlugin = game:GetService("CoreGui") ~= nil -- it might be better to probe for the RobloxLocked property
local hasOpenCloud = pcall(function() return Instance.new("ModuleScript").Source end)

if hasLocalUser and hasPlugin and hasOpenCloud then
	-- most likely CommandBar (4)
elseif hasPlugin and hasOpenCloud then
	-- most likely StudioPlugin (5)
else
	-- most likely GameScript (2)
end

If you need to know your exact identity in the script, there isn’t a method that returns it, though in most cases it’s just better to probe for the capabilities anyway.

This isn’t an option unfortunately; plugin is nil in ModuleScripts, even if it was imported by a plugin.

1 Like

The reason for this is the module script needs to know if it’s running from the command bar or from a script. So 2 and 5 are internally treated the same. 4 is the wildcard. I have this, but I haven’t tested it yet (code excerpt):

-- Inline code to check if we're running in command line mode.
if runService:IsStudio() == true then
	local success, result = pcall(function()
		if type(script.Source) == "string" then
			return true
		end
		return false
	end)
	if success == true then
		if result == true then
			local instructions = require(script.Instructions)
			print(string.format(instructions, script:GetFullName()))
		end
	end
end

Since the source is protected, I’m hoping that it will error with context 2. I know that it will work on 4 though.

If executing this from a GameScript (2), this should cause the pcall to fail upon attempting to index script.Source, as it is PluginOrOpenCloud. If executed from a StudioPlugin (5) or the CommandBar (4), it will pass. I edited my example code above to account for PluginOrOpenCloud because it’s treated differently.

Elaborate on what you mean by this?

So you want strictly 4? That’s a bit tough.
You’re reserved to using LogService and printidentity() to determine the identity or just pass an argument in whatever module you’re requring, IsCommandBar. This is a rather niche request for Roblox

Which is why I don’t want to put in a feature request. I basically need to know if this is running in the command bar or not.