Skip to content
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
4 changes: 4 additions & 0 deletions solutions/webhook-chat-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Google Chat App Webhook

Please see related guide on how to
[send messages to Google Chat with incoming webhooks](https://developers.google.com/workspace/chat/quickstart/webhooks).
36 changes: 36 additions & 0 deletions solutions/webhook-chat-app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2022 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/


// [START chat_webhook]
/**
* Sends asynchronous message to Google Chat
* @return {Object} response
*/
async function webhook() {
const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN"

Choose a reason for hiding this comment

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

security-critical critical

Hardcoding credentials, even with placeholders, is a significant security risk. It can lead to accidental exposure of sensitive data if this code is committed with real values. It's a best practice to load sensitive information from environment variables. You should also add error handling for when these environment variables are not set.

Suggested change
const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN"
const url = `https://chat.googleapis.com/v1/spaces/${process.env.SPACE_ID}/messages?key=${process.env.KEY}&token=${process.env.TOKEN}`

const res = await fetch(url, {
method: "POST",
headers: {"Content-Type": "application/json; charset=UTF-8"},
body: JSON.stringify({
text: "Hello from a Node script!"
})
});
return await res.json();

Choose a reason for hiding this comment

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

high

The current implementation does not handle non-successful HTTP responses (e.g., 4xx, 5xx status codes). fetch only rejects on network errors, not on HTTP error statuses. If the API returns an error, res.json() will likely fail with an uninformative error if the response body is not JSON. You should check res.ok and throw a descriptive error if the request was not successful.

  if (!res.ok) {
    throw new Error(`HTTP error! status: ${res.status}, body: ${await res.text()}`);
  }
  return res.json();

}

webhook().then(res => console.log(res));

Choose a reason for hiding this comment

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

high

The promise returned by webhook() is not being handled for rejections. If the fetch call fails (e.g., network error, or an HTTP error is thrown), it will result in an unhandled promise rejection, which can crash the Node.js process. You should add a .catch() block to handle potential errors.

webhook().then(res => console.log(res)).catch(console.error);

// [END chat_webhook]
54 changes: 54 additions & 0 deletions solutions/webhook-chat-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions solutions/webhook-chat-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "webhook-chat-app",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"node-fetch": "^2.6.0"
},
"license": "Apache-2.0"
}
39 changes: 39 additions & 0 deletions solutions/webhook-chat-app/thread-reply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/


// [START chat_webhook_thread]
/**
* Sends asynchronous message to Google Chat
* @return {Object} response
*/
async function webhook() {
const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"

Choose a reason for hiding this comment

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

security-critical critical

Hardcoding credentials, even with placeholders, is a significant security risk. It can lead to accidental exposure of sensitive data if this code is committed with real values. It's a best practice to load sensitive information from environment variables. You should also add error handling for when these environment variables are not set.

Suggested change
const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
const url = `https://chat.googleapis.com/v1/spaces/${process.env.SPACE_ID}/messages?key=${process.env.KEY}&token=${process.env.TOKEN}&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD}`

const res = await fetch(url, {
method: "POST",
headers: {"Content-Type": "application/json; charset=UTF-8"},
body: JSON.stringify({
text: "Hello from a Node script!",
thread: {
threadKey: "THREAD_KEY_VALUE"
}
})
});
return await res.json();

Choose a reason for hiding this comment

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

high

The current implementation does not handle non-successful HTTP responses (e.g., 4xx, 5xx status codes). fetch only rejects on network errors, not on HTTP error statuses. If the API returns an error, res.json() will likely fail with an uninformative error if the response body is not JSON. You should check res.ok and throw a descriptive error if the request was not successful.

  if (!res.ok) {
    throw new Error(`HTTP error! status: ${res.status}, body: ${await res.text()}`);
  }
  return res.json();

}

webhook().then(res => console.log(res));

Choose a reason for hiding this comment

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

high

The promise returned by webhook() is not being handled for rejections. If the fetch call fails (e.g., network error, or an HTTP error is thrown), it will result in an unhandled promise rejection, which can crash the Node.js process. You should add a .catch() block to handle potential errors.

webhook().then(res => console.log(res)).catch(console.error);

// [END chat_webhook_thread]
Loading