| 104 | } |
| 105 | |
| 106 | func UploadTaskPlugin(c *gin.Context) { |
| 107 | var request taskPluginUploadRequest |
| 108 | if err := c.ShouldBindJSON(&request); err != nil { |
| 109 | common.ApiErrorMsg(c, err.Error()) |
| 110 | return |
| 111 | } |
| 112 | if len(request.Source) > maxTaskPluginSourceBytes { |
| 113 | common.ApiErrorMsg(c, "plugin source exceeds 8 MiB") |
| 114 | return |
| 115 | } |
| 116 | marketplaceKind := "" |
| 117 | if indexURL := strings.TrimSpace(request.MarketplaceIndexURL); indexURL != "" { |
| 118 | if !slices.ContainsFunc(setting.GetTaskPluginMarketplaceSources(), func(source setting.TaskPluginMarketplaceSource) bool { |
| 119 | return strings.TrimSpace(source.IndexURL) == indexURL |
| 120 | }) { |
| 121 | common.ApiErrorT(c, "The plugin's marketplace source is not in the source list") |
| 122 | return |
| 123 | } |
| 124 | marketplaceKind = setting.TaskPluginMarketplaceSourceKind(indexURL) |
| 125 | } |
| 126 | if expected := strings.TrimSpace(request.SourceSha256); expected != "" { |
| 127 | actual := fmt.Sprintf("%x", sha256.Sum256([]byte(request.Source))) |
| 128 | if !strings.EqualFold(actual, expected) { |
| 129 | common.ApiErrorMsg(c, "plugin source sha256 mismatch") |
| 130 | return |
| 131 | } |
| 132 | } |
| 133 | temporary := jsplugin.NewRegistry() |
| 134 | loaded, err := temporary.Register(request.Source, jsplugin.Options{}) |
| 135 | if err != nil { |
| 136 | taskPluginCompileError(c, err) |
| 137 | return |
| 138 | } |
| 139 | if err = jsplugin.ValidateV1Meta(loaded.Meta); err != nil { |
| 140 | common.ApiErrorMsg(c, err.Error()) |
| 141 | return |
| 142 | } |
| 143 | // A plugin from a community or third-party source must not take the key |
| 144 | // of a built-in plugin, and its key carries its author's name. |
| 145 | if marketplaceKind != "" && marketplaceKind != setting.TaskPluginMarketplaceKindOfficial { |
| 146 | if taskPluginHasFactory(loaded.Meta.Key) { |
| 147 | common.ApiErrorT(c, "A community plugin cannot use the key of the built-in plugin {{key}}", map[string]any{"key": loaded.Meta.Key}) |
| 148 | return |
| 149 | } |
| 150 | if !strings.Contains(loaded.Meta.Key, "_") { |
| 151 | common.ApiErrorT(c, "A community plugin's key must start with its author's GitHub name and an underscore, like alice_foo") |
| 152 | return |
| 153 | } |
| 154 | } |
| 155 | icon := strings.TrimSpace(request.Icon) |
| 156 | if icon != "" { |
| 157 | if _, _, err = jsplugin.DecodeIconDataURI(icon); err != nil { |
| 158 | common.ApiErrorMsg(c, err.Error()) |
| 159 | return |
| 160 | } |
| 161 | } |
| 162 | enabled := true |
| 163 | if request.Enabled != nil { |