Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

fix the check for whether is_url to match all the other ones in codebase #3405

Merged
merged 5 commits into from
Jan 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3405.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix contains_url check to be consistent with other instances in code-base and check that value is an instance of string.
11 changes: 8 additions & 3 deletions synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from six import text_type

import jsonschema
from canonicaljson import json
from jsonschema import FormatChecker
Expand Down Expand Up @@ -353,7 +355,7 @@ def check(self, event):
sender = event.user_id
room_id = None
ev_type = "m.presence"
is_url = False
contains_url = False
else:
sender = event.get("sender", None)
if not sender:
Expand All @@ -368,13 +370,16 @@ def check(self, event):

room_id = event.get("room_id", None)
ev_type = event.get("type", None)
is_url = "url" in event.get("content", {})

content = event.get("content", {})
# check if there is a string url field in the content for filtering purposes
contains_url = isinstance(content.get("url"), text_type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the spec says "includes only events with a url key in their content"

it doesn't say it has to be a string...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this might be a good opportunity to clarify the spec so it makes sense)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, if there are compelling reasons to do so, then sure, let's change the spec. Are there, though?

[if you actually want to check it's a string, you probably want six.string_types rather than six.text_type]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not really a use case for a singular-named variable to contain an object/array, and it's somewhat implied it is a string anyways. Might as well just say it is a string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but (a) that's not quite the same as 'filters have to check for the stringiness' and (b) "might as well" doesn't sound worth the effort of changing the spec to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richvdh, existing code disagrees:

contains_url = "url" in content
if contains_url:
contains_url &= isinstance(content["url"], text_type)


return self.check_fields(
room_id,
sender,
ev_type,
is_url,
contains_url,
)

def check_fields(self, room_id, sender, event_type, contains_url):
Expand Down