binwiederhier 2 лет назад
Родитель
Сommit
dc8932cd95
5 измененных файлов с 13 добавлено и 2 удалено
  1. 4 1
      client/client.go
  2. 1 0
      cmd/app.go
  3. 4 0
      cmd/publish.go
  4. 3 1
      cmd/subscribe.go
  5. 1 0
      docs/releases.md

+ 4 - 1
client/client.go

@@ -97,7 +97,10 @@ func (c *Client) Publish(topic, message string, options ...PublishOption) (*Mess
 // WithNoFirebase, and the generic WithHeader.
 func (c *Client) PublishReader(topic string, body io.Reader, options ...PublishOption) (*Message, error) {
 	topicURL := c.expandTopicURL(topic)
-	req, _ := http.NewRequest("POST", topicURL, body)
+	req, err := http.NewRequest("POST", topicURL, body)
+	if err != nil {
+		return nil, err
+	}
 	for _, option := range options {
 		if err := option(req); err != nil {
 			return nil, err

+ 1 - 0
cmd/app.go

@@ -29,6 +29,7 @@ var flagsDefault = []cli.Flag{
 
 var (
 	logLevelOverrideRegex = regexp.MustCompile(`(?i)^([^=\s]+)(?:\s*=\s*(\S+))?\s*->\s*(TRACE|DEBUG|INFO|WARN|ERROR)$`)
+	topicRegex            = regexp.MustCompile(`^[-_A-Za-z0-9]{1,64}$`) // Same as in server/server.go
 )
 
 // New creates a new CLI application

+ 4 - 0
cmd/publish.go

@@ -249,6 +249,10 @@ func parseTopicMessageCommand(c *cli.Context) (topic string, message string, com
 	if c.String("message") != "" {
 		message = c.String("message")
 	}
+	if !topicRegex.MatchString(topic) {
+		err = fmt.Errorf("topic %s contains invalid characters", topic)
+		return
+	}
 	return
 }
 

+ 3 - 1
cmd/subscribe.go

@@ -72,7 +72,7 @@ ntfy subscribe TOPIC COMMAND
     $NTFY_TITLE     $title, $t            Message title
     $NTFY_PRIORITY  $priority, $prio, $p  Message priority (1=min, 5=max)
     $NTFY_TAGS      $tags, $tag, $ta      Message tags (comma separated list)
-	$NTFY_RAW       $raw                  Raw JSON message
+    $NTFY_RAW       $raw                  Raw JSON message
 
   Examples:
     ntfy sub mytopic 'notify-send "$m"'    # Execute command for incoming messages
@@ -108,6 +108,8 @@ func execSubscribe(c *cli.Context) error {
 	// Checks
 	if user != "" && token != "" {
 		return errors.New("cannot set both --user and --token")
+	} else if !topicRegex.MatchString(topic) {
+		return fmt.Errorf("topic %s contains invalid characters", topic)
 	}
 
 	if !fromConfig {

+ 1 - 0
docs/releases.md

@@ -1226,6 +1226,7 @@ and the [ntfy Android app](https://github.com/binwiederhier/ntfy-android/release
 
 * Support encoding any header as RFC 2047 ([#737](https://github.com/binwiederhier/ntfy/issues/737), thanks to [@cfouche3005](https://github.com/cfouche3005) for reporting)
 * Do not forward poll requests for UnifiedPush messages (no ticket, thanks to NoName for reporting)
+* Fix `ntfy pub %` segfaulting ([#760](https://github.com/binwiederhier/ntfy/issues/760), thanks to [@clesmian](https://github.com/clesmian) for reporting)
 
 **Maintenance:**