fileResourceHandler returns a ReadResourceHandler that reads paths using dir as a base directory. It honors client roots and protects against path traversal attacks. The dir argument should be a filesystem path. It need not be absolute, but that is recommended to avoid a dependency on the current w
(dir string)
| 848 | // are always caught. The SDK also protects against symlink-based attacks, |
| 849 | // where symlinks under dir lead out of the tree. |
| 850 | func fileResourceHandler(dir string) ResourceHandler { |
| 851 | // Convert dir to an absolute path. |
| 852 | dirFilepath, err := filepath.Abs(dir) |
| 853 | if err != nil { |
| 854 | panic(err) |
| 855 | } |
| 856 | return func(ctx context.Context, req *ReadResourceRequest) (_ *ReadResourceResult, err error) { |
| 857 | defer util.Wrapf(&err, "reading resource %s", req.Params.URI) |
| 858 | |
| 859 | // TODO(#25): use a memoizing API here. |
| 860 | rootRes, err := req.Session.ListRoots(ctx, nil) |
| 861 | if err != nil { |
| 862 | return nil, fmt.Errorf("listing roots: %w", err) |
| 863 | } |
| 864 | roots, err := fileRoots(rootRes.Roots) |
| 865 | if err != nil { |
| 866 | return nil, err |
| 867 | } |
| 868 | data, err := readFileResource(req.Params.URI, dirFilepath, roots) |
| 869 | if err != nil { |
| 870 | return nil, err |
| 871 | } |
| 872 | // TODO(jba): figure out mime type. Omit for now: Server.readResource will fill it in. |
| 873 | return &ReadResourceResult{Contents: []*ResourceContents{ |
| 874 | {URI: req.Params.URI, Blob: data}, |
| 875 | }}, nil |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // ResourceUpdated sends a notification to all clients that have subscribed to the |
| 880 | // resource specified in params. This method is the primary way for a |
no test coverage detected
searching dependent graphs…