Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Skills Permission and Security Control

Skills Permission and Security Control

Skills can access the file system, call external APIs, and execute scripts; if these capabilities are abused, they pose security risks.

This article introduces how to establish security boundaries during the design phase to prevent Skills from performing unexpected operations.


Default Permission Scope of Skills

In Claude's execution environment, Skills' permissions are determined by the sandbox and are not unlimited.

Operation types Permission Status Description
Read uploaded files Allowed Only /mnt/user-data/uploads/
Write output files Allowed Only /mnt/user-data/outputs/ and /home/claude/
Read system files Limited Read-only mount, cannot modify system files
Access external network Limited Only whitelisted domains are allowed
Execute arbitrary system commands Limited Cannot use sudo, cannot modify system configuration
Access other users' data Forbid Guaranteed by sandbox isolation

Skills are by design a practice of the least privilege principle: a Skill can only access the resources it explicitly needs. If a Skill needs permissions beyond the above scope, the design should be reconsidered.


Clearly State Permission Boundaries in SKILL.md

Clearly declare in the Skill documentation which resources it will access, so users understand the scope of the Skill's behavior before using it.



## Permission Description



This Skill will perform the following operations. Please confirm that you understand:



File Access

- 读取:/mnt/user-data/uploads/ 下用户上传的文件

- 写入:/mnt/user-data/outputs/ 下的输出文件



Network Access

- 无(本 Skill 不访问任何外部网络)



Operations that will not be performed

- 不读取系统文件

- 不修改已上传的原始文件

- 不访问任何外部服务


Prevent Path Traversal Attacks

When a script accepts a user-provided file path, it needs to verify that the path is within the allowed range, preventing users from ../ accessing directories they should not access.

Example

File path: scripts/safe_path.py

import

os

Allowed read directories

ALLOWED_READ_DIRS

=

[

   

"/mnt/user-data/uploads"

,

   

"/mnt/skills/public"

,

]

Allowed write directories

ALLOWED_WRITE_DIRS

=

[

   

"/mnt/user-data/outputs"

,

   

"/home/claude"

,

]

def

is_safe_path

(

path:

str

,

allowed_dirs:

list

)

bool

:

   

"""
    Check whether the path is within the allowed directories

    Prevent path traversal attacks such as ../../../etc/passwd
    """

   

Resolve to an absolute path (eliminating .. and symlinks)

    real_path

=

os

.

path

.

realpath

(

os

.

path

.

abspath

(

path

)

)

   

for

allowed

in

allowed_dirs:

        real_allowed

=

os

.

path

.

realpath

(

allowed

)

       

Check whether real_path starts with the allowed directory

       

if

real_path.

startswith

(

real_allowed +

os

.

sep

)

or

real_path

==

real_allowed:

           

return

True

   

return

False

def

safe_read_path

(

user_input:

str

)

str

:

   

"""Validate a read path, raise an exception if invalid"""

   

if

not

is_safe_path

(

user_input

,

ALLOWED_READ_DIRS

)

:

       

raise

PermissionError

(

            f

"Access denied: {user_input}\n"

            f

"Only the following directories are allowed for reading: {ALLOWED_READ_DIRS}"

       

)

   

return

os

.

path

.

realpath

(

user_input

)

def

safe_write_path

(

user_input:

str

)

str

:

   

"""Validate a write path, raise an exception if invalid"""

   

if

not

is_safe_path

(

user_input

,

ALLOWED_WRITE_DIRS

)

:

       

raise

PermissionError

(

            f

"Write denied: {user_input}\n"

            f

"Only the following directories are allowed for writing: {ALLOWED_WRITE_DIRS}"

       

)

   

return

os

.

path

.

realpath

(

user_input

)

Usage examples

if

name

==

"main"

:

   

Normal path: allowed

    ok_path

=

safe_read_path

(

"/mnt/user-data/uploads/runoob.csv"

)

   

print

(

f

"Allowed: {ok_path}"

)

   

Traversal path: denied

   

try

:

        bad_path

=

safe_read_path

(

"/mnt/user-data/uploads/../../etc/passwd"

)

   

except

PermissionError

as

e:

       

print

(

f

"Blocked: {e}"

)



通过:/mnt/user-data/uploads/runoob.csv

已拦截:拒绝访问:/mnt/user-data/uploads/../../etc/passwd

只允许读取以下目录:['/mnt/user-data/uploads', '/mnt/skills/public']


Secure Storage of API Keys

Different key management methods vary significantly in security level.

Method Safety Recommendation
Hardcoded in scripts Very low, will be committed to Git Forbid
Environment Variables Medium, process isolation Recommended (development phase)
.env file (added to .gitignore) Medium, file stored locally Recommended (local use)
System secret manager (such as Vault) High, centralized management Recommended (production environment)

Example

File path: scripts/config.py

Securely read configuration from multiple sources, searching in descending order of priority

import

os

def

get_secret

(

key:

str

,

required:

bool

=

True

)

str

:

   

"""
    Read keys from the following sources in order of priority:
    1. Environment variables (highest priority)
    2. /home/claude/.skill_secrets file (local key file)
    3. If required=True and not found, raise an exception
    """

   

1. Environment variables

    value

=

os

.

environ

.

get

(

key

)

   

if

value:

       

return

value

   

2. Local key file (each line in format KEY=VALUE)

    secrets_file

=

"/home/claude/.skill_secrets"

   

if

os

.

path

.

exists

(

secrets_file

)

:

       

with

open

(

secrets_file

)

as

f:

           

for

line

in

f:

                line

=

line.

strip

(

)

               

if

line.

startswith

(

f

"{key}="

)

:

                   

return

line

[

len

(

key

)

1

:

]

   

3. Not found

   

if

required:

       

raise

EnvironmentError

(

            f

"Missing required key: {key}\n"

            f

"Please set the environment variable: export {key}='your key'"

       

)

   

return

""


Security Filtering of Input Content

When a Skill passes user input to a Shell command, the input must be escaped to prevent command injection.

Example

File path: scripts/safe_exec.py

import

subprocess

import

shlex

def

safe_shell_exec

(

template:

str

,

user_input:

str

)

str

:

   

"""
    Safely embed user input into Shell commands

    Wrong approach: os.system(f"process {user_input}") # command injection risk!
    Correct approach: use a parameter list and let subprocess handle escaping
    """

   

Use a list rather than a string; subprocess will handle escaping automatically

   

cmd

=

[

"python"

,

"scripts/process.py"

,

user_input

]

    result

=

subprocess

.

run

(

cmd

,

capture_output

=

True

,

text

=

True

,

timeout

=

30

)

   

return

result.

stdout

If you must build a string command, use shlex.quote to escape user input

def

safe_string_exec

(

user_filename:

str

)

str

:

    safe_name

=

shlex

.

quote

(

user_filename

)

 

Automatically add quotes and escape special characters

   

cmd

=

f

"wc -l {safe_name}"

    result

=

subprocess

.

run

(

cmd

,

shell

=

True

,

capture_output

=

True

,

text

=

True

)

   

return

result.

stdout

Never use os.system(f"cmd {user_input}") this way to execute commands. If user input contains ; rm -rf / such content, it will lead to catastrophic consequences. Always use subprocess's list argument form.