Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7569c8d

Browse files
committed
fix: read allowUnsafePathPattern from storageclass annotations
Signed-off-by: Derek Su <[email protected]>
1 parent 7e533e3 commit 7569c8d

8 files changed

Lines changed: 100 additions & 2 deletions

File tree

provisioner.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,13 @@ func pathFromPattern(pattern string, opts pvController.ProvisionOptions, allowUn
317317
return "", err
318318
}
319319

320+
if allowUnsafePath {
321+
return buf.String(), nil
322+
}
323+
320324
path := buf.String()
321325
fixedBasePathPrefix := filepath.Join(opts.PVC.Namespace, opts.PVC.Name) + string(filepath.Separator)
322-
if !allowUnsafePath && !strings.HasPrefix(path, fixedBasePathPrefix) {
326+
if !strings.HasPrefix(path, fixedBasePathPrefix) {
323327
return "", fmt.Errorf("pathPattern must start with {{ .PVC.Namespace }}/{{ .PVC.Name }}/: %s", path)
324328
}
325329

@@ -385,6 +389,16 @@ func (p *LocalPathProvisioner) provisionFor(opts pvController.ProvisionOptions,
385389
logrus.Warnf("failed to parse allowUnsafePathPattern %v, defaulting to false: %v", allowUnsafePathPattern, err)
386390
allowUnsafePath = false
387391
}
392+
} else {
393+
// Read from storageclass annotation for backward compatibility
394+
allowUnsafePathAnnotation, exists := opts.StorageClass.GetAnnotations()["allowUnsafePathPattern"]
395+
if exists {
396+
allowUnsafePath, err = strconv.ParseBool(allowUnsafePathAnnotation)
397+
if err != nil {
398+
logrus.Warnf("failed to parse allow-unsafe-path-pattern annotation %v, defaulting to false: %v", allowUnsafePathAnnotation, err)
399+
allowUnsafePath = false
400+
}
401+
}
388402
}
389403
folderName, err = pathFromPattern(pathPattern, opts, allowUnsafePath)
390404
if err != nil {

test/pod_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ func (p *PodTestSuite) TestPodWithSkipPathPatternCheck() {
167167
runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
168168
}
169169

170+
func (p *PodTestSuite) TestPodWithSkipPathPatternCheckByAnnotation() {
171+
p.kustomizeDir = "skip-path-pattern-check-by-annotation"
172+
173+
runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
174+
}
175+
170176
// ADD THIS NEW TEST METHOD
171177
func (p *PodTestSuite) TestPathTraversalPrevention() {
172178
testCases := []struct {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- ../../../deploy
5+
- storage-class.yaml
6+
- pod.yaml
7+
- pvc.yaml
8+
images:
9+
- name: rancher/local-path-provisioner
10+
newTag: dev
11+
labels:
12+
- includeSelectors: true
13+
pairs:
14+
app: local-path-provisioner
15+
patches:
16+
- path: local-path-config.yaml
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
kind: ConfigMap
2+
apiVersion: v1
3+
metadata:
4+
name: local-path-config
5+
namespace: local-path-storage
6+
data:
7+
config.json: |-
8+
{
9+
"storageClassConfigs":{
10+
"local-path-skip-path-pattern-check-by-annotation": {
11+
"nodePathMap": [
12+
{
13+
"node":"DEFAULT_PATH_FOR_NON_LISTED_NODES",
14+
"paths":["/opt/local-path-provisioner"]
15+
}
16+
]
17+
}
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: volume-test
5+
spec:
6+
containers:
7+
- name: volume-test
8+
image: nginx:stable-alpine
9+
imagePullPolicy: IfNotPresent
10+
volumeMounts:
11+
- name: volv
12+
mountPath: /data
13+
ports:
14+
- containerPort: 80
15+
volumes:
16+
- name: volv
17+
persistentVolumeClaim:
18+
claimName: local-path-pvc
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
labels:
5+
app: test
6+
name: local-path-pvc
7+
spec:
8+
storageClassName: local-path-skip-path-pattern-check-by-annotation
9+
accessModes:
10+
- ReadWriteOnce
11+
resources:
12+
requests:
13+
storage: 100Mi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: storage.k8s.io/v1
2+
kind: StorageClass
3+
metadata:
4+
name: local-path-skip-path-pattern-check-by-annotation
5+
annotations:
6+
allowUnsafePathPattern: "true"
7+
provisioner: rancher.io/local-path
8+
parameters:
9+
nodePath: /opt/local-path-provisioner
10+
pathPattern: "/opt/../{{ .PVC.Namespace }}_{{ .PVC.Name }}/../etc/"
11+
volumeBindingMode: WaitForFirstConsumer
12+
reclaimPolicy: Delete

test/testdata/skip-path-pattern-check/storage-class.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ provisioner: rancher.io/local-path
66
parameters:
77
nodePath: /opt/local-path-provisioner
88
allowUnsafePathPattern: "true"
9-
pathPattern: "/opt/../{{ .PVC.Namespace }}/{{ .PVC.Name }}/../etc/"
9+
pathPattern: "/opt/../{{ .PVC.Namespace }}_{{ .PVC.Name }}/../etc/"
1010
volumeBindingMode: WaitForFirstConsumer
1111
reclaimPolicy: Delete

0 commit comments

Comments
 (0)