CVE-2023-0739 (published 2023-02-08T05:15Z)

Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition') in GitHub repository answerdev/answer prior to 1.0.4.

CVSS v3 base score: 6.8, MEDIUM (CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:U/C:N/I:H/A:H)

Weakness information:
cwe_id: CWE-362
cwe_name: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
description: The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
extended_description: 
            This can have security implications when the expected synchronization is in security-critical code, such as recording whether a user is authenticated or modifying important state information that should not be influenced by an outsider.
            A race condition occurs within concurrent environments, and is effectively a property of a code sequence. Depending on the context, a code sequence may be in the form of a function call, a small number of instructions, a series of program invocations, etc.
            A race condition violates these properties, which are closely related:
               
                  Exclusivity - the code sequence is given exclusive access to the shared resource, i.e., no other code sequence can modify properties of the shared resource before the original sequence has completed execution.
                  Atomicity - the code sequence is behaviorally atomic, i.e., no other thread or process can concurrently execute the same sequence of instructions (or a subset) against the same resource.
               
            A race condition exists when an "interfering code sequence" can still access the shared resource, violating exclusivity. Programmers may assume that certain code sequences execute too quickly to be affected by an interfering code sequence; when they are not, this violates atomicity. For example, the single "x++" statement may appear atomic at the code layer, but it is actually non-atomic at the instruction layer, since it involves a read (the original value of x), followed by a computation (x+1), followed by a write (save the result to x).
            The interfering code sequence could be "trusted" or "untrusted." A trusted interfering code sequence occurs within the product; it cannot be modified by the attacker, and it can only be invoked indirectly. An untrusted interfering code sequence can be authored directly by the attacker, and typically it is external to the vulnerable product.
         
         

Fix information:
repo_url: https://github.com/answerdev/answer
commit_details: repo_url: https://github.com/answerdev/answer
author: aichy126
author_date: 2023-01-30 15:25:43+08:00
msg: update add db session
repository: repo_url: https://github.com/answerdev/answer
repo_name: apache/incubator-answer
description: A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
repo_language: Go
owner: answerdev
file_changes: filename: docs.go
change_type: ModificationType.MODIFY
diff: @@ -7507,11 +7507,6 @@ const docTemplate = `{
                     "type": "string",
                     "maxLength": 4096
                 },
-                "bio_html": {
-                    "description": "bio",
-                    "type": "string",
-                    "maxLength": 4096
-                },
                 "display_name": {
                     "description": "display_name",
                     "type": "string",

programming_language: Swift
filename: swagger.json
change_type: ModificationType.MODIFY
diff: @@ -7495,11 +7495,6 @@
                     "type": "string",
                     "maxLength": 4096
                 },
-                "bio_html": {
-                    "description": "bio",
-                    "type": "string",
-                    "maxLength": 4096
-                },
                 "display_name": {
                     "description": "display_name",
                     "type": "string",

programming_language: Swift
filename: swagger.yaml
change_type: ModificationType.MODIFY
diff: @@ -1587,10 +1587,6 @@ definitions:
         description: bio
         maxLength: 4096
         type: string
-      bio_html:
-        description: bio
-        maxLength: 4096
-        type: string
       display_name:
         description: display_name
         maxLength: 30

programming_language: CoffeeScript
filename: collection_repo.go
change_type: ModificationType.MODIFY
diff: @@ -11,6 +11,7 @@ import (
 	collectioncommon "github.com/answerdev/answer/internal/service/collection_common"
 	"github.com/answerdev/answer/internal/service/unique"
 	"github.com/segmentfault/pacman/errors"
+	"xorm.io/xorm"
 )
 
 // collectionRepo collection repository
@@ -29,15 +30,28 @@ func NewCollectionRepo(data *data.Data, uniqueIDRepo unique.UniqueIDRepo) collec
 
 // AddCollection add collection
 func (cr *collectionRepo) AddCollection(ctx context.Context, collection *entity.Collection) (err error) {
-	id, err := cr.uniqueIDRepo.GenUniqueIDStr(ctx, collection.TableName())
-	if err == nil {
-		collection.ID = id
-		_, err = cr.data.DB.Insert(collection)
+	_, err = cr.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
+		var has bool
+		dbcollection := &entity.Collection{}
+		result = nil
+		has, err = session.Where("user_id = ? and object_id = ?", collection.UserID, collection.ObjectID).Get(dbcollection)
 		if err != nil {
-			return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+			return
 		}
-	}
-	return nil
+		if has {
+			return
+		}
+		id, err := cr.uniqueIDRepo.GenUniqueIDStr(ctx, collection.TableName())
+		if err == nil {
+			collection.ID = id
+			_, err = session.Insert(collection)
+			if err != nil {
+				return result, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+			}
+		}
+		return
+	})
+	return err
 }
 
 // RemoveCollection delete collection

programming_language: Go